Spaces:
Sleeping
Sleeping
working on the bar
Browse files
app.py
CHANGED
|
@@ -29,30 +29,27 @@ def predict_emotion(text):
|
|
| 29 |
|
| 30 |
if not text:
|
| 31 |
logger.warning("Empty text received")
|
| 32 |
-
return
|
| 33 |
-
'emotion': ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise'],
|
| 34 |
-
'score': [0, 0, 0, 0, 0, 0]
|
| 35 |
-
})
|
| 36 |
|
| 37 |
# Get predictions
|
| 38 |
logger.info("Running prediction...")
|
| 39 |
predictions = classifier(text)[0]
|
| 40 |
logger.info(f"Raw predictions: {predictions}")
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
df.columns = ['emotion', 'score']
|
| 45 |
-
df = df.sort_values('score', ascending=True)
|
| 46 |
-
logger.info(f"Processed scores as DataFrame: {df.to_dict()}")
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
logger.error(f"Error in prediction: {str(e)}")
|
| 52 |
-
return
|
| 53 |
-
'emotion': ['error'],
|
| 54 |
-
'score': [1.0]
|
| 55 |
-
})
|
| 56 |
|
| 57 |
# Create the Gradio interface
|
| 58 |
demo = gr.Interface(
|
|
@@ -63,21 +60,20 @@ demo = gr.Interface(
|
|
| 63 |
lines=4
|
| 64 |
),
|
| 65 |
outputs=gr.BarPlot(
|
| 66 |
-
title="Emotion Probabilities",
|
| 67 |
-
x="emotion",
|
| 68 |
y="score",
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
height=400
|
|
|
|
| 72 |
),
|
| 73 |
title="Emotion Detection with DistilBERT",
|
| 74 |
description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
|
| 75 |
examples=[
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
],
|
| 82 |
allow_flagging="never"
|
| 83 |
)
|
|
|
|
| 29 |
|
| 30 |
if not text:
|
| 31 |
logger.warning("Empty text received")
|
| 32 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Get predictions
|
| 35 |
logger.info("Running prediction...")
|
| 36 |
predictions = classifier(text)[0]
|
| 37 |
logger.info(f"Raw predictions: {predictions}")
|
| 38 |
|
| 39 |
+
# Sort predictions by score in descending order
|
| 40 |
+
predictions.sort(key=lambda x: x['score'], reverse=True)
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Extract labels and scores
|
| 43 |
+
labels = [p['label'] for p in predictions]
|
| 44 |
+
scores = [p['score'] for p in predictions]
|
| 45 |
+
|
| 46 |
+
logger.info(f"Processed scores: {dict(zip(labels, scores))}")
|
| 47 |
+
|
| 48 |
+
return (labels, scores)
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
logger.error(f"Error in prediction: {str(e)}")
|
| 52 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# Create the Gradio interface
|
| 55 |
demo = gr.Interface(
|
|
|
|
| 60 |
lines=4
|
| 61 |
),
|
| 62 |
outputs=gr.BarPlot(
|
|
|
|
|
|
|
| 63 |
y="score",
|
| 64 |
+
title="Emotion Probabilities",
|
| 65 |
+
tooltip=["emotion", "score"],
|
| 66 |
+
height=400,
|
| 67 |
+
color="#2563eb"
|
| 68 |
),
|
| 69 |
title="Emotion Detection with DistilBERT",
|
| 70 |
description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
|
| 71 |
examples=[
|
| 72 |
+
"I am so happy to see you!",
|
| 73 |
+
"I'm really angry about what happened.",
|
| 74 |
+
"The sunset was absolutely beautiful today.",
|
| 75 |
+
"I'm worried about the upcoming exam.",
|
| 76 |
+
"Fear is the mind-killer. I will face my fear."
|
| 77 |
],
|
| 78 |
allow_flagging="never"
|
| 79 |
)
|