Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,35 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
import torch
|
| 5 |
import torch.nn.functional as F
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
MODEL_DIR = "./"
|
| 9 |
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class TextRequest(BaseModel):
|
| 18 |
text: str
|
| 19 |
|
|
|
|
| 20 |
@app.post("/predict")
|
| 21 |
def predict(request: TextRequest):
|
| 22 |
-
inputs = tokenizer(request.text, truncation=True, padding=True, max_length=512
|
| 23 |
with torch.no_grad():
|
| 24 |
logits = model(**inputs).logits
|
| 25 |
probs = F.softmax(logits, dim=-1).squeeze()
|
|
@@ -27,6 +37,8 @@ def predict(request: TextRequest):
|
|
| 27 |
topk = torch.topk(probs, k=5)
|
| 28 |
indices = topk.indices.tolist()
|
| 29 |
scores = topk.values.tolist()
|
|
|
|
|
|
|
| 30 |
id2label = model.config.id2label if hasattr(model.config, "id2label") else {i: f"Label {i}" for i in range(len(probs))}
|
| 31 |
|
| 32 |
results = [{"label": id2label[idx], "score": f"{score:.4f}"} for idx, score in zip(indices, scores)]
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
import torch
|
| 6 |
import torch.nn.functional as F
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# Load model and tokenizer from local files
|
| 10 |
MODEL_DIR = "./"
|
| 11 |
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 13 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)
|
| 14 |
model.eval()
|
| 15 |
|
| 16 |
+
# FastAPI app
|
| 17 |
app = FastAPI()
|
| 18 |
|
| 19 |
+
# Serve index.html when visiting root
|
| 20 |
+
@app.get("/", response_class=HTMLResponse)
|
| 21 |
+
def serve_index():
|
| 22 |
+
with open("index.html", "r") as f:
|
| 23 |
+
return f.read()
|
| 24 |
+
|
| 25 |
+
# Request body model
|
| 26 |
class TextRequest(BaseModel):
|
| 27 |
text: str
|
| 28 |
|
| 29 |
+
# Predict endpoint
|
| 30 |
@app.post("/predict")
|
| 31 |
def predict(request: TextRequest):
|
| 32 |
+
inputs = tokenizer(request.text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 33 |
with torch.no_grad():
|
| 34 |
logits = model(**inputs).logits
|
| 35 |
probs = F.softmax(logits, dim=-1).squeeze()
|
|
|
|
| 37 |
topk = torch.topk(probs, k=5)
|
| 38 |
indices = topk.indices.tolist()
|
| 39 |
scores = topk.values.tolist()
|
| 40 |
+
|
| 41 |
+
# Use id2label mapping if available
|
| 42 |
id2label = model.config.id2label if hasattr(model.config, "id2label") else {i: f"Label {i}" for i in range(len(probs))}
|
| 43 |
|
| 44 |
results = [{"label": id2label[idx], "score": f"{score:.4f}"} for idx, score in zip(indices, scores)]
|