Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +13 -0
- main.py +31 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /code
|
| 5 |
+
# Install dependencies
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
# Copy app files
|
| 9 |
+
COPY main.py .
|
| 10 |
+
# Expose port for Hugging Face Spaces
|
| 11 |
+
EXPOSE 7860
|
| 12 |
+
# Start the FastAPI app using uvicorn
|
| 13 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf-cache"
|
| 3 |
+
os.environ["HF_HOME"] = "/tmp/hf-home"
|
| 4 |
+
|
| 5 |
+
from fastapi import FastAPI, Request
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
# Define FastAPI app
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Load zero-shot classification pipeline
|
| 13 |
+
classifier = pipeline(
|
| 14 |
+
"zero-shot-classification",
|
| 15 |
+
model="MoritzLaurer/deberta-v3-large-zeroshot-v2.0"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Define input schema
|
| 20 |
+
class InputText(BaseModel):
|
| 21 |
+
text: str
|
| 22 |
+
|
| 23 |
+
@app.post("/classify")
|
| 24 |
+
async def classify_text(data: InputText):
|
| 25 |
+
candidate_labels = ["contains electronic components", "does not contain electronic components"]
|
| 26 |
+
result = classifier(data.text, candidate_labels, multi_label=False)
|
| 27 |
+
return {
|
| 28 |
+
"input": data.text,
|
| 29 |
+
"label": result["labels"][0],
|
| 30 |
+
"score": result["scores"][0]
|
| 31 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers[sentencepiece]
|
| 4 |
+
torch
|