Drew commited on
Commit
42eda46
1 Parent(s): 35c50c7

ripping out old db code and clean up

Browse files
Files changed (1) hide show
  1. app.py +46 -245
app.py CHANGED
@@ -18,11 +18,6 @@ from datetime import datetime
18
  #from dotenv import load_dotenv
19
 
20
  max_image_entries_to_show = 5
21
- '''
22
- load_dotenv()
23
- openai_key = os.getenv("OPENAI_KEY")
24
- elevenlabs_key = os.getenv("ELEVENLABS_KEY")
25
- '''
26
  import os
27
 
28
  openai_key = os.getenv('OPENAI_KEY')
@@ -40,62 +35,7 @@ voice=Voice(
40
 
41
  current_theme = "Household Objects"
42
 
43
- counter = 0
44
-
45
- class Photo:
46
- def __init__(self, name, image, theme, time_stamp):
47
- self.name = name
48
- self.image = image
49
- self.theme = theme
50
- self.time_stamp = time_stamp
51
-
52
- class PhotoWithResults:
53
- def __init__(self, name, image, theme, time_stamp, audio_url, style_score, theme_score, total_score, evaluation):
54
- self.name = name
55
- self.image = image
56
- self.theme = theme
57
- self.time_stamp = time_stamp
58
- self.audio_url = audio_url
59
- self.style_score = style_score
60
- self.theme_score = theme_score
61
- self.total_score = total_score
62
- self.evaluation = evaluation
63
-
64
  gallery_images = []
65
- #gallery_images.append((id, image, name, theme, theme_score, style_score, evaluation, audio_filepath))
66
-
67
- def playmusic_thread():
68
- # Load MP3 file as bytes
69
- with open('jeopardy.mp3', 'rb') as f:
70
- audio_data = f.read()
71
- play(audio_data)
72
-
73
- def speak_thread(text):
74
- """Function to run in the thread."""
75
- audio = client.generate(
76
- voice=voice,
77
- text=text,
78
- model="eleven_turbo_v2"
79
- )
80
- play(audio)
81
-
82
- def speak_and_save_thread(text, guid):
83
- """Function to run in the thread."""
84
- audio = client.generate(
85
- voice=voice,
86
- text=text,
87
- model="eleven_turbo_v2"
88
- )
89
- play(audio)
90
- save(audio, "audio/" + guid + ".mp3")
91
-
92
- def speak_and_save(text, audio_guid):
93
- thread = threading.Thread(target=speak_and_save_thread, args=(text,audio_guid,))
94
- thread.start()
95
-
96
- def speak(text):
97
- thread = threading.Thread(target=speak_thread, args=(text,))
98
- thread.start()
99
 
100
  def play_audio(audio_url):
101
  play(audio_url)
@@ -105,21 +45,6 @@ def playmusic():
105
  thread = threading.Thread(target=playmusic_thread, args=())
106
  thread.start()
107
 
108
- def get_theme():
109
- print ("GETTING THEME")
110
- response = requests.get("http://127.0.0.1:5000/theme")
111
- theme_info = response.json()
112
- print ("current theme: ", theme_info['theme'])
113
- return theme_info['theme']
114
-
115
- def set_theme(new_theme):
116
- #response = requests.post("http://127.0.0.1:5000/theme", json={"theme": new_theme})
117
- #theme_info = response.json()
118
- print ("new theme: ", new_theme)
119
- global current_theme
120
- current_theme = new_theme
121
- return new_theme
122
-
123
  def submit_photo(name, base64_image, time_stamp, theme):
124
  import requests
125
 
@@ -223,11 +148,10 @@ def generate_theme():
223
  try:
224
  theme = output_json['theme']
225
  dialog = output_json['dialog']
226
- speak(dialog)
 
227
  print(f"dialog: {dialog}")
228
  print(f"theme: {theme}")
229
- # Update theme on the server
230
- #set_theme(theme)
231
  current_theme = theme
232
  return theme
233
  except KeyError as e:
@@ -253,56 +177,6 @@ def create_image_with_text(image_data, text):
253
  image.save(buffered, format="JPEG")
254
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
255
 
256
- def get_photos_gallery_DB():
257
- import requests
258
- url = "http://127.0.0.1:5000/photos_with_results"
259
- response = requests.get(url)
260
- data = response.json()
261
- gallery_images = []
262
- for item in data:
263
- image_data = base64.b64decode(item['image'])
264
- image = Image.open(io.BytesIO(image_data))
265
- id = item['id']
266
- '''
267
- # Create a URL for the audio file
268
- audio_url = f"http://127.0.0.1:5000/audio/{item['audio_url']}.mp3"
269
- caption = f"{item['theme']} by {item['name']} | Style: {item['style_score']} | Theme: {item['theme_score']} | {audio_url}"'''
270
- evaluation = item['evaluation']
271
- #score = f"Style: {item['style_score']} | Theme: {item['theme_score']} | Total: {item['total_score']}"
272
- if item['style_score'] == None:
273
- item['style_score'] = 0
274
- if item['theme_score'] == None:
275
- item['theme_score'] = 0
276
- score = {"Theme Score": item['theme_score'] / 5.0, "Style Score": item['style_score'] / 5.0}
277
- name = item['name']
278
- theme = item['theme']
279
- gallery_images.append((id, image, name, theme, evaluation, score))
280
- print ("GALLERY IMAGES: ", gallery_images)
281
- return gallery_images
282
-
283
- def get_photos_db():
284
- import requests
285
- url = "http://127.0.0.1:5000/photos"
286
- response = requests.get(url)
287
- data = response.json()
288
- formatted_data = []
289
- for item in data:
290
- image_with_text = create_image_with_text(item['image'], f"{item['name']}, {item['total_score']}")
291
- row = [
292
-
293
- item['name'],
294
- f"<img src='data:image/jpeg;base64,{image_with_text}' style='width:100%; height:100%;'/>"
295
- #item['evaluation'],
296
- ]
297
- formatted_data.append(row)
298
- return formatted_data
299
-
300
- def clear_photos_db():
301
- import requests
302
- url = "http://127.0.0.1:5000/clear"
303
- response = requests.post(url)
304
- return response.json()
305
-
306
  def encode_image(image):
307
  if image.mode != 'RGB':
308
  image = image.convert('RGB')
@@ -310,38 +184,25 @@ def encode_image(image):
310
  image.save(buffered, format="JPEG")
311
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
312
 
313
- def process_image(id, image, name, theme):
314
  global gallery_images
315
- print ("PROCESSING IMAGE", name, theme, image)
 
 
 
 
 
 
 
 
316
  audio_guid = str(uuid.uuid4())
317
  print ("Image: ", image)
318
  #base64_image = encode_image(image)
319
- evaluation, theme_score, style_score = describe_image(image, name, theme) # Updated to unpack three values
320
- total_score = theme_score + style_score # Calculate total score
321
- #submit_photo_with_results(name, base64_image, datetime.now().isoformat(), audio_guid, theme, style_score, theme_score,
322
- #total_score, evaluation)
323
- #submit_updated_results(id, name, style_score, theme_score, total_score, evaluation)
324
  audio_filepath = gen_tts_and_save_to_path(evaluation, audio_guid)
325
- #gallery_images.append((id, image, name, theme, theme_score, style_score, evaluation, audio_filepath))
326
  # put at top of list
327
- gallery_images.insert(0, (id, image, name, theme, theme_score, style_score, evaluation, audio_filepath))
328
- return evaluation
329
-
330
- def submit_updated_results_db(photo_id, name, style_score, theme_score, total_score, evaluation):
331
- url = "http://127.0.0.1:5000/update_photo/" + str(photo_id)
332
- response = requests.post(url, json={
333
- "name": name,
334
- "style_score": style_score,
335
- "theme_score": theme_score,
336
- "total_score": total_score,
337
- "evaluation": evaluation
338
- })
339
- return response.json()
340
-
341
- def add_image_to_db(image, name, theme):
342
- base64_image = encode_image(image)
343
- theme = get_theme()
344
- submit_photo(name, base64_image, datetime.now().isoformat(), theme)
345
 
346
  def describe_image(image_array, name, theme):
347
  base64_image = encode_image(image_array)
@@ -425,56 +286,36 @@ def clear_gallery():
425
  gallery_images = []
426
  return update_gallery()
427
 
428
- def update_gallery(initial=False):
429
  global gallery_images
430
  gallery_items = []
431
  for i in range(max_image_entries_to_show):
432
- # This assumes gallery_images is a list of tuples (image, audio_url)
433
- #create_image_button(image, audio_url)
434
- #print ("IMAGE: ", image)
435
- #print ("AUDIO URL: ", audio_url)
436
- #image_input = gr.Image(image, label="Photo", type="pil")
437
- #name_input = gr.Textbox(label="Player Name", interactive=False)
438
-
439
- #gr.Audio(value=audio_url, label="Audio", type="filepath", show_label=False)
440
- print ("GALLERY IMAGES: ", gallery_images)
441
  if (i < len(gallery_images)):
442
- print ("GALLERY IMAGE: ", gallery_images[i])
443
- (id, image, name, theme, theme_score, style_score, evaluation, audio_filepath ) = gallery_images[i]
444
- print ("evaluation: ", evaluation)
445
- print ("ID", id)
446
- print ("audio_filepath: ", audio_filepath)
447
- id_output = gr.Textbox(label="ID", interactive=False, value=id, visible=False)
448
  name_output = gr.Textbox(label="Player", interactive=False, value=name, visible=True)
449
  theme_output = gr.Textbox(label="Theme", interactive=False, value=theme, visible=True)
450
- judge_button = gr.Button("Judge Image", visible=True)
451
  image_output = gr.Image(image, label="Photo", type="pil", visible=True)
452
  description_output = gr.Label(label="Image Description", value =evaluation, visible=True)
453
  audio_filepath = gr.Audio(audio_filepath, visible=True)
454
  score_output = gr.Label(label="Scores", value = theme_score + style_score, visible=True)
455
 
456
  else:
457
- id_output = gr.Textbox(label="ID", interactive=False, value="", visible=False)
458
  name_output = gr.Textbox(label="Player", interactive=False, value="", visible=False)
459
  theme_output = gr.Textbox(label="Theme", interactive=False, value="", visible=False)
460
- judge_button = gr.Button("Judge Image", visible=False)
461
  image_output = gr.Image(label="Photo", type="pil", visible=False)
462
  description_output = gr.Label(label="Image Description", visible=False)
463
  audio_filepath = gr.Audio(visible=False)
464
  score_output = gr.Label(label="Scores", visible=False)
465
 
466
 
467
- gallery_items.append(id_output)
468
  gallery_items.append(name_output)
469
  gallery_items.append(theme_output)
470
  gallery_items.append(image_output)
471
- gallery_items.append(judge_button)
472
  gallery_items.append(description_output)
473
  gallery_items.append(audio_filepath)
474
  gallery_items.append(score_output)
475
 
476
- if (initial):
477
- judge_button.click(fn=process_image, inputs=[id_output, image_output, name_output, theme_output], outputs=[description_output])
478
  return gallery_items
479
 
480
  def gen_tts_and_save_to_path(text, filename):
@@ -489,29 +330,11 @@ def gen_tts_and_save_to_path(text, filename):
489
 
490
  save(audio, full_path)
491
  return full_path
492
-
493
- def save_test_audio(text_box):
494
- print ("SAVING TEST AUDIO: ", text_box)
495
- audio = client.generate(
496
- voice=voice,
497
- text=text_box,
498
- model="eleven_turbo_v2"
499
- )
500
- audio_path = "audio/" + text_box.replace(" ", "_") + ".mp3"
501
- # make sure the audio directory exists
502
- os.makedirs(os.path.dirname(audio_path), exist_ok=True)
503
- save(audio, audio_path)
504
- return audio_path
505
-
506
- def get_current_theme():
507
- global current_theme
508
- print ("GETTING CURRENT THEME: " + current_theme)
509
- return current_theme
510
 
511
  def set_theme(input):
512
  global current_theme
513
  current_theme = input
514
- print ("CURRENT THEME: ", current_theme)
515
  return current_theme
516
 
517
  def game_start():
@@ -519,44 +342,31 @@ def game_start():
519
  return current_theme
520
 
521
  with gr.Blocks() as demo:
522
- with gr.Tab("Gallery"):
523
  with gr.Row():
524
-
525
-
526
- print ("CURRENT THEME: ", get_current_theme())
527
- current_theme_textbox = gr.Textbox(label="Current Theme", interactive=True, value=current_theme)
528
- new_theme_input = gr.Textbox(label="New Theme")
529
- update_theme_button = gr.Button("Update Theme")
530
- refresh_theme_button = gr.Button("Refresh Theme")
 
 
 
 
 
 
531
  generate_theme_button = gr.Button("Generate Theme")
532
 
533
- #when current theme is updated, update the theme on the server
534
- #current_theme.update(set_theme, inputs=new_theme_input, outputs=current_theme)
535
- current_theme_textbox.submit(set_theme, inputs=current_theme_textbox, outputs=current_theme_textbox)
536
-
537
  generate_theme_button.click(
538
  fn=generate_theme,
539
  inputs=None,
540
- outputs=current_theme_textbox
541
  )
542
-
543
- update_theme_button.click(
544
- fn=set_theme,
545
- inputs=new_theme_input,
546
- outputs=current_theme_textbox
547
- )
548
-
549
- '''
550
- refresh_theme_button.click(
551
- fn=get_theme,
552
- inputs=[],
553
- outputs=current_theme_textbox
554
- )
555
- '''
556
-
557
 
558
  refresh_button = gr.Button("Refresh Gallery")
559
- gallery_items = update_gallery(initial=True)
560
 
561
  # Use the function and connect outputs properly
562
  refresh_button.click(
@@ -565,29 +375,20 @@ with gr.Blocks() as demo:
565
  outputs=gallery_items
566
  )
567
 
568
- with gr.Tab("Phone"):
569
- with gr.Row():
570
- name_input = gr.Textbox(label="Player Name")
571
- id_input = gr.Textbox(label="ID", visible=True)
572
- with gr.Row():
573
- with gr.Column():
574
- image_input = gr.Image(type="pil", label="Upload Image")
575
- judge_button = gr.Button("Judge Image")
576
- description_output = gr.Textbox(label="Image Description", interactive=False)
577
- #score_output = gr.Label(label="Scores")
578
- judge_button.click(fn=process_image, inputs=[id_input, image_input, name_input, current_theme_textbox], outputs=[description_output])
579
-
580
  with gr.Tab("Dev"):
581
  with gr.Column():
582
  clear_button = gr.Button("Clear Gallery")
583
- with gr.Tab("Test"):
584
- begin_game_button = gr.Button("Begin Game")
585
- bg_music = gr.Audio(label="Background Music", autoplay=True)
586
- def playmusic():
587
- return "jeopardy.mp3"
588
- begin_game_button.click(playmusic, inputs=None, outputs=bg_music)
589
- clear_button.click(clear_gallery, inputs=[], outputs=gallery_items)
590
- demo.load(game_start, inputs=None, outputs=[current_theme_textbox])
 
 
591
  demo.load(update_gallery,
592
  inputs=[],
593
  outputs=gallery_items
 
18
  #from dotenv import load_dotenv
19
 
20
  max_image_entries_to_show = 5
 
 
 
 
 
21
  import os
22
 
23
  openai_key = os.getenv('OPENAI_KEY')
 
35
 
36
  current_theme = "Household Objects"
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  gallery_images = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  def play_audio(audio_url):
41
  play(audio_url)
 
45
  thread = threading.Thread(target=playmusic_thread, args=())
46
  thread.start()
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  def submit_photo(name, base64_image, time_stamp, theme):
49
  import requests
50
 
 
148
  try:
149
  theme = output_json['theme']
150
  dialog = output_json['dialog']
151
+ # to do - put this into a audio button
152
+ #speak(dialog)
153
  print(f"dialog: {dialog}")
154
  print(f"theme: {theme}")
 
 
155
  current_theme = theme
156
  return theme
157
  except KeyError as e:
 
177
  image.save(buffered, format="JPEG")
178
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  def encode_image(image):
181
  if image.mode != 'RGB':
182
  image = image.convert('RGB')
 
184
  image.save(buffered, format="JPEG")
185
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
186
 
187
+ def process_image(image, name):
188
  global gallery_images
189
+ global current_theme
190
+
191
+ if name == "":
192
+ return "Please enter a name."
193
+
194
+ if image is None:
195
+ return "Please upload an image."
196
+
197
+ print ("PROCESSING IMAGE", name, current_theme, image)
198
  audio_guid = str(uuid.uuid4())
199
  print ("Image: ", image)
200
  #base64_image = encode_image(image)
201
+ evaluation, theme_score, style_score = describe_image(image, name, current_theme) # Updated to unpack three values
 
 
 
 
202
  audio_filepath = gen_tts_and_save_to_path(evaluation, audio_guid)
 
203
  # put at top of list
204
+ gallery_images.insert(0, (image, name, current_theme, theme_score, style_score, evaluation, audio_filepath))
205
+ return "Your entry has been judged! Learn how you scored in the Judging Panel."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
  def describe_image(image_array, name, theme):
208
  base64_image = encode_image(image_array)
 
286
  gallery_images = []
287
  return update_gallery()
288
 
289
+ def update_gallery():
290
  global gallery_images
291
  gallery_items = []
292
  for i in range(max_image_entries_to_show):
 
 
 
 
 
 
 
 
 
293
  if (i < len(gallery_images)):
294
+ print ("loading image: ", i)
295
+ (image, name, theme, theme_score, style_score, evaluation, audio_filepath ) = gallery_images[i]
 
 
 
 
296
  name_output = gr.Textbox(label="Player", interactive=False, value=name, visible=True)
297
  theme_output = gr.Textbox(label="Theme", interactive=False, value=theme, visible=True)
 
298
  image_output = gr.Image(image, label="Photo", type="pil", visible=True)
299
  description_output = gr.Label(label="Image Description", value =evaluation, visible=True)
300
  audio_filepath = gr.Audio(audio_filepath, visible=True)
301
  score_output = gr.Label(label="Scores", value = theme_score + style_score, visible=True)
302
 
303
  else:
 
304
  name_output = gr.Textbox(label="Player", interactive=False, value="", visible=False)
305
  theme_output = gr.Textbox(label="Theme", interactive=False, value="", visible=False)
 
306
  image_output = gr.Image(label="Photo", type="pil", visible=False)
307
  description_output = gr.Label(label="Image Description", visible=False)
308
  audio_filepath = gr.Audio(visible=False)
309
  score_output = gr.Label(label="Scores", visible=False)
310
 
311
 
 
312
  gallery_items.append(name_output)
313
  gallery_items.append(theme_output)
314
  gallery_items.append(image_output)
 
315
  gallery_items.append(description_output)
316
  gallery_items.append(audio_filepath)
317
  gallery_items.append(score_output)
318
 
 
 
319
  return gallery_items
320
 
321
  def gen_tts_and_save_to_path(text, filename):
 
330
 
331
  save(audio, full_path)
332
  return full_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
 
334
  def set_theme(input):
335
  global current_theme
336
  current_theme = input
337
+ print ("changed theme: ", current_theme)
338
  return current_theme
339
 
340
  def game_start():
 
342
  return current_theme
343
 
344
  with gr.Blocks() as demo:
345
+ with gr.Tab("Photo Submission"):
346
  with gr.Row():
347
+ name_input = gr.Textbox(label="Player Name")
348
+ theme_reminder = gr.Textbox(current_theme, label="Current Theme")
349
+ with gr.Row():
350
+ with gr.Column():
351
+ image_input = gr.Image(type="pil", label="Upload Image")
352
+ judge_button = gr.Button("Judge Image")
353
+ process_image_output = gr.Textbox(label="Judging Feedback", interactive=False)
354
+ #score_output = gr.Label(label="Scores")
355
+ judge_button.click(fn=process_image, inputs=[image_input, name_input], outputs=[process_image_output])
356
+
357
+ with gr.Tab("Judging Panel"):
358
+ with gr.Row():
359
+ theme_textbox = gr.Textbox(label="Change Theme", interactive=True, value=current_theme)
360
  generate_theme_button = gr.Button("Generate Theme")
361
 
 
 
 
 
362
  generate_theme_button.click(
363
  fn=generate_theme,
364
  inputs=None,
365
+ outputs=theme_textbox
366
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
 
368
  refresh_button = gr.Button("Refresh Gallery")
369
+ gallery_items = update_gallery()
370
 
371
  # Use the function and connect outputs properly
372
  refresh_button.click(
 
375
  outputs=gallery_items
376
  )
377
 
378
+
 
 
 
 
 
 
 
 
 
 
 
379
  with gr.Tab("Dev"):
380
  with gr.Column():
381
  clear_button = gr.Button("Clear Gallery")
382
+
383
+ begin_game_button = gr.Button("Begin Game")
384
+ bg_music = gr.Audio(label="Background Music", autoplay=True)
385
+ def playmusic():
386
+ return "jeopardy.mp3"
387
+ begin_game_button.click(playmusic, inputs=None, outputs=bg_music)
388
+
389
+ clear_button.click(clear_gallery, inputs=[], outputs=gallery_items)
390
+ theme_textbox.change(set_theme, inputs=theme_textbox, outputs=theme_reminder)
391
+ demo.load(game_start, inputs=None, outputs=[theme_textbox])
392
  demo.load(update_gallery,
393
  inputs=[],
394
  outputs=gallery_items