Update fastapi_app/app.py
Browse files- fastapi_app/app.py +110 -112
fastapi_app/app.py
CHANGED
|
@@ -1,112 +1,110 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import logging
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from fastapi import
|
| 6 |
-
from fastapi.
|
| 7 |
-
from fastapi.
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
logging.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
model
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
logger.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
"
|
| 86 |
-
"
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
"
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import logging
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
+
from fastapi import FastAPI, Request, Form
|
| 5 |
+
from fastapi.responses import HTMLResponse
|
| 6 |
+
from fastapi.templating import Jinja2Templates
|
| 7 |
+
from fastapi.staticfiles import StaticFiles
|
| 8 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 9 |
+
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
model = None
|
| 14 |
+
tokenizer = None
|
| 15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@asynccontextmanager
|
| 19 |
+
async def lifespan(app: FastAPI):
|
| 20 |
+
"""Load model on startup and cleanup on shutdown"""
|
| 21 |
+
global model, tokenizer
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
model_id = "codeby-hp/FinetuneTinybert-SentimentClassification"
|
| 25 |
+
|
| 26 |
+
logger.info(f"Loading tokenizer from {model_id}...")
|
| 27 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 28 |
+
|
| 29 |
+
logger.info(f"Loading model from {model_id}...")
|
| 30 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 31 |
+
model.to(device)
|
| 32 |
+
model.eval()
|
| 33 |
+
|
| 34 |
+
logger.info(f"Model loaded successfully on {device}")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
logger.error(f"Error loading model: {e}")
|
| 37 |
+
raise
|
| 38 |
+
|
| 39 |
+
yield
|
| 40 |
+
|
| 41 |
+
logger.info("Shutting down...")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
app = FastAPI(title="Sentiment Analysis API", lifespan=lifespan)
|
| 45 |
+
|
| 46 |
+
templates = Jinja2Templates(directory="templates")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.get("/", response_class=HTMLResponse)
|
| 50 |
+
async def home(request: Request):
|
| 51 |
+
"""Render the home page"""
|
| 52 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.post("/predict")
|
| 56 |
+
async def predict(request: Request, text: str = Form(...)):
|
| 57 |
+
"""Predict sentiment for the given text"""
|
| 58 |
+
if not text.strip():
|
| 59 |
+
return templates.TemplateResponse(
|
| 60 |
+
"index.html",
|
| 61 |
+
{"request": request, "error": "Please enter some text to analyze"},
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
inputs = tokenizer(
|
| 66 |
+
text, return_tensors="pt", truncation=True, max_length=512, padding=True
|
| 67 |
+
)
|
| 68 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 69 |
+
|
| 70 |
+
with torch.no_grad():
|
| 71 |
+
outputs = model(**inputs)
|
| 72 |
+
logits = outputs.logits
|
| 73 |
+
probabilities = torch.nn.functional.softmax(logits, dim=-1)
|
| 74 |
+
predicted_class = torch.argmax(probabilities, dim=-1).item()
|
| 75 |
+
confidence = probabilities[0][predicted_class].item()
|
| 76 |
+
|
| 77 |
+
sentiment_map = {0: "Negative", 1: "Positive"}
|
| 78 |
+
sentiment = sentiment_map.get(predicted_class, "Unknown")
|
| 79 |
+
|
| 80 |
+
return templates.TemplateResponse(
|
| 81 |
+
"index.html",
|
| 82 |
+
{
|
| 83 |
+
"request": request,
|
| 84 |
+
"text": text,
|
| 85 |
+
"sentiment": sentiment,
|
| 86 |
+
"confidence": round(confidence * 100, 2),
|
| 87 |
+
},
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logger.error(f"Prediction error: {e}")
|
| 92 |
+
return templates.TemplateResponse(
|
| 93 |
+
"index.html", {"request": request, "error": f"An error occurred: {str(e)}"}
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@app.get("/health")
|
| 98 |
+
async def health_check():
|
| 99 |
+
"""Health check endpoint"""
|
| 100 |
+
return {
|
| 101 |
+
"status": "healthy",
|
| 102 |
+
"model_loaded": model is not None,
|
| 103 |
+
"device": str(device),
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
import uvicorn
|
| 109 |
+
|
| 110 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|