🔧 Fix Gradio compatibility - remove deprecated @gr .cache decorator
Browse files
app.py
CHANGED
|
@@ -13,20 +13,23 @@ import pandas as pd
|
|
| 13 |
import plotly.express as px
|
| 14 |
import plotly.graph_objects as go
|
| 15 |
|
| 16 |
-
# Initialize the model
|
| 17 |
-
|
|
|
|
| 18 |
def load_model():
|
| 19 |
"""Load the emotion classification model"""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Emotion mappings
|
| 32 |
EMOTION_EMOJIS = {
|
|
@@ -51,14 +54,14 @@ def classify_emotion(text):
|
|
| 51 |
"""Classify emotion for a single text"""
|
| 52 |
if not text.strip():
|
| 53 |
return "Please enter some text to analyze.", None, None
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
if
|
| 57 |
return "Model failed to load. Please try again.", None, None
|
| 58 |
|
| 59 |
try:
|
| 60 |
# Get prediction
|
| 61 |
-
result =
|
| 62 |
emotion = result[0]['label'].lower()
|
| 63 |
confidence = result[0]['score']
|
| 64 |
|
|
@@ -83,10 +86,10 @@ def classify_emotion(text):
|
|
| 83 |
scores = []
|
| 84 |
|
| 85 |
# Get scores for all emotions (if available)
|
| 86 |
-
|
| 87 |
-
all_results =
|
| 88 |
scores = [next((r['score'] for r in all_results if r['label'].lower() == e), 0) for e in emotions]
|
| 89 |
-
|
| 90 |
# If only top prediction available, set others to 0
|
| 91 |
scores = [confidence if e == emotion else 0 for e in emotions]
|
| 92 |
|
|
@@ -134,9 +137,9 @@ def classify_batch(text_input):
|
|
| 134 |
"""Classify emotions for multiple texts"""
|
| 135 |
if not text_input.strip():
|
| 136 |
return "Please enter texts to analyze (one per line).", None
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
if
|
| 140 |
return "Model failed to load. Please try again.", None
|
| 141 |
|
| 142 |
try:
|
|
@@ -149,7 +152,7 @@ def classify_batch(text_input):
|
|
| 149 |
# Classify all texts
|
| 150 |
results = []
|
| 151 |
for text in texts:
|
| 152 |
-
result =
|
| 153 |
emotion = result[0]['label'].lower()
|
| 154 |
confidence = result[0]['score']
|
| 155 |
emoji = EMOTION_EMOJIS.get(emotion, '🤔')
|
|
@@ -198,8 +201,8 @@ def classify_batch(text_input):
|
|
| 198 |
|
| 199 |
def run_predefined_tests():
|
| 200 |
"""Run predefined test cases"""
|
| 201 |
-
|
| 202 |
-
if
|
| 203 |
return "Model failed to load. Please try again.", None
|
| 204 |
|
| 205 |
# Predefined test cases
|
|
@@ -228,7 +231,7 @@ def run_predefined_tests():
|
|
| 228 |
correct = 0
|
| 229 |
|
| 230 |
for text, expected, flag in test_cases:
|
| 231 |
-
result =
|
| 232 |
predicted = result[0]['label'].lower()
|
| 233 |
confidence = result[0]['score']
|
| 234 |
|
|
|
|
| 13 |
import plotly.express as px
|
| 14 |
import plotly.graph_objects as go
|
| 15 |
|
| 16 |
+
# Initialize the model globally
|
| 17 |
+
classifier = None
|
| 18 |
+
|
| 19 |
def load_model():
|
| 20 |
"""Load the emotion classification model"""
|
| 21 |
+
global classifier
|
| 22 |
+
if classifier is None:
|
| 23 |
+
try:
|
| 24 |
+
classifier = pipeline(
|
| 25 |
+
"text-classification",
|
| 26 |
+
model="rmtariq/multilingual-emotion-classifier",
|
| 27 |
+
device=0 if torch.cuda.is_available() else -1
|
| 28 |
+
)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error loading model: {e}")
|
| 31 |
+
return None
|
| 32 |
+
return classifier
|
| 33 |
|
| 34 |
# Emotion mappings
|
| 35 |
EMOTION_EMOJIS = {
|
|
|
|
| 54 |
"""Classify emotion for a single text"""
|
| 55 |
if not text.strip():
|
| 56 |
return "Please enter some text to analyze.", None, None
|
| 57 |
+
|
| 58 |
+
model = load_model()
|
| 59 |
+
if model is None:
|
| 60 |
return "Model failed to load. Please try again.", None, None
|
| 61 |
|
| 62 |
try:
|
| 63 |
# Get prediction
|
| 64 |
+
result = model(text)
|
| 65 |
emotion = result[0]['label'].lower()
|
| 66 |
confidence = result[0]['score']
|
| 67 |
|
|
|
|
| 86 |
scores = []
|
| 87 |
|
| 88 |
# Get scores for all emotions (if available)
|
| 89 |
+
try:
|
| 90 |
+
all_results = model(text, return_all_scores=True)
|
| 91 |
scores = [next((r['score'] for r in all_results if r['label'].lower() == e), 0) for e in emotions]
|
| 92 |
+
except:
|
| 93 |
# If only top prediction available, set others to 0
|
| 94 |
scores = [confidence if e == emotion else 0 for e in emotions]
|
| 95 |
|
|
|
|
| 137 |
"""Classify emotions for multiple texts"""
|
| 138 |
if not text_input.strip():
|
| 139 |
return "Please enter texts to analyze (one per line).", None
|
| 140 |
+
|
| 141 |
+
model = load_model()
|
| 142 |
+
if model is None:
|
| 143 |
return "Model failed to load. Please try again.", None
|
| 144 |
|
| 145 |
try:
|
|
|
|
| 152 |
# Classify all texts
|
| 153 |
results = []
|
| 154 |
for text in texts:
|
| 155 |
+
result = model(text)
|
| 156 |
emotion = result[0]['label'].lower()
|
| 157 |
confidence = result[0]['score']
|
| 158 |
emoji = EMOTION_EMOJIS.get(emotion, '🤔')
|
|
|
|
| 201 |
|
| 202 |
def run_predefined_tests():
|
| 203 |
"""Run predefined test cases"""
|
| 204 |
+
model = load_model()
|
| 205 |
+
if model is None:
|
| 206 |
return "Model failed to load. Please try again.", None
|
| 207 |
|
| 208 |
# Predefined test cases
|
|
|
|
| 231 |
correct = 0
|
| 232 |
|
| 233 |
for text, expected, flag in test_cases:
|
| 234 |
+
result = model(text)
|
| 235 |
predicted = result[0]['label'].lower()
|
| 236 |
confidence = result[0]['score']
|
| 237 |
|