runMark commited on
Commit
e151700
·
verified ·
1 Parent(s): 9980f57

Upload 18 files

Browse files
.eslintrc.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "extends": "next/core-web-vitals"
3
+ }
.gitignore ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2
+
3
+ # dependencies
4
+ /node_modules
5
+ /.pnp
6
+ .pnp.js
7
+ .yarn/install-state.gz
8
+
9
+ # testing
10
+ /coverage
11
+
12
+ # next.js
13
+ /.next/
14
+ /out/
15
+
16
+ # production
17
+ /build
18
+
19
+ # misc
20
+ .DS_Store
21
+ *.pem
22
+
23
+ # debug
24
+ npm-debug.log*
25
+ yarn-debug.log*
26
+ yarn-error.log*
27
+
28
+ # local env files
29
+ .env*.local
30
+
31
+ # vercel
32
+ .vercel
33
+
34
+ # typescript
35
+ *.tsbuildinfo
36
+ next-env.d.ts
Dockerfile ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # syntax=docker/dockerfile:1.4
2
+
3
+ # Adapted from https://github.com/vercel/next.js/blob/e60a1e747c3f521fc24dfd9ee2989e13afeb0a9b/examples/with-docker/Dockerfile
4
+ # For more information, see https://nextjs.org/docs/pages/building-your-application/deploying#docker-image
5
+
6
+ FROM node:18 AS base
7
+
8
+ # Install dependencies only when needed
9
+ FROM base AS deps
10
+ WORKDIR /app
11
+
12
+ # Install dependencies based on the preferred package manager
13
+ COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
14
+ RUN \
15
+ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
16
+ elif [ -f package-lock.json ]; then npm ci; \
17
+ elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
18
+ else echo "Lockfile not found." && exit 1; \
19
+ fi
20
+
21
+
22
+ # Rebuild the source code only when needed
23
+ FROM base AS builder
24
+ WORKDIR /app
25
+ COPY --from=deps --link /app/node_modules ./node_modules
26
+ COPY --link . .
27
+
28
+ # Next.js collects completely anonymous telemetry data about general usage.
29
+ # Learn more here: https://nextjs.org/telemetry
30
+ # Uncomment the following line in case you want to disable telemetry during the build.
31
+ # ENV NEXT_TELEMETRY_DISABLED 1
32
+
33
+ RUN npm run build
34
+
35
+ # If using yarn comment out above and use below instead
36
+ # RUN yarn build
37
+
38
+ # Production image, copy all the files and run next
39
+ FROM base AS runner
40
+ WORKDIR /app
41
+
42
+ ENV NODE_ENV production
43
+ # Uncomment the following line in case you want to disable telemetry during runtime.
44
+ # ENV NEXT_TELEMETRY_DISABLED 1
45
+
46
+ RUN \
47
+ addgroup --system --gid 1001 nodejs; \
48
+ adduser --system --uid 1001 nextjs
49
+
50
+ COPY --from=builder --link /app/public ./public
51
+
52
+ # Automatically leverage output traces to reduce image size
53
+ # https://nextjs.org/docs/advanced-features/output-file-tracing
54
+ COPY --from=builder --link --chown=1001:1001 /app/.next/standalone ./
55
+ COPY --from=builder --link --chown=1001:1001 /app/.next/static ./.next/static
56
+
57
+ USER nextjs
58
+
59
+ EXPOSE 3000
60
+
61
+ ENV PORT 3000
62
+ ENV HOSTNAME 0.0.0.0
63
+
64
+ # Allow the running process to write model files to the cache folder.
65
+ # NOTE: In practice, you would probably want to pre-download the model files to avoid having to download them on-the-fly.
66
+ RUN mkdir -p /app/node_modules/@xenova/.cache/
67
+ RUN chmod 777 -R /app/node_modules/@xenova/
68
+
69
+ CMD ["node", "server.js"]
README.md CHANGED
@@ -1,10 +1,36 @@
1
- ---
2
- title: Transformer In Server
3
- emoji: 🔥
4
- colorFrom: blue
5
- colorTo: indigo
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2
+
3
+ ## Getting Started
4
+
5
+ First, run the development server:
6
+
7
+ ```bash
8
+ npm run dev
9
+ # or
10
+ yarn dev
11
+ # or
12
+ pnpm dev
13
+ # or
14
+ bun dev
15
+ ```
16
+
17
+ Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18
+
19
+ You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.
20
+
21
+ This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
22
+
23
+ ## Learn More
24
+
25
+ To learn more about Next.js, take a look at the following resources:
26
+
27
+ - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28
+ - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
29
+
30
+ You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
31
+
32
+ ## Deploy on Vercel
33
+
34
+ The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
35
+
36
+ Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
app/classification/pipeline.js ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { pipeline } from "@xenova/transformers";
2
+
3
+ // define pipeline singleton class to get a pipeline
4
+ const P = () => class PipelineSingleton {
5
+ static task = 'text-classification';
6
+ static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english';
7
+ static instance = null;
8
+
9
+ static async getInstance(progress_callback = null) {
10
+ if (this.instance === null) {
11
+ this.instance = await pipeline(this.task, this.model, { progress_callback });
12
+ }
13
+ return this.instance;
14
+ }
15
+ }
16
+
17
+ let PipelineSingleton;
18
+
19
+ if (process.env.NODE_ENV !== 'production') {
20
+ if (!global.PipelineSingleton) {
21
+ global.PipelineSingleton = P();
22
+ }
23
+ PipelineSingleton = global.PipelineSingleton;
24
+ } else {
25
+ PipelineSingleton = P();
26
+ }
27
+
28
+ export default PipelineSingleton;
app/classification/route.js ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { NextResponse } from "next/server";
2
+ import PipelineSingleton from "./pipeline";
3
+
4
+ export async function GET(request) {
5
+
6
+ const text = request.nextUrl.searchParams.get('text');
7
+
8
+ if (!text) {
9
+ return NextResponse.json({
10
+ error: 'Missing text parameter',
11
+ }, { status: 400 });
12
+ }
13
+
14
+ const classifier = await PipelineSingleton.getInstance();
15
+
16
+ const result = await classifier(text);
17
+
18
+ return NextResponse.json(result);
19
+ }
app/favicon.ico ADDED
app/globals.css ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
4
+
5
+ :root {
6
+ --foreground-rgb: 0, 0, 0;
7
+ --background-start-rgb: 214, 219, 220;
8
+ --background-end-rgb: 255, 255, 255;
9
+ }
10
+
11
+ @media (prefers-color-scheme: dark) {
12
+ :root {
13
+ --foreground-rgb: 255, 255, 255;
14
+ --background-start-rgb: 0, 0, 0;
15
+ --background-end-rgb: 0, 0, 0;
16
+ }
17
+ }
18
+
19
+ body {
20
+ color: rgb(var(--foreground-rgb));
21
+ background: linear-gradient(
22
+ to bottom,
23
+ transparent,
24
+ rgb(var(--background-end-rgb))
25
+ )
26
+ rgb(var(--background-start-rgb));
27
+ }
28
+
29
+ @layer utilities {
30
+ .text-balance {
31
+ text-wrap: balance;
32
+ }
33
+ }
app/layout.js ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Inter } from "next/font/google";
2
+ import "./globals.css";
3
+
4
+ const inter = Inter({ subsets: ["latin"] });
5
+
6
+ export const metadata = {
7
+ title: "Create Next App",
8
+ description: "Generated by create next app",
9
+ };
10
+
11
+ export default function RootLayout({ children }) {
12
+ return (
13
+ <html lang="en">
14
+ <body className={inter.className}>{children}</body>
15
+ </html>
16
+ );
17
+ }
app/page.js ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use client';
2
+
3
+ import { useState } from "react";
4
+
5
+ export default function Home() {
6
+
7
+ const [result, setResult] = useState(null);
8
+ const [ready, setReady] = useState(false);
9
+
10
+ const classify = async (text) => {
11
+ if (!text) return;
12
+
13
+ const result = await fetch(`/classification?text=${encodeURIComponent(text)}`);
14
+
15
+ if (!ready) setReady(true);
16
+
17
+ const json = await result.json();
18
+ setResult(json);
19
+ };
20
+
21
+ return (
22
+ <main className="flex min-h-screen flex-col items-center justify-center p-12">
23
+ <h1 className="text-5xl font-bold mb-2 text-center">Transformers.js</h1>
24
+ <h2 className="text-2xl mb-4 text-center">Next.js template (server-side)</h2>
25
+ <input
26
+ type="text"
27
+ className="w-full max-w-xs p-2 border border-gray-300 rounded mb-4"
28
+ placeholder="Enter text here"
29
+ onInput={e => {
30
+ classify(e.target.value);
31
+ }}
32
+ />
33
+
34
+ {ready !== null && (
35
+ <pre className="bg-gray-100 p-2 rounded">
36
+ {
37
+ (!ready || !result) ? 'Loading...' : JSON.stringify(result, null, 2)}
38
+ </pre>
39
+ )}
40
+ </main>
41
+ );
42
+ }
jsconfig.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "compilerOptions": {
3
+ "paths": {
4
+ "@/*": ["./*"]
5
+ }
6
+ }
7
+ }
next.config.mjs ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /** @type {import('next').NextConfig} */
2
+ const nextConfig = {
3
+
4
+ // (Optional) Export as a standalone site
5
+ // See https://nextjs.org/docs/pages/api-reference/next-config-js/output#automatically-copying-traced-files
6
+ output: 'standalone', // Feel free to modify/remove this option
7
+
8
+ // Indicate that these packages should not be bundled by webpack
9
+ experimental: {
10
+ serverComponentsExternalPackages: ['sharp', 'onnxruntime-node'],
11
+ },
12
+ };
13
+
14
+ export default nextConfig;
package-lock.json ADDED
The diff for this file is too large to render. See raw diff
 
package.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "transformer-in-server",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "scripts": {
6
+ "dev": "next dev",
7
+ "build": "next build",
8
+ "start": "next start",
9
+ "lint": "next lint"
10
+ },
11
+ "dependencies": {
12
+ "@xenova/transformers": "^2.17.2",
13
+ "next": "14.2.4",
14
+ "react": "^18",
15
+ "react-dom": "^18"
16
+ },
17
+ "devDependencies": {
18
+ "eslint": "^8",
19
+ "eslint-config-next": "14.2.4",
20
+ "postcss": "^8",
21
+ "tailwindcss": "^3.4.1"
22
+ }
23
+ }
postcss.config.mjs ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ /** @type {import('postcss-load-config').Config} */
2
+ const config = {
3
+ plugins: {
4
+ tailwindcss: {},
5
+ },
6
+ };
7
+
8
+ export default config;
public/next.svg ADDED
public/vercel.svg ADDED
tailwind.config.js ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /** @type {import('tailwindcss').Config} */
2
+ module.exports = {
3
+ content: [
4
+ "./pages/**/*.{js,ts,jsx,tsx,mdx}",
5
+ "./components/**/*.{js,ts,jsx,tsx,mdx}",
6
+ "./app/**/*.{js,ts,jsx,tsx,mdx}",
7
+ ],
8
+ theme: {
9
+ extend: {
10
+ backgroundImage: {
11
+ "gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
12
+ "gradient-conic":
13
+ "conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
14
+ },
15
+ },
16
+ },
17
+ plugins: [],
18
+ };