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

Support for Typescript docs #1055

Open
SadmanYasar opened this issue Nov 26, 2024 · 2 comments
Open

Support for Typescript docs #1055

SadmanYasar opened this issue Nov 26, 2024 · 2 comments
Labels
question Further information is requested

Comments

@SadmanYasar
Copy link

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

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).
const P = () => class PipelineSingleton {
    static task: PipelineType = 'text-classification';
    static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english';
    static instance: PipelineSingleton | null = null;

    // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
    static async getInstance(progress_callback: Function | undefined = undefined) {
        if (!this.instance) {
            this.instance = pipeline(this.task, this.model, { progress_callback });
        }
        return this.instance;
    }
}

let PipelineSingleton: ReturnType<typeof P>;
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
    const globalWithPipeline = global as typeof global & { PipelineSingleton: ReturnType<typeof P> };

    if (!globalWithPipeline.PipelineSingleton) {
        globalWithPipeline.PipelineSingleton = P();
    }

    PipelineSingleton = globalWithPipeline.PipelineSingleton;
} else {
    PipelineSingleton = P();
}
export default PipelineSingleton;

request.ts

import { NextResponse } from 'next/server'
import PipelineSingleton from './pipeline';

export async function GET(request: Request) {
    // Extract the text parameter from the query string
    const url = new URL(request.url);
    const text = url.searchParams.get('text');
    if (!text) {
        return NextResponse.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.
    const classifier = await PipelineSingleton.getInstance();  // SHOWS THE ERROR -  Type 'PipelineSingleton' has no call signatures.ts(2349)

    // Actually perform the classification
    const result = await classifier(text);

    return NextResponse.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.

@SadmanYasar SadmanYasar added the question Further information is requested label Nov 26, 2024
@SadmanYasar
Copy link
Author

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'
import PipelineSingleton from './pipeline';
import { Pipeline } from '@huggingface/transformers';

export async function GET(request: Request) {
    // Extract the text parameter from the query string
    const url = new URL(request.url);
    const text = url.searchParams.get('text');
    if (!text) {
        return NextResponse.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.
    const classifier = await PipelineSingleton.getInstance() as Pipeline; // UPDATED THIS LINE

    // Actually perform the classification
    const result = await classifier?.(text);

    return NextResponse.json(result);
}

@emojiiii
Copy link
Contributor

const task: 'text-classification' = 'text-classification';

or

static task = 'text-classification' as const;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants