Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,93 @@
|
|
| 1 |
-
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@app.get("/")
|
| 17 |
-
def
|
| 18 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
@app.post("/predict")
|
| 21 |
-
def predict(
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import logging
|
| 6 |
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
+
app = FastAPI(
|
| 11 |
+
title="Sentiment Analysis API",
|
| 12 |
+
description="Analyze sentiment of text using BERT fine-tuned on IMDB dataset",
|
| 13 |
+
version="1.0.0",
|
| 14 |
+
docs_url="/docs",
|
| 15 |
+
redoc_url="/redoc"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"],
|
| 21 |
+
allow_credentials=True,
|
| 22 |
+
allow_methods=["*"],
|
| 23 |
+
allow_headers=["*"],
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
logger.info("Loading sentiment analysis model...")
|
| 27 |
+
try:
|
| 28 |
+
classifier = pipeline("sentiment-analysis", model="Adedayo2000/sentiment-model")
|
| 29 |
+
logger.info("Model loaded successfully!")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
logger.error(f"Failed to load model: {e}")
|
| 32 |
+
classifier = None
|
| 33 |
+
|
| 34 |
+
class TextInput(BaseModel):
|
| 35 |
+
text: str
|
| 36 |
+
|
| 37 |
+
class Config:
|
| 38 |
+
json_schema_extra = {
|
| 39 |
+
"example": {
|
| 40 |
+
"text": "I love this product! It's amazing!"
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
|
| 44 |
@app.get("/")
|
| 45 |
+
def home():
|
| 46 |
+
return {
|
| 47 |
+
"message": "Sentiment Analysis API",
|
| 48 |
+
"status": "live",
|
| 49 |
+
"model": "Adedayo2000/sentiment-model",
|
| 50 |
+
"endpoints": {
|
| 51 |
+
"GET /": "API information",
|
| 52 |
+
"POST /predict": "Analyze sentiment of text",
|
| 53 |
+
"GET /docs": "Interactive API documentation (Swagger UI)",
|
| 54 |
+
"GET /redoc": "Alternative API documentation (ReDoc)",
|
| 55 |
+
"GET /health": "Health check"
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
@app.get("/health")
|
| 60 |
+
def health_check():
|
| 61 |
+
return {
|
| 62 |
+
"status": "healthy",
|
| 63 |
+
"model_loaded": classifier is not None
|
| 64 |
+
}
|
| 65 |
|
| 66 |
@app.post("/predict")
|
| 67 |
+
def predict(input: TextInput):
|
| 68 |
+
if not input.text or not input.text.strip():
|
| 69 |
+
return {
|
| 70 |
+
"error": "Text cannot be empty",
|
| 71 |
+
"example": {"text": "I love this product!"}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
if classifier is None:
|
| 75 |
+
return {
|
| 76 |
+
"error": "Model not loaded",
|
| 77 |
+
"status": "unavailable"
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
result = classifier(input.text)[0]
|
| 82 |
+
return {
|
| 83 |
+
"text": input.text,
|
| 84 |
+
"sentiment": result["label"],
|
| 85 |
+
"confidence": round(result["score"], 4)
|
| 86 |
+
}
|
| 87 |
+
except Exception as e:
|
| 88 |
+
logger.error(f"Prediction error: {e}")
|
| 89 |
+
return {
|
| 90 |
+
"error": "Prediction failed",
|
| 91 |
+
"details": str(e)
|
| 92 |
+
}
|
| 93 |
+
|