You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been trying to implement server side sentiment analysis using this tutorial but its in Javascript. I looked through the docs but there seems to be no information on implementing it using Typescript. So far I have integrated Typescript but there is one error that is difficult to fix. This is what I have implemented so far:
pipeline.ts
import{pipeline,PipelineType}from"@huggingface/transformers";// Use the Singleton pattern to enable lazy construction of the pipeline.// NOTE: We wrap the class in a function to prevent code duplication (see below).constP=()=>classPipelineSingleton{statictask: PipelineType='text-classification';staticmodel='Xenova/distilbert-base-uncased-finetuned-sst-2-english';staticinstance: PipelineSingleton|null=null;// eslint-disable-next-line @typescript-eslint/no-unsafe-function-typestaticasyncgetInstance(progress_callback: Function|undefined=undefined){if(!this.instance){this.instance=pipeline(this.task,this.model,{ progress_callback });}returnthis.instance;}}letPipelineSingleton: ReturnType<typeofP>;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-postgresconstglobalWithPipeline=globalastypeofglobal&{PipelineSingleton: ReturnType<typeofP>};if(!globalWithPipeline.PipelineSingleton){globalWithPipeline.PipelineSingleton=P();}PipelineSingleton=globalWithPipeline.PipelineSingleton;}else{PipelineSingleton=P();}exportdefaultPipelineSingleton;
request.ts
import{NextResponse}from'next/server'importPipelineSingletonfrom'./pipeline';exportasyncfunctionGET(request: Request){// Extract the text parameter from the query stringconsturl=newURL(request.url);consttext=url.searchParams.get('text');if(!text){returnNextResponse.json({error: 'Missing text parameter',},{status: 400});}// Get the classification pipeline. When called for the first time,// this will load the pipeline and cache it for future use.constclassifier=awaitPipelineSingleton.getInstance();// SHOWS THE ERROR - Type 'PipelineSingleton' has no call signatures.ts(2349)// Actually perform the classificationconstresult=awaitclassifier(text);returnNextResponse.json(result);}
The problem is in the routes.ts when calling the classifier method. Typescript shows the error:
This expression is not callable.
Type 'PipelineSingleton' has no call signatures.ts(2349)
So this probably means that my Typescript implementation is incorrect for Pipeline. Would appreciate any help on this. TIA.
The text was updated successfully, but these errors were encountered:
Not sure if this is a good fix or not but I kind of fixed the type issue for classifier method by typecasting the response from getInstance method to Pipeline type. The current version (pipeline.ts same as before):
route.ts
import{NextResponse}from'next/server'importPipelineSingletonfrom'./pipeline';import{Pipeline}from'@huggingface/transformers';exportasyncfunctionGET(request: Request){// Extract the text parameter from the query stringconsturl=newURL(request.url);consttext=url.searchParams.get('text');if(!text){returnNextResponse.json({error: 'Missing text parameter',},{status: 400});}// Get the classification pipeline. When called for the first time,// this will load the pipeline and cache it for future use.constclassifier=awaitPipelineSingleton.getInstance()asPipeline;// UPDATED THIS LINE// Actually perform the classificationconstresult=awaitclassifier?.(text);returnNextResponse.json(result);}
Question
I have been trying to implement server side sentiment analysis using this tutorial but its in Javascript. I looked through the docs but there seems to be no information on implementing it using Typescript. So far I have integrated Typescript but there is one error that is difficult to fix. This is what I have implemented so far:
pipeline.ts
request.ts
The problem is in the routes.ts when calling the classifier method. Typescript shows the error:
So this probably means that my Typescript implementation is incorrect for Pipeline. Would appreciate any help on this. TIA.
The text was updated successfully, but these errors were encountered: