Spaces:
Sleeping
Sleeping
Initial deploy of FastAPI model to Hugging Face Spaces
Browse files- Dockerfile +17 -0
- app/label_classes.npy +3 -0
- app/main.py +30 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /code
|
| 5 |
+
|
| 6 |
+
# Install dependencies
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
+
|
| 10 |
+
# Copy app code
|
| 11 |
+
COPY app /code/app
|
| 12 |
+
|
| 13 |
+
# Set environment variables
|
| 14 |
+
ENV TRANSFORMERS_CACHE=/code/cache
|
| 15 |
+
|
| 16 |
+
# Expose port and run
|
| 17 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/label_classes.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eeaed33f5b1679639ec637bca7f6c14857aa10ac84c201269527543e9a1002dd
|
| 3 |
+
size 573
|
app/main.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 4 |
+
from sklearn.preprocessing import LabelEncoder
|
| 5 |
+
import torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
model = BertForSequenceClassification.from_pretrained("JaySenpai/bert-model")
|
| 11 |
+
tokenizer = BertTokenizer.from_pretrained("JaySenpai/bert-model")
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
le = LabelEncoder()
|
| 15 |
+
le.classes_ = np.load(r"C:\Users\jayes\Downloads\label_classes.npy", allow_pickle=True)
|
| 16 |
+
|
| 17 |
+
class TextInput(BaseModel):
|
| 18 |
+
text: str
|
| 19 |
+
|
| 20 |
+
@app.post("/predict")
|
| 21 |
+
async def predict(data: TextInput):
|
| 22 |
+
text = data.text # ✅ Dot notation
|
| 23 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 27 |
+
pred_class = torch.argmax(probs, dim=1).item()
|
| 28 |
+
pred_label = le.classes_[pred_class]
|
| 29 |
+
confidence = probs[0][pred_class].item()
|
| 30 |
+
return {"predicted_category": pred_label, "confidence": confidence}
|