lunadebruyne commited on
Commit
1742f92
Β·
1 Parent(s): f3aef14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -20
app.py CHANGED
@@ -161,19 +161,6 @@ def dist(output_file, input_checks):
161
  'Frequency': [3, 5, 1, 8, 2, 3, 4, 7, 1, 12, 4, 2, 3, 6, 3, 10, 3, 4],
162
  'Emotion category': ['neutral', 'anger', 'fear', 'joy', 'love', 'sadness', 'neutral', 'anger', 'fear', 'joy', 'love', 'sadness', 'neutral', 'anger', 'fear', 'joy', 'love', 'sadness']})
163
 
164
- if "peaks" in input_checks:
165
- return gr.LinePlot.update(data,x="Date",y="Frequency",color="Emotion category",color_legend_position="bottom",title="Emotion distribution over time",tooltip=['Date', 'Frequency', 'Emotion category'],height=300,width=500,visible=True), gr.update(visible=True), gr.update(visible=False) # next_button_peaks becomes available
166
- elif "topics" in input_checks:
167
- return gr.LinePlot.update(data,x="Date",y="Frequency",color="Emotion category",color_legend_position="bottom",title="Emotion distribution over time",tooltip=['Date', 'Frequency', 'Emotion category'],height=300,width=500,visible=True), gr.update(visible=False), gr.update(visible=True) # next_button_topics becomes available
168
- else:
169
- return gr.LinePlot.update(data,x="Date",y="Frequency",color="Emotion category",color_legend_position="bottom",title="Emotion distribution over time",tooltip=['Date', 'Frequency', 'Emotion category'],height=300,width=500,visible=True), gr.update(visible=False), gr.update(visible=False) # no next_button becomes available
170
-
171
- def dist_colors(output_file, input_checks):
172
- data = pd.DataFrame({
173
- 'Date': ['1/1', '1/1', '1/1', '1/1', '1/1', '1/1', '2/1', '2/1', '2/1', '2/1', '2/1', '2/1', '3/1', '3/1', '3/1', '3/1', '3/1', '3/1'],
174
- 'Frequency': [3, 5, 1, 8, 2, 3, 4, 7, 1, 12, 4, 2, 3, 6, 3, 10, 3, 4],
175
- 'Emotion category': ['neutral', 'anger', 'fear', 'joy', 'love', 'sadness', 'neutral', 'anger', 'fear', 'joy', 'love', 'sadness', 'neutral', 'anger', 'fear', 'joy', 'love', 'sadness']})
176
-
177
  domain = ['neutral', 'anger', 'fear', 'joy', 'love', 'sadness']
178
  range_ = ['#999999', '#b22222', '#663399', '#ffcc00', '#db7093', '#6495ed']
179
  n = max(data['Frequency'])
@@ -209,13 +196,96 @@ def dist_colors(output_file, input_checks):
209
  else:
210
  return gr.Plot.update(value=plot, visible=True), gr.update(visible=False), gr.update(visible=False) # no next_button becomes available
211
 
212
-
213
  def peaks(output_file, input_checks):
214
- output = "There are peaks on some dates."
215
- if "topics" in input_checks:
216
- return gr.update(value=output, visible=True), gr.update(visible=True) # next_button_topics becomes available
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  else:
218
- return gr.update(value=output, visible=True), gr.update(visible=False) # no next_button becomes available
219
 
220
  def topics(output_file, input_checks):
221
  output = "Some topics are found."
@@ -258,14 +328,14 @@ with gr.Blocks() as demo:
258
  output_dist = gr.Plot(label="Emotion distribution over time", visible=False)
259
  next_button_peaks = gr.Button("Show peaks", visible=False)
260
 
261
- output_peaks = gr.Textbox(label="Peaks", visible=False)
262
  next_button_topics = gr.Button("Show topics", visible=False)
263
 
264
  output_topics = gr.Textbox(label="Topics", visible=False)
265
 
266
  send_btn.click(fn=file, inputs=[input_file,input_checks], outputs=[output_file,next_button_freq,next_button_dist,next_button_peaks,next_button_topics])
267
  next_button_freq.click(fn=freq, inputs=[output_file,input_checks], outputs=[output_plot,next_button_dist,next_button_peaks,next_button_topics])
268
- next_button_dist.click(fn=dist_colors, inputs=[output_file,input_checks], outputs=[output_dist,next_button_peaks,next_button_topics])
269
  next_button_peaks.click(fn=peaks, inputs=[output_file,input_checks], outputs=[output_peaks,next_button_topics])
270
  next_button_topics.click(fn=topics, inputs=[output_file,input_checks], outputs=output_topics)
271
 
 
161
  'Frequency': [3, 5, 1, 8, 2, 3, 4, 7, 1, 12, 4, 2, 3, 6, 3, 10, 3, 4],
162
  'Emotion category': ['neutral', 'anger', 'fear', 'joy', 'love', 'sadness', 'neutral', 'anger', 'fear', 'joy', 'love', 'sadness', 'neutral', 'anger', 'fear', 'joy', 'love', 'sadness']})
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  domain = ['neutral', 'anger', 'fear', 'joy', 'love', 'sadness']
165
  range_ = ['#999999', '#b22222', '#663399', '#ffcc00', '#db7093', '#6495ed']
166
  n = max(data['Frequency'])
 
196
  else:
197
  return gr.Plot.update(value=plot, visible=True), gr.update(visible=False), gr.update(visible=False) # no next_button becomes available
198
 
 
199
  def peaks(output_file, input_checks):
