Spaces:
Sleeping
Sleeping
File size: 3,590 Bytes
7dee534 | 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 | import torch
from transformers import BertForSequenceClassification, BertTokenizer
from huggingface_hub import hf_hub_download
import numpy as np
from sklearn.preprocessing import LabelEncoder
import re
from fastapi import FastAPI
from pydantic import BaseModel
REPO_ID = "waseem11/severity_model"
device = torch.device("cpu")
app = FastAPI()
# ------------------------
# Preprocessing
# ------------------------
def clean_text(text):
text = str(text).replace('\n', ' ').replace('\r', ' ')
return re.sub(r'\s+', ' ', text).strip()
def normalize_urdu(text):
text = re.sub(r'[آأإا]', 'ا', text)
text = re.sub(r'[يى]', 'ی', text)
text = re.sub(r'[ةۀ]', 'ہ', text)
text = re.sub(r'[\u064B-\u0652]', '', text)
return text
def preprocess(text):
return normalize_urdu(clean_text(text))
def map_severity(label, confidence):
if label == 'None':
return 'Mild'
elif label == 'Mild':
return 'Moderate' if confidence >= 0.5 else 'Mild'
elif label == 'Severe':
return 'Severe'
return 'Mild'
# ------------------------
# Load Models (startup par sirf ek dafa load hon ge)
# ------------------------
print("Loading tokenizer...")
tokenizer = BertTokenizer.from_pretrained("bert-base-multilingual-cased")
sev_encoder = LabelEncoder()
sev_encoder.classes_ = np.array(['None', 'Mild', 'Severe'])
bin_encoder = LabelEncoder()
bin_encoder.classes_ = np.array(['Not Depressed', 'Depressed'])
print("Loading binary model...")
binary_path = hf_hub_download(REPO_ID, "binary_model.pt")
binary_model = BertForSequenceClassification.from_pretrained(
"bert-base-multilingual-cased", num_labels=2)
binary_model.load_state_dict(torch.load(binary_path, map_location=device), strict=False)
binary_model.eval()
print("Loading severity model...")
severity_path = hf_hub_download(REPO_ID, "severity_model.pt")
severity_model = BertForSequenceClassification.from_pretrained(
"bert-base-multilingual-cased", num_labels=3)
severity_model.load_state_dict(torch.load(severity_path, map_location=device), strict=False)
severity_model.eval()
print("All models ready ✓")
# ------------------------
# Request Body
# ------------------------
class TextInput(BaseModel):
text: str
# ------------------------
# API Endpoint
# ------------------------
@app.post("/predict")
def predict_api(data: TextInput):
text = data.text
if not text.strip():
return {"error": "Text khali hai"}
processed = preprocess(text)
inputs = tokenizer(processed, return_tensors="pt",
truncation=True, padding=True, max_length=128)
with torch.no_grad():
bin_out = binary_model(**inputs)
bin_pred = torch.argmax(bin_out.logits, dim=1).item()
bin_conf = torch.softmax(bin_out.logits, dim=1).max().item()
bin_label = bin_encoder.inverse_transform([bin_pred])[0]
if bin_label == 'Not Depressed':
return {
"status": "Not Depressed",
"confidence": f"{bin_conf:.1%}",
"severity": "None"
}
with torch.no_grad():
sev_out = severity_model(**inputs)
sev_probs = torch.softmax(sev_out.logits, dim=1)
sev_pred = torch.argmax(sev_probs, dim=1).item()
sev_conf = sev_probs[0][sev_pred].item()
sev_label = sev_encoder.inverse_transform([sev_pred])[0]
sev_mapped = map_severity(sev_label, sev_conf)
return {
"status": "Depressed",
"confidence": f"{bin_conf:.1%}",
"severity": sev_mapped,
"severity_confidence": f"{sev_conf:.1%}"
} |