File size: 660 Bytes
5a74c0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// https://nextjs.org/docs/app/building-your-application/routing/route-handlers

import { pipeline } from "@huggingface/transformers";

// NOTE: We attach the classifier to the global object to avoid unnecessary reloads during development
const classifier = (globalThis.classifier ??= await pipeline(
  "text-classification",
  "Xenova/distilbert-base-uncased-finetuned-sst-2-english",
));

export async function GET(request) {
  const text = request.nextUrl.searchParams.get("text");

  if (!text) {
    return Response.json({ message: "No text provided" }, { status: 400 });
  }

  const result = await classifier(text);
  return Response.json(result[0]);
}