Spaces:
Runtime error
Runtime error
init based on submission
Browse filesFiles changed (22)
- .eslintrc.jsonAdded+3
- .gitignoreAdded+40
- DockerfileAdded+70
- README copy.mdAdded+45
- app/api/classify/route.jsAdded+20
- app/classifier.jsAdded+32
- app/favicon.icoAdded
- app/fonts/GeistMonoVF.woffAdded
- app/fonts/GeistVF.woffAdded
- app/globals.cssAdded+21
- app/layout.jsAdded+30
- app/page.jsAdded+92
- jsconfig.jsonAdded+7
- next.config.mjsAdded+9
- package-lock.jsonAdded
- package.jsonAdded+24
- postcss.config.mjsAdded+8
- public/globe.svgAdded
- public/hf-logo.svgAdded
- public/next.svgAdded
- public/topcoder-logo.svgAdded
- tailwind.config.jsAdded+17
123{ "extends": "next/core-web-vitals"}12345678910111213141516171819202122232425262728293031323334353637383940# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies/node_modules/.pnp.pnp.*.yarn/*!.yarn/patches!.yarn/plugins!.yarn/releases!.yarn/versions
# testing/coverage
# next.js/.next//out/
# production/build
# misc.DS_Store*.pem
# debugnpm-debug.log*yarn-debug.log*yarn-error.log*
# env files (can opt-in for commiting if needed).env*
# vercel.vercel
# typescript*.tsbuildinfonext-env.d.ts12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970# Adapted from https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile# For more information, see https://nextjs.org/docs/pages/building-your-application/deploying#docker-image
# Use a base image for buildingFROM node:18-slim AS base
# Install gitRUN apt-get update && apt-get install -y git
# Clone the repository and navigate to the next-server folderWORKDIR /appRUN git clone https://github.com/huggingface/transformers.js-examples .
# Set the working directory to the next-server folderWORKDIR /app/next-server
# Install dependencies only when neededFROM base AS deps
# Install dependencies based on the preferred package managerRUN \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ elif [ -f package-lock.json ]; then npm ci; \ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ else echo "Lockfile not found." && exit 1; \ fi
# Rebuild the source code only when neededFROM base AS builderWORKDIR /app/next-serverCOPY --from=deps /app/next-server/node_modules ./node_modulesCOPY . .
RUN \ if [ -f yarn.lock ]; then yarn run build; \ elif [ -f package-lock.json ]; then npm run build; \ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ else echo "Lockfile not found." && exit 1; \ fi
# Production image, copy all the files and run nextFROM base AS runnerWORKDIR /app/next-server
ENV NODE_ENV=production
RUN addgroup --system --gid 1001 nodejsRUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/next-server/public ./public
# Set the correct permission for prerender cacheRUN mkdir .nextRUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image sizeCOPY --from=builder --chown=nextjs:nodejs /app/next-server/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/next-server/.next/static ./.next/static
USER nextjs
# Allow the running process to write model files to the cache folder.RUN mkdir -p /app/next-server/node_modules/@huggingface/transformers/.cacheRUN chmod 777 -R /app/next-server/node_modules/@huggingface/transformers/.cache
EXPOSE 3000
ENV PORT=3000ENV HOSTNAME="0.0.0.0"CMD ["node", "server.js"]123456789101112131415161718192021222324252627282930313233343536373839404142434445---title: Topcoder Hugging Face Space Template with Docker + Next.jsemoji: 🗄️colorFrom: bluecolorTo: purplesdk: dockerpinned: falseapp_port: 3000---
# Topcoder Hugging Face Space Template with Docker + Next.js
This project, bootstrapped using [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app), demonstrates how to use `@huggingface/transformers` in [Next.js](https://nextjs.org) in the Topcoder Huggig Face organization.
Original repo source: https://github.com/huggingface/transformers.js-examples/tree/main/next-server
## Instructions
1. Clone the repository:
```sh git clone https://github.com/topcoder-platform/tc-huggingface-spaces.git ```
2. Change directory to the `spaces/space-demo-template` project:
```sh cd tc-huggingface-spaces/spaces/space-demo-template ```
3. Install the dependencies:
```sh npm install ```
4. Run the development server:
```sh npm run dev ```
5. Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.6. You can start editing the page by modifying `app/page.js` (Next.js) and `app/api/classify/route.js` (Transformers.js). The page auto-updates as you edit the file.7. Enjoy developing at Topcoder with Hugging Face.1234567891011121314151617181920// 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 developmentconst 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]);}1234567891011121314151617181920212223242526272829303132"use client";
import { useEffect, useState } from "react";
export default function Classifier() { const [text, setText] = useState("I love Topcoder!"); const [result, setResult] = useState(null);
useEffect(() => { const params = new URLSearchParams(); params.append("text", text); const url = "/api/classify?" + params.toString();
fetch(url) .then((res) => res.json()) .then((o) => setResult(o)); }, [text]);
return ( <> <input value={text} onChange={(e) => setText(e.target.value)} className="border border-gray-300 rounded p-2 dark:bg-black dark:text-white w-full" ></input>
<pre className="border border-gray-300 rounded p-2 dark:bg-black dark:text-white w-full min-h-[120px]"> {result ? JSON.stringify(result, null, 2) : "Loading…"} </pre> </> );}123456789101112131415161718192021@tailwind base;@tailwind components;@tailwind utilities;
:root { --background: #ffffff; --foreground: #171717;}
@media (prefers-color-scheme: dark) { :root { --background: #0a0a0a; --foreground: #ededed; }}
body { color: var(--foreground); background: var(--background); font-family: Arial, Helvetica, sans-serif;}123456789101112131415161718192021222324252627282930import localFont from "next/font/local";import "./globals.css";
const geistSans = localFont({ src: "./fonts/GeistVF.woff", variable: "--font-geist-sans", weight: "100 900",});const geistMono = localFont({ src: "./fonts/GeistMonoVF.woff", variable: "--font-geist-mono", weight: "100 900",});
export const metadata = { title: "Topcoder Hugging Face Space Template with Docker + Next.js", description: "Generated by create next app",};
export default function RootLayout({ children }) { return ( <html lang="en"> <body className={`${geistSans.variable} ${geistMono.variable} antialiased`} > {children} </body> </html> );}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import Image from "next/image";import Classifier from "./classifier";
export default function Home() { return ( <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-8 font-[family-name:var(--font-geist-sans)]"> <main className="flex flex-col gap-8 row-start-2 items-center sm:items-start"> <div className="flex items-center"> <Image src="/topcoder-logo.svg" alt="Topcoder logo" width={201} height={31} priority /> <span className="text-4xl font-light mx-5">×</span> <Image src="/hf-logo.svg" alt="Hugging Face logo" width={50} height={50} priority /> <span className="text-4xl font-light mx-5">×</span> <Image className="dark:invert" src="/next.svg" alt="Next.js logo" width={180} height={38} priority /> </div> <ol className="list-inside list-decimal text-sm text-center sm:text-left font-[family-name:var(--font-geist-mono)]"> <li className="mb-2"> Get started by editing{" "} <code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-semibold"> app/page.js </code> . </li> <li className="mb-2"> Update Transformers.js code in{" "} <code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-semibold"> app/api/classify/route.js </code> . </li> <li>Save and see your changes instantly.</li> </ol>
<div className="flex gap-4 items-center flex-col sm:flex-row"> <a className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5" href="https://github.com/topcoder-platform/tc-huggingface-spaces.git" target="_blank" rel="noopener noreferrer" > Source code </a>
{/* <a className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5" href="https://huggingface.co/docs/transformers.js/index" target="_blank" rel="noopener noreferrer" > Read our docs </a> */} </div> <Classifier /> </main> <footer className="row-start-3 flex gap-6 flex-wrap items-center justify-center"> <a className="flex items-center gap-2 hover:underline hover:underline-offset-4" href="https://www.topcoder.com/ai-hub/competitions?utm_campaign=huggingface&utm_source=huggingface&utm_medium=Topcoder" target="_blank" rel="noopener noreferrer" > <Image aria-hidden src="/globe.svg" alt="Globe icon" width={16} height={16} /> Go to Topcoder AI Hub Competitions → </a> </footer> </div> );}1234567{ "compilerOptions": { "paths": { "@/*": ["./*"] } }}123456789/** @type {import('next').NextConfig} */const nextConfig = { // https://nextjs.org/docs/app/api-reference/config/next-config-js/output output: "standalone", // https://nextjs.org/docs/app/api-reference/config/next-config-js/serverExternalPackages serverExternalPackages: ["@huggingface/transformers"],};
export default nextConfig;123456789101112131415161718192021222324{ "name": "tc-hugginface-next-server-template", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev --turbopack", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@huggingface/transformers": "3.5.0", "next": "^15.2.4", "react": "^19.0.0", "react-dom": "^19.0.0" }, "devDependencies": { "@eslint/eslintrc": "^3", "eslint": "^9", "eslint-config-next": "15.1.6", "postcss": "^8", "tailwindcss": "^3.4.1" }}12345678/** @type {import('postcss-load-config').Config} */const config = { plugins: { tailwindcss: {}, },};
export default config;1234567891011121314151617/** @type {import('tailwindcss').Config} */module.exports = { content: [ "./pages/**/*.{js,ts,jsx,tsx,mdx}", "./components/**/*.{js,ts,jsx,tsx,mdx}", "./app/**/*.{js,ts,jsx,tsx,mdx}", ], theme: { extend: { colors: { background: "var(--background)", foreground: "var(--foreground)", }, }, }, plugins: [],};.eslintrc.jsonAdded+3
.gitignoreAdded+40
DockerfileAdded+70
README copy.mdAdded+45
app/api/classify/route.jsAdded+20
app/classifier.jsAdded+32
app/favicon.icoAdded
app/fonts/GeistMonoVF.woffAdded
app/fonts/GeistVF.woffAdded
app/globals.cssAdded+21
app/layout.jsAdded+30
app/page.jsAdded+92
jsconfig.jsonAdded+7
next.config.mjsAdded+9
package-lock.jsonAdded
package.jsonAdded+24
postcss.config.mjsAdded+8
public/globe.svgAdded
public/hf-logo.svgAdded
public/next.svgAdded
public/topcoder-logo.svgAdded
tailwind.config.jsAdded+17