Spaces:
Sleeping
Sleeping
runtime error
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ logger.info("Initializing emotion classification pipeline...")
|
|
| 18 |
classifier = pipeline(
|
| 19 |
"text-classification",
|
| 20 |
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
| 21 |
-
|
| 22 |
)
|
| 23 |
logger.info("Pipeline initialized successfully")
|
| 24 |
|
|
@@ -29,11 +29,12 @@ def predict_emotion(text):
|
|
| 29 |
|
| 30 |
if not text:
|
| 31 |
logger.warning("Empty text received")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
# Get predictions
|
| 39 |
logger.info("Running prediction...")
|
|
@@ -44,18 +45,20 @@ def predict_emotion(text):
|
|
| 44 |
sorted_predictions = sorted(predictions, key=lambda x: x['score'], reverse=True)
|
| 45 |
|
| 46 |
# Create DataFrame with the correct format
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
'score': [pred['score'] for pred in sorted_predictions]
|
| 50 |
-
}
|
| 51 |
-
df = pd.DataFrame(data)
|
| 52 |
logger.info(f"Processed scores:\n{df}")
|
| 53 |
|
| 54 |
-
return df
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
logger.error(f"Error in prediction: {str(e)}")
|
| 58 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Create the Gradio interface
|
| 61 |
demo = gr.Interface(
|
|
@@ -66,12 +69,16 @@ demo = gr.Interface(
|
|
| 66 |
lines=4
|
| 67 |
),
|
| 68 |
outputs=gr.BarPlot(
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
title="Emotion Probabilities",
|
| 72 |
-
height=400,
|
| 73 |
color="#2563eb",
|
| 74 |
-
|
|
|
|
| 75 |
),
|
| 76 |
title="Emotion Detection with DistilBERT",
|
| 77 |
description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
|
|
|
|
| 18 |
classifier = pipeline(
|
| 19 |
"text-classification",
|
| 20 |
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
| 21 |
+
top_k=None # Return all scores
|
| 22 |
)
|
| 23 |
logger.info("Pipeline initialized successfully")
|
| 24 |
|
|
|
|
| 29 |
|
| 30 |
if not text:
|
| 31 |
logger.warning("Empty text received")
|
| 32 |
+
return gr.BarPlot.update(
|
| 33 |
+
value=pd.DataFrame({
|
| 34 |
+
'label': ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise'],
|
| 35 |
+
'score': [0, 0, 0, 0, 0, 0]
|
| 36 |
+
})
|
| 37 |
+
)
|
| 38 |
|
| 39 |
# Get predictions
|
| 40 |
logger.info("Running prediction...")
|
|
|
|
| 45 |
sorted_predictions = sorted(predictions, key=lambda x: x['score'], reverse=True)
|
| 46 |
|
| 47 |
# Create DataFrame with the correct format
|
| 48 |
+
df = pd.DataFrame(sorted_predictions)
|
| 49 |
+
df.columns = ['label', 'score'] # Rename columns to match expected format
|
|
|
|
|
|
|
|
|
|
| 50 |
logger.info(f"Processed scores:\n{df}")
|
| 51 |
|
| 52 |
+
return gr.BarPlot.update(value=df)
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
logger.error(f"Error in prediction: {str(e)}")
|
| 56 |
+
return gr.BarPlot.update(
|
| 57 |
+
value=pd.DataFrame({
|
| 58 |
+
'label': ['error'],
|
| 59 |
+
'score': [1.0]
|
| 60 |
+
})
|
| 61 |
+
)
|
| 62 |
|
| 63 |
# Create the Gradio interface
|
| 64 |
demo = gr.Interface(
|
|
|
|
| 69 |
lines=4
|
| 70 |
),
|
| 71 |
outputs=gr.BarPlot(
|
| 72 |
+
value=pd.DataFrame({
|
| 73 |
+
'label': ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise'],
|
| 74 |
+
'score': [0, 0, 0, 0, 0, 0]
|
| 75 |
+
}),
|
| 76 |
+
x="label",
|
| 77 |
+
y="score",
|
| 78 |
title="Emotion Probabilities",
|
|
|
|
| 79 |
color="#2563eb",
|
| 80 |
+
height=400,
|
| 81 |
+
vertical=True
|
| 82 |
),
|
| 83 |
title="Emotion Detection with DistilBERT",
|
| 84 |
description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
|