Dan Mo commited on
Commit
33ce270
·
1 Parent(s): b668c7e

Add random example feature and restructure emotion examples in Gradio interface

Browse files
Files changed (1) hide show
  1. app.py +88 -90
app.py CHANGED
@@ -7,6 +7,7 @@ import gradio as gr
7
  from utils import logger
8
  from emoji_processor import EmojiProcessor
9
  from config import EMBEDDING_MODELS
 
10
 
11
  class EmojiMashupApp:
12
  def __init__(self):
@@ -15,6 +16,9 @@ class EmojiMashupApp:
15
  self.processor = EmojiProcessor(model_key="mpnet", use_cached_embeddings=True) # Default to mpnet
16
  self.processor.load_emoji_dictionaries()
17
 
 
 
 
18
  def create_model_dropdown_choices(self):
19
  """Create formatted choices for the model dropdown.
20
 
@@ -51,6 +55,17 @@ class EmojiMashupApp:
51
  return f"Failed to switch to {model_key} model"
52
  else:
53
  return f"Unknown model: {model_key}"
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  def process_with_model(self, model_selection, text, use_cached_embeddings):
56
  """Process text with selected model.
@@ -81,6 +96,43 @@ class EmojiMashupApp:
81
  Returns:
82
  Gradio Interface object
83
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  with gr.Blocks(title="Sentence → Emoji Mashup") as interface:
85
  gr.Markdown("# Sentence → Emoji Mashup")
86
  gr.Markdown("Get the top emotion and event emoji from your sentence, and view the mashup!")
@@ -102,12 +154,15 @@ class EmojiMashupApp:
102
  info="When enabled, embeddings will be saved to and loaded from disk"
103
  )
104
 
105
- # Text input
106
- text_input = gr.Textbox(
107
- lines=2,
108
- placeholder="Type a sentence...",
109
- label="Your message"
110
- )
 
 
 
111
 
112
  # Process button
113
  submit_btn = gr.Button("Generate Emoji Mashup", variant="primary")
@@ -141,6 +196,13 @@ class EmojiMashupApp:
141
  outputs=[model_info]
142
  )
143
 
 
 
 
 
 
 
 
144
  # Process button handler with share button visibility update
145
  def process_and_show_share(model_selection, text, use_cached_embeddings):
146
  result = self.process_with_model(model_selection, text, use_cached_embeddings)
@@ -174,92 +236,28 @@ class EmojiMashupApp:
174
  }"""
175
  )
176
 
177
- # Examples based on Plutchik's Wheel of Emotions - now open by default
178
- with gr.Accordion("Examples", open=True):
179
- gr.Markdown("### Primary Emotions")
180
- gr.Examples(
181
- examples=[
182
- # Joy vs. Sadness
183
- ["I feel so happy and excited today!"],
184
- ["I'm feeling really sad and down right now"],
185
-
186
- # Trust vs. Disgust
187
- ["I completely trust my best friend with my life"],
188
- ["That smells absolutely disgusting and makes me nauseous"],
189
-
190
- # Fear vs. Anger
191
- ["I'm terrified of what might happen next"],
192
- ["I'm furious about how they treated me yesterday"],
193
-
194
- # Surprise vs. Anticipation
195
- ["Wow! I can't believe what just happened - totally unexpected!"],
196
- ["I'm eagerly waiting to see what happens next"]
197
- ],
198
- inputs=text_input,
199
- label="Primary Emotions"
200
- )
201
 
202
- gr.Markdown("### Secondary Emotions")
203
- gr.Examples(
204
- examples=[
205
- # Love (Joy + Trust)
206
- ["I deeply love and adore my family more than anything"],
207
-
208
- # Submission (Trust + Fear)
209
- ["I respect their authority and will follow their instructions"],
210
-
211
- # Awe (Fear + Surprise)
212
- ["I'm in awe of the magnificent view from the summit"],
213
-
214
- # Disapproval (Surprise + Sadness)
215
- ["I'm disappointed by the unexpected poor quality of work"],
216
-
217
- # Remorse (Sadness + Disgust)
218
- ["I feel so guilty and ashamed about what I did"],
219
-
220
- # Contempt (Disgust + Anger)
221
- ["I have nothing but contempt for their dishonest behavior"],
222
-
223
- # Aggressiveness (Anger + Anticipation)
224
- ["I'm determined to confront them about this issue"],
225
-
226
- # Optimism (Anticipation + Joy)
227
- ["I'm optimistic and hopeful about what the future holds"]
228
- ],
229
- inputs=text_input,
230
- label="Secondary Emotions"
231
- )
232
 
233
- gr.Markdown("### Tertiary Emotions")
234
- gr.Examples(
235
- examples=[
236
- # Anxiety (Anticipation + Fear)
237
- ["I'm feeling anxious about my upcoming presentation"],
238
-
239
- # Hope (Anticipation + Trust)
240
- ["I'm hopeful that everything will work out in the end"],
241
-
242
- # Jealousy (Anger + Trust)
243
- ["I felt jealous when I saw them together laughing"],
244
-
245
- # Sentimentality (Trust + Sadness)
246
- ["Looking at old photos makes me feel nostalgic and sentimental"],
247
-
248
- # Despair (Fear + Sadness)
249
- ["I'm in complete despair and see no way out of this situation"],
250
-
251
- # Shame (Fear + Disgust)
252
- ["I'm so embarrassed and ashamed of my behavior yesterday"],
253
-
254
- # Morbidness (Disgust + Joy)
255
- ["I have a strange fascination with creepy abandoned buildings"],
256
-
257
- # Delight (Surprise + Joy)
258
- ["I was absolutely delighted by the unexpected gift"]
259
- ],
260
- inputs=text_input,
261
- label="Tertiary Emotions"
262
- )
263
 
264
  return interface
265
 
 
7
  from utils import logger
8
  from emoji_processor import EmojiProcessor
9
  from config import EMBEDDING_MODELS
10
+ import random
11
 
12
  class EmojiMashupApp:
13
  def __init__(self):
 
16
  self.processor = EmojiProcessor(model_key="mpnet", use_cached_embeddings=True) # Default to mpnet
17
  self.processor.load_emoji_dictionaries()
18
 
19
+ # Store all example sentences for the random picker
20
+ self.all_examples = []
21
+
22
  def create_model_dropdown_choices(self):
23
  """Create formatted choices for the model dropdown.
