Spaces:
Runtime error
Runtime error
Commit ·
eead084
1
Parent(s): 1b7a941
Level 2: Integrated real Sentiment Analysis model
Browse files- Dockerfile +5 -5
- main.py +13 -5
- requirements.txt +3 -1
Dockerfile
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
-
# Step 1: Use a lightweight Python base image
|
| 2 |
FROM python:3.9-slim
|
| 3 |
|
| 4 |
-
# Step 2: Set the working directory inside the container
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
-
#
|
| 8 |
COPY requirements.txt .
|
| 9 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
| 12 |
COPY . .
|
| 13 |
|
| 14 |
-
# Step 5: Command to run the API using Uvicorn
|
| 15 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
|
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
+
# Install dependencies first (to cache this layer)
|
| 6 |
COPY requirements.txt .
|
| 7 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
|
| 9 |
+
# Pre-download the model so it's baked into the image
|
| 10 |
+
# This prevents the container from downloading it at runtime
|
| 11 |
+
RUN python -c "from transformers import pipeline; pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english')"
|
| 12 |
+
|
| 13 |
COPY . .
|
| 14 |
|
|
|
|
| 15 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
main.py
CHANGED
|
@@ -1,14 +1,22 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
@app.get("/")
|
| 6 |
def home():
|
| 7 |
-
return {"status": "
|
| 8 |
|
| 9 |
@app.post("/predict")
|
| 10 |
def predict(data: dict):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
+
# Load the model once when the server starts
|
| 7 |
+
# This is "Sentiment Analysis" by default
|
| 8 |
+
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 9 |
+
|
| 10 |
@app.get("/")
|
| 11 |
def home():
|
| 12 |
+
return {"status": "Level 2 Live", "model": "DistilBERT Sentiment"}
|
| 13 |
|
| 14 |
@app.post("/predict")
|
| 15 |
def predict(data: dict):
|
| 16 |
+
text = data.get("text", "")
|
| 17 |
+
if not text:
|
| 18 |
+
return {"error": "No text provided"}
|
| 19 |
+
|
| 20 |
+
# Run the model
|
| 21 |
+
result = classifier(text)
|
| 22 |
+
return {"input": text, "result": result[0]}
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
fastapi
|
| 2 |
-
uvicorn
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|