Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ if hf_token:
|
|
| 33 |
|
| 34 |
# Available emotion detection models
|
| 35 |
EMOTION_MODELS = {
|
| 36 |
-
"AnasAlokla/multilingual_go_emotions": "Multilingual Go Emotions (
|
| 37 |
"AnasAlokla/multilingual_go_emotions_V1.1": "Multilingual Go Emotions (V1.1)"
|
| 38 |
}
|
| 39 |
|
|
@@ -100,25 +100,57 @@ def get_ai_response(user_input, emotion_predictions):
|
|
| 100 |
else:
|
| 101 |
return responses
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def main():
|
| 104 |
# Sidebar configurations
|
| 105 |
-
st.sidebar.header("Configuration")
|
| 106 |
|
| 107 |
# Language Selection
|
| 108 |
selected_language = st.sidebar.selectbox(
|
| 109 |
-
"Select Interface Language",
|
| 110 |
list(LANGUAGES.keys()),
|
| 111 |
index=0 # Default to English
|
| 112 |
)
|
| 113 |
|
| 114 |
# Model Selection
|
| 115 |
selected_model_key = st.sidebar.selectbox(
|
| 116 |
-
"Select Emotion Detection Model",
|
| 117 |
list(EMOTION_MODELS.keys()),
|
| 118 |
format_func=lambda x: EMOTION_MODELS[x],
|
| 119 |
index=0 # Default to first model
|
| 120 |
)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# Load the selected emotion classifier
|
| 123 |
emotion_classifier = load_emotion_classifier(selected_model_key)
|
| 124 |
|
|
@@ -128,18 +160,21 @@ def main():
|
|
| 128 |
return
|
| 129 |
|
| 130 |
# Display selected model info
|
| 131 |
-
st.sidebar.
|
| 132 |
|
| 133 |
# Display Image
|
| 134 |
st.image('chatBot_image.jpg', channels='RGB')
|
| 135 |
|
| 136 |
# Set page title and header based on selected language
|
| 137 |
st.title(LANGUAGES[selected_language]['title'])
|
|
|
|
| 138 |
|
| 139 |
# Input Text Box
|
| 140 |
-
user_input = st.
|
| 141 |
-
LANGUAGES[selected_language]['input_placeholder'],
|
| 142 |
-
""
|
|
|
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
if user_input:
|
|
@@ -147,10 +182,35 @@ def main():
|
|
| 147 |
with st.spinner("Analyzing emotions..."):
|
| 148 |
emotion_predictions = emotion_classifier(user_input)
|
| 149 |
|
| 150 |
-
# Display
|
|
|
|
|
|
|
|
|
|
| 151 |
st.subheader(LANGUAGES[selected_language]['emotions_header'])
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
# Get AI Response
|
| 156 |
with st.spinner("Generating response..."):
|
|
|
|
| 33 |
|
| 34 |
# Available emotion detection models
|
| 35 |
EMOTION_MODELS = {
|
| 36 |
+
"AnasAlokla/multilingual_go_emotions": "Multilingual Go Emotions (Original)",
|
| 37 |
"AnasAlokla/multilingual_go_emotions_V1.1": "Multilingual Go Emotions (V1.1)"
|
| 38 |
}
|
| 39 |
|
|
|
|
| 100 |
else:
|
| 101 |
return responses
|
| 102 |
|
| 103 |
+
def display_top_predictions(emotion_predictions, selected_language, num_predictions=10):
|
| 104 |
+
"""Display top emotion predictions in sidebar."""
|
| 105 |
+
# Sort predictions by score in descending order
|
| 106 |
+
sorted_predictions = sorted(emotion_predictions, key=lambda x: x['score'], reverse=True)
|
| 107 |
+
|
| 108 |
+
# Take top N predictions
|
| 109 |
+
top_predictions = sorted_predictions[:num_predictions]
|
| 110 |
+
|
| 111 |
+
# Display in sidebar
|
| 112 |
+
st.sidebar.markdown("---")
|
| 113 |
+
st.sidebar.subheader("π― Top Emotion Predictions")
|
| 114 |
+
|
| 115 |
+
for i, prediction in enumerate(top_predictions, 1):
|
| 116 |
+
emotion = prediction['label']
|
| 117 |
+
score = prediction['score']
|
| 118 |
+
percentage = score * 100
|
| 119 |
+
|
| 120 |
+
# Create a progress bar for visual representation
|
| 121 |
+
st.sidebar.markdown(f"**{i}. {emotion.title()}**")
|
| 122 |
+
st.sidebar.progress(score)
|
| 123 |
+
st.sidebar.markdown(f"Score: {percentage:.1f}%")
|
| 124 |
+
st.sidebar.markdown("---")
|
| 125 |
+
|
| 126 |
def main():
|
| 127 |
# Sidebar configurations
|
| 128 |
+
st.sidebar.header("βοΈ Configuration")
|
| 129 |
|
| 130 |
# Language Selection
|
| 131 |
selected_language = st.sidebar.selectbox(
|
| 132 |
+
"π Select Interface Language",
|
| 133 |
list(LANGUAGES.keys()),
|
| 134 |
index=0 # Default to English
|
| 135 |
)
|
| 136 |
|
| 137 |
# Model Selection
|
| 138 |
selected_model_key = st.sidebar.selectbox(
|
| 139 |
+
"π€ Select Emotion Detection Model",
|
| 140 |
list(EMOTION_MODELS.keys()),
|
| 141 |
format_func=lambda x: EMOTION_MODELS[x],
|
| 142 |
index=0 # Default to first model
|
| 143 |
)
|
| 144 |
|
| 145 |
+
# Number of predictions to show in sidebar
|
| 146 |
+
num_predictions = st.sidebar.slider(
|
| 147 |
+
"π Number of predictions to show",
|
| 148 |
+
min_value=5,
|
| 149 |
+
max_value=15,
|
| 150 |
+
value=10,
|
| 151 |
+
step=1
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
# Load the selected emotion classifier
|
| 155 |
emotion_classifier = load_emotion_classifier(selected_model_key)
|
| 156 |
|
|
|
|
| 160 |
return
|
| 161 |
|
| 162 |
# Display selected model info
|
| 163 |
+
st.sidebar.success(f"β
Current Model: {EMOTION_MODELS[selected_model_key]}")
|
| 164 |
|
| 165 |
# Display Image
|
| 166 |
st.image('chatBot_image.jpg', channels='RGB')
|
| 167 |
|
| 168 |
# Set page title and header based on selected language
|
| 169 |
st.title(LANGUAGES[selected_language]['title'])
|
| 170 |
+
st.markdown("### π¬ Enter your text to analyze emotions and get AI response")
|
| 171 |
|
| 172 |
# Input Text Box
|
| 173 |
+
user_input = st.text_area(
|
| 174 |
+
LANGUAGES[selected_language]['input_placeholder'],
|
| 175 |
+
"",
|
| 176 |
+
height=100,
|
| 177 |
+
help="Type your message here to analyze emotions"
|
| 178 |
)
|
| 179 |
|
| 180 |
if user_input:
|
|
|
|
| 182 |
with st.spinner("Analyzing emotions..."):
|
| 183 |
emotion_predictions = emotion_classifier(user_input)
|
| 184 |
|
| 185 |
+
# Display top predictions in sidebar
|
| 186 |
+
display_top_predictions(emotion_predictions, selected_language, num_predictions)
|
| 187 |
+
|
| 188 |
+
# Display Emotions in main area (top 5)
|
| 189 |
st.subheader(LANGUAGES[selected_language]['emotions_header'])
|
| 190 |
+
top_5_emotions = sorted(emotion_predictions, key=lambda x: x['score'], reverse=True)[:5]
|
| 191 |
+
|
| 192 |
+
# Create columns for better display
|
| 193 |
+
col1, col2 = st.columns(2)
|
| 194 |
+
|
| 195 |
+
for i, prediction in enumerate(top_5_emotions):
|
| 196 |
+
emotion = prediction['label']
|
| 197 |
+
score = prediction['score']
|
| 198 |
+
percentage = score * 100
|
| 199 |
+
|
| 200 |
+
if i % 2 == 0:
|
| 201 |
+
with col1:
|
| 202 |
+
st.metric(
|
| 203 |
+
label=emotion.title(),
|
| 204 |
+
value=f"{percentage:.1f}%",
|
| 205 |
+
delta=None
|
| 206 |
+
)
|
| 207 |
+
else:
|
| 208 |
+
with col2:
|
| 209 |
+
st.metric(
|
| 210 |
+
label=emotion.title(),
|
| 211 |
+
value=f"{percentage:.1f}%",
|
| 212 |
+
delta=None
|
| 213 |
+
)
|
| 214 |
|
| 215 |
# Get AI Response
|
| 216 |
with st.spinner("Generating response..."):
|