Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import io
|
5 |
+
import json
|
6 |
+
|
7 |
+
# Load the emotion classification model
|
8 |
+
@st.cache_resource
|
9 |
+
def load_model():
|
10 |
+
return pipeline(
|
11 |
+
"text-classification",
|
12 |
+
model="j-hartmann/emotion-english-distilroberta-base",
|
13 |
+
return_all_scores=True
|
14 |
+
)
|
15 |
+
|
16 |
+
emotion_classifier = load_model()
|
17 |
+
|
18 |
+
# Define a function to predict emotions and generate a bar chart
|
19 |
+
def predict_emotion_with_chart(text):
|
20 |
+
if not text.strip():
|
21 |
+
return None, None
|
22 |
+
|
23 |
+
# Get predictions
|
24 |
+
results = emotion_classifier(text)
|
25 |
+
emotions = {result['label']: round(result['score'], 2) for result in results[0]}
|
26 |
+
sorted_emotions = dict(sorted(emotions.items(), key=lambda item: item[1], reverse=True))
|
27 |
+
|
28 |
+
# Create a bar chart
|
29 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
30 |
+
ax.bar(sorted_emotions.keys(), sorted_emotions.values(), color="skyblue")
|
31 |
+
ax.set_title("Emotion Scores")
|
32 |
+
ax.set_ylabel("Confidence Score")
|
33 |
+
ax.set_ylim(0, 1)
|
34 |
+
ax.set_xticklabels(sorted_emotions.keys(), rotation=45, ha="right")
|
35 |
+
plt.tight_layout()
|
36 |
+
|
37 |
+
return sorted_emotions, fig
|
38 |
+
|
39 |
+
# Function to generate JSON result
|
40 |
+
def generate_json(text):
|
41 |
+
results = emotion_classifier(text)
|
42 |
+
emotions = {result['label']: round(result['score'], 2) for result in results[0]}
|
43 |
+
return json.dumps(emotions, indent=2)
|
44 |
+
|
45 |
+
# Streamlit UI
|
46 |
+
st.title("🌟 Enhanced Emotion Detection App")
|
47 |
+
st.markdown("Analyze the emotions in a sentence and visualize them. Enter text to detect emotions, see a bar chart of scores, and download the results as a JSON file.")
|
48 |
+
|
49 |
+
# Input text
|
50 |
+
text_input = st.text_area("Enter text to analyze emotions:", "")
|
51 |
+
|
52 |
+
if st.button("Analyze Emotions"):
|
53 |
+
if text_input.strip():
|
54 |
+
emotion_scores, chart = predict_emotion_with_chart(text_input)
|
55 |
+
if emotion_scores:
|
56 |
+
st.subheader("Emotion Scores")
|
57 |
+
st.json(emotion_scores)
|
58 |
+
st.subheader("Emotion Chart")
|
59 |
+
st.pyplot(chart)
|
60 |
+
else:
|
61 |
+
st.error("No emotions detected. Please enter a valid text.")
|
62 |
+
else:
|
63 |
+
st.warning("Please enter some text.")
|
64 |
+
|
65 |
+
# Download JSON results
|
66 |
+
if text_input.strip():
|
67 |
+
json_data = generate_json(text_input)
|
68 |
+
st.download_button(label="Download Results as JSON", data=json_data, file_name="emotion_results.json", mime="application/json")
|