Spaces:
Running
Running
File size: 7,635 Bytes
5c8556d a332565 5c8556d a332565 5c8556d a332565 5c8556d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | # main.py
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import json
import os
import sys
from datetime import datetime
import torch
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
import re
import shap
import numpy as np
from pathlib import Path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Config
MODEL_DIR = "models"
BEST_METRICS_PATH = "models/best_metrics.json"
DRIFT_LOG_PATH = "models/drift_log.json"
RETRAIN_LOG_PATH = "models/retrain_log.json"
app = FastAPI(
title="Sentiment ML System",
description="Production ML system with DistilBERT",
version="2.0.0"
)
FRONTEND_URL = os.environ.get("FRONTEND_URL")
app.add_middleware(
CORSMiddleware,
allow_origins=[
FRONTEND_URL
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Load model
print("Loading DistilBERT model...")
tokenizer = DistilBertTokenizer.from_pretrained(MODEL_DIR)
model = DistilBertForSequenceClassification.from_pretrained(MODEL_DIR)
model.eval()
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
print(f"✓ DistilBERT loaded on {device}")
class ReviewRequest(BaseModel):
review: str
class PredictionResponse(BaseModel):
sentiment: str
confidence: float
label: int
timestamp: str
class ExplanationResponse(BaseModel):
sentiment: str
confidence: float
label: int
explanation: list
timestamp: str
def preprocess_text(text):
text = text.lower()
text = re.sub(r"<.*?>", "", text)
text = re.sub(r"[^a-z0-9\s]", "", text)
return text.strip()
@app.get("/")
def root():
return {"status": "running", "message": "Sentiment ML System - DistilBERT"}
@app.post("/predict", response_model=PredictionResponse)
def predict(request: ReviewRequest):
if not request.review.strip():
raise HTTPException(status_code=400, detail="Review text cannot be empty")
try:
review = preprocess_text(request.review)
inputs = tokenizer(
review,
return_tensors="pt",
truncation=True,
max_length=256,
padding="max_length"
)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
probabilities = torch.softmax(logits, dim=-1)
label = int(torch.argmax(probabilities, dim=-1).item())
confidence = float(probabilities[0][label].item())
sentiment = "Positive" if label == 1 else "Negative"
return PredictionResponse(
sentiment=sentiment,
confidence=round(confidence, 4),
label=label,
timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/metrics")
def get_metrics():
response = {}
if os.path.exists(BEST_METRICS_PATH):
with open(BEST_METRICS_PATH, "r") as f:
response["best_model"] = json.load(f)
else:
response["best_model"] = None
if os.path.exists(DRIFT_LOG_PATH):
with open(DRIFT_LOG_PATH, "r") as f:
response["drift_log"] = json.load(f)
else:
response["drift_log"] = []
if os.path.exists(RETRAIN_LOG_PATH):
with open(RETRAIN_LOG_PATH, "r") as f:
response["retrain_log"] = json.load(f)
else:
response["retrain_log"] = []
return response
@app.get("/health")
def health():
return {
"status": "healthy",
"model": "DistilBERT",
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
@app.post("/explain", response_model=ExplanationResponse)
def explain(request: ReviewRequest):
if not request.review.strip():
raise HTTPException(status_code=400, detail="Review text cannot be empty")
try:
review = preprocess_text(request.review)
# Get prediction first
inputs = tokenizer(
review,
return_tensors="pt",
truncation=True,
max_length=256,
padding="max_length",
return_offsets_mapping=True
)
offset_mapping = inputs.pop("offset_mapping")[0]
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
probabilities = torch.softmax(logits, dim=-1)
label = int(torch.argmax(probabilities, dim=-1).item())
confidence = float(probabilities[0][label].item())
sentiment = "Positive" if label == 1 else "Negative"
# SHAP explanation
def model_predict(texts):
"""Wrapper for SHAP"""
all_probs = []
for text in texts:
text_clean = preprocess_text(text)
inputs = tokenizer(
text_clean,
return_tensors="pt",
truncation=True,
max_length=256,
padding="max_length"
)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
probs = torch.softmax(outputs.logits, dim=-1).cpu().numpy()[0]
all_probs.append(probs)
return np.array(all_probs)
# Create explainer
explainer = shap.Explainer(model_predict, tokenizer)
# Get SHAP values
shap_values = explainer([review])
# Extract word impacts for the predicted class
tokens = tokenizer.tokenize(review)
token_impacts = shap_values.values[0, :, label]
# Map tokens back to words
word_impacts = []
current_word = ""
current_impact = 0.0
for i, (token, impact) in enumerate(zip(tokens, token_impacts)):
if token.startswith("##"):
# Continuation of previous word
current_word += token[2:]
current_impact += impact
else:
# New word
if current_word:
word_impacts.append({
"word": current_word,
"impact": round(float(current_impact), 4)
})
current_word = token
current_impact = impact
# Add last word
if current_word:
word_impacts.append({
"word": current_word,
"impact": round(float(current_impact), 4)
})
# Filter out special tokens and very low impacts
word_impacts = [
w for w in word_impacts
if w["word"] not in ["[CLS]", "[SEP]", "[PAD]"] and abs(w["impact"]) > 0.01
]
return ExplanationResponse(
sentiment=sentiment,
confidence=round(confidence, 4),
label=label,
explanation=word_impacts,
timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
|