200
+ peaks_neutral = {"13/3/2020": "up", "20/3/2020": "up", "21/3/2020": "down"}
201
+ peaks_anger = {"13/3/2020": "up", "20/3/2020": "up", "21/3/2020": "down"}
202
+ peaks_fear = {"13/3/2020": "up", "20/3/2020": "up", "21/3/2020": "down"}
203
+ peaks_joy = {"13/3/2020": "up", "20/3/2020": "up", "21/3/2020": "down"}
204
+ peaks_love = {"13/3/2020": "up", "20/3/2020": "up", "21/3/2020": "down"}
205
+ peaks_sadness = {"13/3/2020": "up", "20/3/2020": "up", "21/3/2020": "down"}
206
+
207
+ text_neutral = ", ".join([str(key) + " (↑)" if value == "up" else str(key) + " (↓)" for key, value in peaks_neutral.items()])
208
+ text_anger = ", ".join([str(key) + " (↑)" if value == "up" else str(key) + " (↓)" for key, value in peaks_anger.items()])
209
+ text_fear = ", ".join([str(key) + " (↑)" if value == "up" else str(key) + " (↓)" for key, value in peaks_fear.items()])
210
+ text_joy = ", ".join([str(key) + " (↑)" if value == "up" else str(key) + " (↓)" for key, value in peaks_joy.items()])
211
+ text_love = ", ".join([str(key) + " (↑)" if value == "up" else str(key) + " (↓)" for key, value in peaks_love.items()])
212
+ text_sadness = ", ".join([str(key) + " (↑)" if value == "up" else str(key) + " (↓)" for key, value in peaks_sadness.items()])
213
+
214
+ html = (
215
+ '<html>'
216
+ '<head>'
217
+ '<meta name="viewport" content="width=device-width, initial-scale=1">'
218
+ '<style>'
219
+ '.dot_neutral {'
220
+ 'height: 11px;'
221
+ 'width: 11px;'
222
+ 'background-color: #999999;'
223
+ 'border-radius: 50%;'
224
+ 'display: inline-block;'
225
+ '}'
226
+ '.dot_anger {'
227
+ 'height: 11px;'
228
+ 'width: 11px;'
229
+ 'background-color: #b22222;'
230
+ 'border-radius: 50%;'
231
+ 'display: inline-block;'
232
+ '}'
233
+ '.dot_fear {'
234
+ 'height: 11px;'
235
+ 'width: 11px;'
236
+ 'background-color: #663399;'
237
+ 'border-radius: 50%;'
238
+ 'display: inline-block;'
239
+ '}'
240
+ '.dot_joy {'
241
+ 'height: 11px;'
242
+ 'width: 11px;'
243
+ 'background-color: #ffcc00;'
244
+ 'border-radius: 50%;'
245
+ 'display: inline-block;'
246
+ '}'
247
+ '.dot_love {'
248
+ 'height: 11px;'
249
+ 'width: 11px;'
250
+ 'background-color: #db7093;'
251
+ 'border-radius: 50%;'
252
+ 'display: inline-block;'
253
+ '}'
254
+ '.dot_sadness {'
255
+ 'height: 11px;'
256
+ 'width: 11px;'
257
+ 'background-color: #6495ed;'
258
+ 'border-radius: 50%;'
259
+ 'display: inline-block;'
260
+ '}'
261
+ '.tab {'
262
+ 'padding-left: 1em;'
263
+ '}'
264
+ '</style>'
265
+ '</head>'
266
+ '<body>'
267
+ '<div>'
268
+ '<p>These significant fluctuations were found:</p>'
269
+ '<p><span class="dot_neutral"></span> neutral:</p>'
270
+ '<p class="tab">' + text_neutral + '<p>'
271
+ '<p><span class="dot_anger"></span> anger:</p>'
272
+ '<p class="tab">' + text_anger + '<p>'
273
+ '<p><span class="dot_fear"></span> fear:</p>'
274
+ '<p class="tab">' + text_fear + '<p>'
275
+ '<p><span class="dot_joy"></span> joy:</p>'
276
+ '<p class="tab">' + text_joy + '<p>'
277
+ '<p><span class="dot_love"></span> love:</p>'
278
+ '<p class="tab">' + text_love + '<p>'
279
+ '<p><span class="dot_sadness"></span> sadness:</p>'
280
+ '<p class="tab">' + text_sadness + '<p>'
281
+ '</div>'
282
+ '</body>'
283
+ '</html>'
284
+ )
285
+ if "topics" in input_checks:
286
+ return gr.update(value=html, visible=True), gr.update(visible=True) # next_button_topics becomes available
287
  else:
288
+ return gr.update(value=html, visible=True), gr.update(visible=False) # no next_button becomes available
289
 
290
  def topics(output_file, input_checks):
291
  output = "Some topics are found."
 
328
  output_dist = gr.Plot(label="Emotion distribution over time", visible=False)
329
  next_button_peaks = gr.Button("Show peaks", visible=False)
330
 
331
+ output_peaks = gr.HTML(label="Peaks", visible=False)
332
  next_button_topics = gr.Button("Show topics", visible=False)
333
 
334
  output_topics = gr.Textbox(label="Topics", visible=False)
335
 
336
  send_btn.click(fn=file, inputs=[input_file,input_checks], outputs=[output_file,next_button_freq,next_button_dist,next_button_peaks,next_button_topics])
337
  next_button_freq.click(fn=freq, inputs=[output_file,input_checks], outputs=[output_plot,next_button_dist,next_button_peaks,next_button_topics])
338
+ next_button_dist.click(fn=dist, inputs=[output_file,input_checks], outputs=[output_dist,next_button_peaks,next_button_topics])
339
  next_button_peaks.click(fn=peaks, inputs=[output_file,input_checks], outputs=[output_peaks,next_button_topics])
340
  next_button_topics.click(fn=topics, inputs=[output_file,input_checks], outputs=output_topics)
341