Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- Dockerfile +25 -0
- app.py +53 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Lightweight Python base
|
| 3 |
+
FROM python:3.12-slim
|
| 4 |
+
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
COPY . /app
|
| 7 |
+
|
| 8 |
+
# Create writable cache folder
|
| 9 |
+
RUN mkdir -p /app/model_cache && chmod -R 777 /app/model_cache
|
| 10 |
+
|
| 11 |
+
# Environment variables for Hugging Face cache
|
| 12 |
+
ENV HF_HOME=/app/model_cache
|
| 13 |
+
ENV TRANSFORMERS_CACHE=/app/model_cache
|
| 14 |
+
ENV HF_DATASETS_CACHE=/app/model_cache
|
| 15 |
+
ENV HF_METRICS_CACHE=/app/model_cache
|
| 16 |
+
|
| 17 |
+
# Install dependencies
|
| 18 |
+
RUN apt-get update && apt-get install -y git
|
| 19 |
+
RUN pip install --upgrade pip
|
| 20 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
+
|
| 22 |
+
EXPOSE 7860
|
| 23 |
+
|
| 24 |
+
# Run FastAPI server
|
| 25 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# ✅ Create writable model cache directory
|
| 9 |
+
os.makedirs("/app/model_cache", exist_ok=True)
|
| 10 |
+
os.environ["HF_HOME"] = "/app/model_cache"
|
| 11 |
+
os.environ["TRANSFORMERS_CACHE"] = "/app/model_cache"
|
| 12 |
+
os.environ["HF_DATASETS_CACHE"] = "/app/model_cache"
|
| 13 |
+
os.environ["HF_METRICS_CACHE"] = "/app/model_cache"
|
| 14 |
+
|
| 15 |
+
MODEL_REPO = "kar137/sambodhan-urgency-classifier"
|
| 16 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 17 |
+
|
| 18 |
+
# ✅ Load model and tokenizer safely
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, cache_dir="/app/model_cache")
|
| 20 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_REPO, cache_dir="/app/model_cache")
|
| 21 |
+
|
| 22 |
+
classifier = pipeline(
|
| 23 |
+
"text-classification",
|
| 24 |
+
model=model,
|
| 25 |
+
tokenizer=tokenizer,
|
| 26 |
+
device=device,
|
| 27 |
+
return_all_scores=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
LABELS = ["NORMAL", "URGENT", "HIGHLY URGENT"]
|
| 31 |
+
|
| 32 |
+
app = FastAPI(title="Sambodhan Urgency Classifier API", version="2.0.3")
|
| 33 |
+
|
| 34 |
+
class TextInput(BaseModel):
|
| 35 |
+
text: str
|
| 36 |
+
|
| 37 |
+
@app.post("/predict")
|
| 38 |
+
async def predict(input_data: TextInput):
|
| 39 |
+
text = input_data.text.strip()
|
| 40 |
+
if not text:
|
| 41 |
+
return {"error": "Empty input"}
|
| 42 |
+
|
| 43 |
+
results = classifier(text)[0]
|
| 44 |
+
top = max(results, key=lambda x: x["score"])
|
| 45 |
+
return {
|
| 46 |
+
"label": top["label"],
|
| 47 |
+
"confidence": round(top["score"], 4),
|
| 48 |
+
"scores": {r["label"]: round(r["score"], 4) for r in results},
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
@app.get("/")
|
| 52 |
+
def root():
|
| 53 |
+
return {"message": "✅ Sambodhan Urgency Classifier API running successfully!"}
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
huggingface-hub
|
| 6 |
+
protobuf
|
| 7 |
+
sentencepiece
|