24
 
 
55
  return f"Failed to switch to {model_key} model"
56
  else:
57
  return f"Unknown model: {model_key}"
58
+
59
+ def get_random_example(self):
60
+ """Get a random example from the collected examples.
61
+
62
+ Returns:
63
+ A randomly selected example sentence
64
+ """
65
+ if not self.all_examples:
66
+ # Return a default message if no examples are available
67
+ return "I feel so happy and excited today!"
68
+ return random.choice(self.all_examples)
69
 
70
  def process_with_model(self, model_selection, text, use_cached_embeddings):
71
  """Process text with selected model.
 
96
  Returns:
97
  Gradio Interface object
98
  """
99
+ # Define all example sentences
100
+ primary_examples = [
101
+ "I feel so happy and excited today!",
102
+ "I'm feeling really sad and down right now",
103
+ "I completely trust my best friend with my life",
104
+ "That smells absolutely disgusting and makes me nauseous",
105
+ "I'm terrified of what might happen next",
106
+ "I'm furious about how they treated me yesterday",
107
+ "Wow! I can't believe what just happened - totally unexpected!",
108
+ "I'm eagerly waiting to see what happens next"
109
+ ]
110
+
111
+ secondary_examples = [
112
+ "I deeply love and adore my family more than anything",
113
+ "I respect their authority and will follow their instructions",
114
+ "I'm in awe of the magnificent view from the summit",
115
+ "I'm disappointed by the unexpected poor quality of work",
116
+ "I feel so guilty and ashamed about what I did",
117
+ "I have nothing but contempt for their dishonest behavior",
118
+ "I'm determined to confront them about this issue",
119
+ "I'm optimistic and hopeful about what the future holds"
120
+ ]
121
+
122
+ tertiary_examples = [
123
+ "I'm feeling anxious about my upcoming presentation",
124
+ "I'm hopeful that everything will work out in the end",
125
+ "I felt jealous when I saw them together laughing",
126
+ "Looking at old photos makes me feel nostalgic and sentimental",
127
+ "I'm in complete despair and see no way out of this situation",
128
+ "I'm so embarrassed and ashamed of my behavior yesterday",
129
+ "I have a strange fascination with creepy abandoned buildings",
130
+ "I was absolutely delighted by the unexpected gift"
131
+ ]
132
+
133
+ # Store all examples for the random picker
134
+ self.all_examples = primary_examples + secondary_examples + tertiary_examples
135
+
136
  with gr.Blocks(title="Sentence → Emoji Mashup") as interface:
137
  gr.Markdown("# Sentence → Emoji Mashup")
138
  gr.Markdown("Get the top emotion and event emoji from your sentence, and view the mashup!")
 
154
  info="When enabled, embeddings will be saved to and loaded from disk"
155
  )
156
 
157
+ # Text input with random example button
158
+ with gr.Row():
159
+ text_input = gr.Textbox(
160
+ lines=2,
161
+ placeholder="Type a sentence...",
162
+ label="Your message",
163
+ scale=9
164
+ )
165
+ random_btn = gr.Button("🎲", scale=1, min_width=40, size="sm", variant="secondary", tooltip="Pick a random example")
166
 
167
  # Process button
168
  submit_btn = gr.Button("Generate Emoji Mashup", variant="primary")
 
196
  outputs=[model_info]
197
  )
198
 
199
+ # Random example button handler
200
+ random_btn.click(
201
+ fn=self.get_random_example,
202
+ inputs=None,
203
+ outputs=text_input
204
+ )
205
+
206
  # Process button handler with share button visibility update
207
  def process_and_show_share(model_selection, text, use_cached_embeddings):
208
  result = self.process_with_model(model_selection, text, use_cached_embeddings)
 
236
  }"""
237
  )
238
 
239
+ # Examples section - using Tabs instead of Accordion to ensure visibility
240
+ gr.Markdown("## Emotion Examples")
241
+ gr.Markdown("Try these examples based on Plutchik's Wheel of Emotions:")
242
+
243
+ with gr.Tabs() as tabs:
244
+ with gr.TabItem("Primary Emotions"):
245
+ gr.Examples(
246
+ examples=[[example] for example in primary_examples],
247
+ inputs=text_input
248
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
+ with gr.TabItem("Secondary Emotions"):
251
+ gr.Examples(
252
+ examples=[[example] for example in secondary_examples],
253
+ inputs=text_input
254
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
+ with gr.TabItem("Tertiary Emotions"):
257
+ gr.Examples(
258
+ examples=[[example] for example in tertiary_examples],
259
+ inputs=text_input
260
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
 
262
  return interface
263