dnzblgn commited on
Commit
69d8dcf
·
verified ·
1 Parent(s): 66c3528

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -83,18 +83,27 @@ def run_sentiment_with_selected_model(text, model_name):
83
  outputs = model(**inputs)
84
 
85
  logits = outputs.logits
86
- probs = torch.nn.functional.softmax(logits, dim=-1)
87
  pred = torch.argmax(probs, dim=-1).item()
88
 
89
- # Get label from model config if available
90
- if model.config.id2label:
91
- label = model.config.id2label[pred]
 
 
 
 
 
 
 
92
  else:
93
- label = "Positive" if pred == 1 else "Negative"
94
 
95
  emoji = "✅" if "positive" in label.lower() else "❌" if "negative" in label.lower() else "⚠️"
96
- return f"{emoji} '{text}' -> {label}"
97
-
 
 
98
 
99
  # ---------------- Gradio UI ----------------
100
  background_css = """
 
83
  outputs = model(**inputs)
84
 
85
  logits = outputs.logits
86
+ probs = F.softmax(logits, dim=-1)
87
  pred = torch.argmax(probs, dim=-1).item()
88
 
89
+ # Custom label mapping
90
+ label_map = {
91
+ "assemblyai/bert-large-uncased-sst2": {0: "Negative", 1: "Positive"},
92
+ "sohan-ai/sentiment-analysis-model-amazon-reviews": {0: "Negative", 1: "Positive"},
93
+ }
94
+
95
+ if model_name in label_map:
96
+ label = label_map[model_name][pred]
97
+ elif model.config.id2label:
98
+ label = model.config.id2label.get(pred, f"LABEL_{pred}")
99
  else:
100
+ label = f"LABEL_{pred}"
101
 
102
  emoji = "✅" if "positive" in label.lower() else "❌" if "negative" in label.lower() else "⚠️"
103
+
104
+ # Add confidence score
105
+ confidence = probs[0][pred].item() * 100
106
+ return f"{emoji} '{text}' -> {label} ({confidence:.1f}%)"
107
 
108
  # ---------------- Gradio UI ----------------
109
  background_css = """