Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Env returns readonly in typescript #361

Closed
kyeshmz opened this issue Oct 17, 2023 · 2 comments · Fixed by #368
Closed

[Bug] Env returns readonly in typescript #361

kyeshmz opened this issue Oct 17, 2023 · 2 comments · Fixed by #368
Labels
bug Something isn't working

Comments

@kyeshmz
Copy link

kyeshmz commented Oct 17, 2023

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
@kyeshmz kyeshmz added the bug Something isn't working label Oct 17, 2023
@xenova
Copy link
Owner

xenova commented Oct 18, 2023

@kungfooman, have any ideas? 😇

@Kit-p
Copy link
Contributor

Kit-p commented Oct 24, 2023

@kyeshmz Regarding your problem on global, it is not related to this project.

A quick way to test is change your tsconfig.json, set "strict": false instead of "strict": true, the error should go away. This is how the prisma example you referred to in the comments work.

But, I do not encourage the above. A proper way to solve the problem can be something like this:

declare global {
  var PipelineSingleton: ReturnType<typeof P>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants