Spaces:
Sleeping
Sleeping
Upload 18 files
Browse files- .eslintrc.json +3 -0
- .gitignore +35 -0
- Dockerfile +64 -0
- README.md +34 -10
- jsconfig.json +7 -0
- next.config.js +13 -0
- package-lock.json +0 -0
- package.json +25 -0
- postcss.config.js +6 -0
- public/next.svg +1 -0
- public/vercel.svg +1 -0
- src/app/classify/pipeline.js +30 -0
- src/app/classify/route.js +22 -0
- src/app/favicon.ico +0 -0
- src/app/globals.css +27 -0
- src/app/layout.js +17 -0
- src/app/page.js +45 -0
- tailwind.config.js +18 -0
.eslintrc.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"extends": "next/core-web-vitals"
|
3 |
+
}
|
.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
8 |
+
# testing
|
9 |
+
/coverage
|
10 |
+
|
11 |
+
# next.js
|
12 |
+
/.next/
|
13 |
+
/out/
|
14 |
+
|
15 |
+
# production
|
16 |
+
/build
|
17 |
+
|
18 |
+
# misc
|
19 |
+
.DS_Store
|
20 |
+
*.pem
|
21 |
+
|
22 |
+
# debug
|
23 |
+
npm-debug.log*
|
24 |
+
yarn-debug.log*
|
25 |
+
yarn-error.log*
|
26 |
+
|
27 |
+
# local env files
|
28 |
+
.env*.local
|
29 |
+
|
30 |
+
# vercel
|
31 |
+
.vercel
|
32 |
+
|
33 |
+
# typescript
|
34 |
+
*.tsbuildinfo
|
35 |
+
next-env.d.ts
|
Dockerfile
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 localhost
|
63 |
+
|
64 |
+
CMD ["node", "server.js"]
|
README.md
CHANGED
@@ -1,10 +1,34 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
```
|
14 |
+
|
15 |
+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
16 |
+
|
17 |
+
You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.
|
18 |
+
|
19 |
+
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
|
20 |
+
|
21 |
+
## Learn More
|
22 |
+
|
23 |
+
To learn more about Next.js, take a look at the following resources:
|
24 |
+
|
25 |
+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
26 |
+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
27 |
+
|
28 |
+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
|
29 |
+
|
30 |
+
## Deploy on Vercel
|
31 |
+
|
32 |
+
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.
|
33 |
+
|
34 |
+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
|
jsconfig.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"compilerOptions": {
|
3 |
+
"paths": {
|
4 |
+
"@/*": ["./src/*"]
|
5 |
+
}
|
6 |
+
}
|
7 |
+
}
|
next.config.js
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/** @type {import('next').NextConfig} */
|
2 |
+
const nextConfig = {
|
3 |
+
// (Optional) Export as a standalone site
|
4 |
+
// See https://nextjs.org/docs/pages/api-reference/next-config-js/output#automatically-copying-traced-files
|
5 |
+
output: 'standalone', // Feel free to modify/remove this option
|
6 |
+
|
7 |
+
// Indicate that these packages should not be bundled by webpack
|
8 |
+
experimental: {
|
9 |
+
serverComponentsExternalPackages: ['sharp', 'onnxruntime-node'],
|
10 |
+
},
|
11 |
+
};
|
12 |
+
|
13 |
+
module.exports = nextConfig;
|
package-lock.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
package.json
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "next",
|
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.4.2",
|
13 |
+
"autoprefixer": "10.4.14",
|
14 |
+
"eslint": "8.45.0",
|
15 |
+
"eslint-config-next": "13.4.12",
|
16 |
+
"next": "13.4.12",
|
17 |
+
"postcss": "8.4.27",
|
18 |
+
"react": "18.2.0",
|
19 |
+
"react-dom": "18.2.0",
|
20 |
+
"tailwindcss": "3.3.3"
|
21 |
+
},
|
22 |
+
"overrides": {
|
23 |
+
"protobufjs": "^7.2.4"
|
24 |
+
}
|
25 |
+
}
|
postcss.config.js
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
module.exports = {
|
2 |
+
plugins: {
|
3 |
+
tailwindcss: {},
|
4 |
+
autoprefixer: {},
|
5 |
+
},
|
6 |
+
}
|
public/next.svg
ADDED
public/vercel.svg
ADDED
src/app/classify/pipeline.js
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { pipeline } from "@xenova/transformers";
|
2 |
+
|
3 |
+
// Use the Singleton pattern to enable lazy construction of the pipeline.
|
4 |
+
// NOTE: We wrap the class in a function to prevent code duplication (see below).
|
5 |
+
const P = () => class PipelineSingleton {
|
6 |
+
static task = 'text-classification';
|
7 |
+
static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english';
|
8 |
+
static instance = null;
|
9 |
+
|
10 |
+
static async getInstance(progress_callback = null) {
|
11 |
+
if (this.instance === null) {
|
12 |
+
this.instance = pipeline(this.task, this.model, { progress_callback });
|
13 |
+
}
|
14 |
+
return this.instance;
|
15 |
+
}
|
16 |
+
}
|
17 |
+
|
18 |
+
let PipelineSingleton;
|
19 |
+
if (process.env.NODE_ENV !== 'production') {
|
20 |
+
// When running in development mode, attach the pipeline to the
|
21 |
+
// global object so that it's preserved between hot reloads.
|
22 |
+
// For more information, see https://vercel.com/guides/nextjs-prisma-postgres
|
23 |
+
if (!global.PipelineSingleton) {
|
24 |
+
global.PipelineSingleton = P();
|
25 |
+
}
|
26 |
+
PipelineSingleton = global.PipelineSingleton;
|
27 |
+
} else {
|
28 |
+
PipelineSingleton = P();
|
29 |
+
}
|
30 |
+
export default PipelineSingleton;
|
src/app/classify/route.js
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Create a custom request handler for the /classify route.
|
2 |
+
// For more information, see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
|
3 |
+
|
4 |
+
import { NextResponse } from 'next/server'
|
5 |
+
import PipelineSingleton from './pipeline.js';
|
6 |
+
|
7 |
+
export async function GET(request) {
|
8 |
+
const text = request.nextUrl.searchParams.get('text');
|
9 |
+
if (!text) {
|
10 |
+
return NextResponse.json({
|
11 |
+
error: 'Missing text parameter',
|
12 |
+
}, { status: 400 });
|
13 |
+
}
|
14 |
+
// Get the classification pipeline. When called for the first time,
|
15 |
+
// this will load the pipeline and cache it for future use.
|
16 |
+
const classifier = await PipelineSingleton.getInstance();
|
17 |
+
|
18 |
+
// Actually perform the classification
|
19 |
+
const result = await classifier(text);
|
20 |
+
|
21 |
+
return NextResponse.json(result);
|
22 |
+
}
|
src/app/favicon.ico
ADDED
src/app/globals.css
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
}
|
src/app/layout.js
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import './globals.css'
|
2 |
+
import { Inter } from 'next/font/google'
|
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 |
+
}
|
src/app/page.js
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'use client'
|
2 |
+
|
3 |
+
import { useState } from 'react'
|
4 |
+
|
5 |
+
export default function Home() {
|
6 |
+
|
7 |
+
// Keep track of the classification result and the model loading status.
|
8 |
+
const [result, setResult] = useState(null);
|
9 |
+
const [ready, setReady] = useState(null);
|
10 |
+
|
11 |
+
const classify = async (text) => {
|
12 |
+
if (!text) return;
|
13 |
+
if (ready === null) setReady(false);
|
14 |
+
|
15 |
+
// Make a request to the /classify route on the server.
|
16 |
+
const result = await fetch(`/classify?text=${encodeURIComponent(text)}`);
|
17 |
+
|
18 |
+
// If this is the first time we've made a request, set the ready flag.
|
19 |
+
if (!ready) setReady(true);
|
20 |
+
|
21 |
+
const json = await result.json();
|
22 |
+
setResult(json);
|
23 |
+
};
|
24 |
+
return (
|
25 |
+
<main className="flex min-h-screen flex-col items-center justify-center p-12">
|
26 |
+
<h1 className="text-5xl font-bold mb-2 text-center">Transformers.js</h1>
|
27 |
+
<h2 className="text-2xl mb-4 text-center">Next.js template (server-side)</h2>
|
28 |
+
<input
|
29 |
+
type="text"
|
30 |
+
className="w-full max-w-xs p-2 border border-gray-300 rounded mb-4"
|
31 |
+
placeholder="Enter text here"
|
32 |
+
onInput={e => {
|
33 |
+
classify(e.target.value);
|
34 |
+
}}
|
35 |
+
/>
|
36 |
+
|
37 |
+
{ready !== null && (
|
38 |
+
<pre className="bg-gray-100 p-2 rounded">
|
39 |
+
{
|
40 |
+
(!ready || !result) ? 'Loading...' : JSON.stringify(result, null, 2)}
|
41 |
+
</pre>
|
42 |
+
)}
|
43 |
+
</main>
|
44 |
+
)
|
45 |
+
}
|
tailwind.config.js
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/** @type {import('tailwindcss').Config} */
|
2 |
+
module.exports = {
|
3 |
+
content: [
|
4 |
+
'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',
|
5 |
+
'./src/components/**/*.{js,ts,jsx,tsx,mdx}',
|
6 |
+
'./src/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 |
+
}
|