Skip to content

Add space demo template #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add space demo template #1

wants to merge 1 commit into from

Conversation

kkartunov
Copy link
Contributor

Creates PR with demo code base for a Hugging Face space in the Topcoder organization.

FROM node:18-slim AS base

# Install git
RUN apt-get update && apt-get install -y git

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using apt-get install -y --no-install-recommends git to avoid installing unnecessary recommended packages, which can help reduce the image size.


# Clone the repository and navigate to the next-server folder
WORKDIR /app
RUN git clone https://github.com/huggingface/transformers.js-examples .

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloning the entire repository might include unnecessary files. Consider using a shallow clone or copying only the necessary files to reduce the image size.


# Install dependencies based on the preferred package manager
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding --production flag to yarn and npm ci commands to install only production dependencies, which can help reduce the image size.


# Allow the running process to write model files to the cache folder.
RUN mkdir -p /app/next-server/node_modules/@huggingface/transformers/.cache
RUN chmod 777 -R /app/next-server/node_modules/@huggingface/transformers/.cache

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using chmod 777 can pose security risks by allowing all users to read, write, and execute. Consider setting more restrictive permissions.

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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using globalThis to store the classifier can lead to potential issues in environments where globalThis is shared across multiple requests or instances. Consider using a more isolated storage mechanism if this is a concern.

const text = request.nextUrl.searchParams.get("text");

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive error message to help users understand what input is expected, such as 'Query parameter "text" is required'.

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that result[0] is always defined before accessing it to prevent potential runtime errors. Consider adding a check to handle cases where result might be empty or undefined.

params.append("text", text);
const url = "/api/classify?" + params.toString();

fetch(url)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for the fetch request to handle potential network errors or server issues. This can improve the user experience by providing feedback if something goes wrong.


fetch(url)
.then((res) => res.json())
.then((o) => setResult(o));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name 'o' is not descriptive. Consider renaming it to something more meaningful, like 'data' or 'response', to improve code readability.


export const metadata = {
title: "Topcoder Hugging Face Space Template with Docker + Next.js",
description: "Generated by create next app",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description in the metadata object is generic. Consider providing a more specific description that accurately reflects the purpose or functionality of this template.

/>
<span className="text-4xl font-light mx-5">&#xd7;</span>
<Image
className="dark:invert"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a fallback for the className attribute to ensure consistent styling in environments where the dark mode class might not be applied.

Source code
</a>

{/* <a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commented-out code block for the 'Read our docs' link should be removed if it's not intended for future use, to keep the codebase clean.

@@ -0,0 +1 @@
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><g clip-path="url(#a)"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.27 14.1a6.5 6.5 0 0 0 3.67-3.45q-1.24.21-2.7.34-.31 1.83-.97 3.1M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.48-1.52a7 7 0 0 1-.96 0H7.5a4 4 0 0 1-.84-1.32q-.38-.89-.63-2.08a40 40 0 0 0 3.92 0q-.25 1.2-.63 2.08a4 4 0 0 1-.84 1.31zm2.94-4.76q1.66-.15 2.95-.43a7 7 0 0 0 0-2.58q-1.3-.27-2.95-.43a18 18 0 0 1 0 3.44m-1.27-3.54a17 17 0 0 1 0 3.64 39 39 0 0 1-4.3 0 17 17 0 0 1 0-3.64 39 39 0 0 1 4.3 0m1.1-1.17q1.45.13 2.69.34a6.5 6.5 0 0 0-3.67-3.44q.65 1.26.98 3.1M8.48 1.5l.01.02q.41.37.84 1.31.38.89.63 2.08a40 40 0 0 0-3.92 0q.25-1.2.63-2.08a4 4 0 0 1 .85-1.32 7 7 0 0 1 .96 0m-2.75.4a6.5 6.5 0 0 0-3.67 3.44 29 29 0 0 1 2.7-.34q.31-1.83.97-3.1M4.58 6.28q-1.66.16-2.95.43a7 7 0 0 0 0 2.58q1.3.27 2.95.43a18 18 0 0 1 0-3.44m.17 4.71q-1.45-.12-2.69-.34a6.5 6.5 0 0 0 3.67 3.44q-.65-1.27-.98-3.1" fill="#666"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h16v16H0z"/></clipPath></defs></svg>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended to add a newline at the end of the file to adhere to POSIX standards and improve compatibility with various tools.

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

Successfully merging this pull request may close these issues.

1 participant