Skip to content

[Bug] Env returns readonly in typescript #361

Closed
@kyeshmz

Description

@kyeshmz

Describe the bug
I am trying to configure my Next.js (<13 and using pages API) to utilize a local model.
#310 and documents refer to using

env.allowRemoteModels = false

which returns an warning as the following.

Cannot assign to 'allowRemoteModels' because it is a read-only property.ts(2540)

I also see that with the current documentation of adding PiplineSingleton to global, global seems to have an error of
Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017),
is there a way to get around this like the prisma client?

let PipelineSingleton: any
if (process.env.NODE_ENV !== 'production') {
  // When running in development mode, attach the pipeline to the
  // global object so that it's preserved between hot reloads.
  // For more information, see https://vercel.com/guides/nextjs-prisma-postgres
  if (!global.PipelineSingleton) {
    global.PipelineSingleton = P()
  }
  PipelineSingleton = global.PipelineSingleton
} else {
  PipelineSingleton = P()
}
export default PipelineSingleton

Is there some kind of typing to get around these problems?

How to reproduce
Steps or a minimal working example to reproduce the behavior

create a next app with this as a server utility, and calling it in API routes.

// let PipelineSingleton: any

import {
  PreTrainedTokenizer,
  Processor,
  PreTrainedModel,
  AutoTokenizer,
  AutoProcessor,
  AutoModel,
  env,
} from '@xenova/transformers'

env.allowRemoteModels = false

const P = () =>
  class PipelineSingleton {
    private static task: string = 'zero-shot-image-classification'
    private static modelID: string = 'Xenova/clip-vit-base-patch16'

    private static tokenizer: PreTrainedTokenizer | null = null
    private static processor: Processor | null = null
    private static model: PreTrainedModel | null = null

    public static async getInstance(progress_callback: (x: number) => void): Promise<{
      tokenizer: PreTrainedTokenizer
      processor: Processor
      model: PreTrainedModel
    }> {
      if (this.tokenizer === null && this.processor === null && this.model === null) {
        this.tokenizer = await AutoTokenizer.from_pretrained(this.modelID)

        this.processor = await AutoProcessor.from_pretrained(this.modelID)
        this.model = await AutoModel.from_pretrained(this.modelID)
      }

      return {
        tokenizer: this.tokenizer!,
        processor: this.processor!,
        model: this.model!,
      }
    }
  }

let PipelineSingleton: any
if (process.env.NODE_ENV !== 'production') {
  // When running in development mode, attach the pipeline to the
  // global object so that it's preserved between hot reloads.
  // For more information, see https://vercel.com/guides/nextjs-prisma-postgres
  if (!global.PipelineSingleton) {
    global.PipelineSingleton = P()
  }
  PipelineSingleton = global.PipelineSingleton
} else {
  PipelineSingleton = P()
}
export default PipelineSingleton

Expected behavior
A clear and concise description of what you expected to happen.

Typing error goes away

Environment

  • Transformers.js version: 2.6.2
  • Operating system (if applicable): Mac

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions