solinm commited on
Commit
9a5c15a
·
verified ·
1 Parent(s): cafc28f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -89
app.py CHANGED
@@ -5,27 +5,36 @@ from groq import Groq
5
  api_key = os.getenv("GROQ_API_KEY")
6
  client = Groq(api_key=api_key)
7
 
 
 
8
  def chat_with_bot_stream(user_input, conversation_history):
9
  if conversation_history is None:
10
  conversation_history = []
11
 
12
  conversation_history.append({"role": "user", "content": user_input})
13
 
 
14
  if len(conversation_history) == 1:
15
  conversation_history.insert(0, {
16
  "role": "system",
17
  "content": (
18
  "You are a music and genre recommendation bot designed to help users discover new music "
19
- "based on their preferences, mood, or activity.\n\nYour responses should be engaging, "
20
- "personalized, and insightful. You can recommend:\n\n- Specific songs, albums, or artists\n"
21
- "- Genres based on mood, activity, or past preferences\n- Hidden gems and deep cuts "
22
- "for music enthusiasts\n- Trending or classic hits based on user taste\n\nBe conversational, "
23
- "suggest multiple options when possible, and encourage users to explore new sounds. "
24
- "If requested, provide brief descriptions of artists or genres, and explain why a "
25
- "recommendation might suit the user."
26
- "If asked about visibility on the website, such as font sizing and themes (dark or light mode ONLY), direct them to the settings tab at the top of the chatbot."
27
- "If asked about the languages you speak, you can say that you speak English, Spanish, and French ONLY. Direct the user to the settings tab to change the language."
28
- "Limit your responses to music-related inquiries only. Limit your responses to 10 lines or less."
 
 
 
 
 
 
29
  )
30
  })
31
 
@@ -48,7 +57,8 @@ def chat_with_bot_stream(user_input, conversation_history):
48
  (msg["content"] if msg["role"] == "user" else None,
49
  msg["content"] if msg["role"] == "assistant" else None)
50
  for msg in conversation_history
51
- ], conversation_history
 
52
 
53
  # word translation
54
  translations = {
@@ -180,35 +190,39 @@ def update_all_settings(theme, font_size, language):
180
 
181
  def update_styles(theme, font_size):
182
  if theme == "Dark":
183
- bg = "#18181B"
184
- text_color = "#ffffff"
185
- input_bg = "#2E2E2E"
186
- button_bg = "#3A3A3A"
187
- settings_box_bg = "#2E2E2E"
188
- apply_button_bg = "#3A3A3A"
189
- radio_bg = "#2E2E2E"
190
- settings_heading_color = text_color
191
  border_color = "#444444"
192
  placeholder_color = "#e8e8e8"
193
- chatbot_bg = "#18181B"
 
 
194
  chatbot_text_color = "#ffffff"
195
  purple="#56246B"
196
  light_purple="#795699"
 
 
 
 
197
  else:
198
- bg = "#ffffff"
199
- text_color = "#000000"
200
- input_bg = "#f2f2f2"
201
- button_bg = "#dddddd"
202
- settings_box_bg = "#eeeeee"
203
- apply_button_bg = "#e0e0e0"
204
- radio_bg = "#e0e0e0"
205
- settings_heading_color = "#333333"
206
- border_color = "#cccccc"
207
- placeholder_color = "#e8e8e8"
208
- chatbot_bg = "#cfcfcf"
209
  chatbot_text_color = "#000000"
210
  purple="#56246B"
211
  light_purple="#795699"
 
 
 
 
212
 
213
  if font_size == "Small":
214
  fsize = "0.8rem"
@@ -226,6 +240,7 @@ def update_styles(theme, font_size):
226
  style = f"""
227
  <style id="dynamic_styles">
228
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
 
229
  body, .gradio-container {{
230
  background-color: {bg} !important;
231
  color: {text_color} !important;
@@ -253,44 +268,39 @@ def update_styles(theme, font_size):
253
  margin-bottom: 10px;
254
  }}
255
 
256
- h3 {{
257
- color: {text_color} !important;
258
- text-align: center;
259
- display: flex;
260
- justify-content: center;
261
- align-items: center;
262
- height: 100%;
263
- font-size: {fsize} !important;
264
- }}
265
-
266
  #chatbot {{
267
  background-color: {chatbot_bg} !important;
268
  color: {chatbot_text_color} !important;
269
  font-size: {fsize} !important;
 
 
 
 
 
 
 
 
270
  }}
271
 
272
  div[role="log"] {{
 
273
  background-color: {chatbot_bg} !important;
274
  color: {chatbot_text_color} !important;
 
275
  }}
276
 
277
- div[data-testid="bot"], div[data-testid="user"] {{
278
- color: {chatbot_text_color} !important;
279
- }}
280
-
281
  #user_input {{
282
  background-color: {purple} !important;
283
- border: 1px solid {purple} !important;
284
  height: 65px;
285
  }}
286
 
287
  #user_input textarea {{
288
  background-color: {light_purple} !important;
289
- color: {text_color} !important;
290
  font-size: {fsize} !important;
291
- border: 1px solid {light_purple} !important;
292
  padding: 10px;
293
- border: none;
294
  }}
295
 
296
  #user_input textarea::placeholder {{
@@ -299,48 +309,78 @@ def update_styles(theme, font_size):
299
  font-size: {fsize} !important;
300
  opacity: 0.73;
301
  }}
302
-
303
  #send_button {{
304
- background-color: {purple} !important;
305
- color: {placeholder_color} !important;
306
  font-size: {fsize} !important;
307
- border: 1px solid {purple} !important;
 
308
  margin-left: 1px;
309
  font-family: 'Inter', sans-serif;
310
- height: 67px;
311
  display: flex;
312
  align-items: center;
313
  justify-content: center;
314
  padding: 0 10px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
  }}
316
 
 
 
 
 
317
  #settings_box {{
318
  background-color: {settings_box_bg} !important;
319
- padding: 10px;
320
- border-radius: 8px;
321
  font-size: {fsize} !important;
322
- border: 1px solid {border_color} !important;
 
 
 
323
  }}
324
 
325
- #settings_box .gr-markdown h3 {{
326
- color: {settings_heading_color} !important;
 
 
 
 
 
 
327
  font-size: {fsize} !important;
328
  }}
329
-
330
- #settings_box .gr-radio-group,
331
- #settings_box .gr-radio-group * {{
332
- background-color: {radio_bg} !important;
 
 
 
 
333
  color: {text_color} !important;
334
- font-size: {fsize} !important;
335
  }}
336
-
337
- #apply_settings_button {{
338
- background-color: {purple} !important;
339
- color: {placeholder_color} !important;
340
- font-size: {fsize} !important;
341
- border: 1px solid {border_color} !important;
342
  }}
343
-
344
  div[role="tablist"] {{
345
  border: 1px;
346
  border-radius: 8px;
@@ -348,6 +388,7 @@ def update_styles(theme, font_size):
348
  height: auto !important;
349
  min-height: 0 !important;
350
  }}
 
351
  div[role="tab"] {{
352
  font-size: {fsize} !important;
353
  padding: {tab_padding} !important;
@@ -355,7 +396,7 @@ def update_styles(theme, font_size):
355
  }}
356
 
357
  button[role="tab"][aria-selected="true"]::after {{
358
- background-color:#56246B !important;
359
  }}
360
 
361
  button[role="tab"][aria-selected="true"] {{
@@ -366,7 +407,7 @@ def update_styles(theme, font_size):
366
  background-color:transparent !important;
367
  color: #616161 !important;
368
  }}
369
-
370
  input[type="radio"] {{
371
  background-color: #795699 !important;
372
  }}
@@ -376,18 +417,32 @@ def update_styles(theme, font_size):
376
  border-color: #795699 !important;
377
  }}
378
 
379
- fieldset {{
380
- background-color: {settings_box_bg} !important;
381
- color: {text_color} !important;
382
  }}
383
 
384
- label[data-testid] {{
385
- background-color: {bg} !important;
386
- color: {text_color} !important;
387
  }}
388
 
389
- [data-testid="block-info"] {{
390
- color: {text_color} !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  }}
392
 
393
  </style>
@@ -402,7 +457,8 @@ with gr.Blocks(css=None) as demo:
402
  with gr.TabItem("💬 Chatbot"):
403
  gr.HTML('<div id="header_section"></div>')
404
  header_desc = gr.HTML('<p id="header_description">' + translations["English"]["header_description"] + '</p>')
405
- chatbot = gr.Chatbot(label="", height=400, show_label=False, elem_id="chatbot")
 
406
  with gr.Row():
407
  user_input = gr.Textbox(
408
  show_label=False,
@@ -419,6 +475,29 @@ with gr.Blocks(css=None) as demo:
419
  )
420
  conversation_state = gr.State(value=[])
421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  send_button.click(
423
  fn=chat_with_bot_stream,
424
  inputs=[user_input, conversation_state],
@@ -436,21 +515,21 @@ with gr.Blocks(css=None) as demo:
436
  theme_radio = gr.Radio(
437
  choices=translations["English"]["theme_choices"],
438
  value="Dark",
439
- label=translations["English"]["theme_label"]
440
  )
441
  font_radio = gr.Radio(
442
  choices=translations["English"]["font_size_choices"],
443
  value="Medium",
444
- label=translations["English"]["font_size_label"]
445
  )
446
  language_radio = gr.Radio(
447
  choices=translations["English"]["language_choices"],
448
  value="English",
449
- label=translations["English"]["select_language_label"]
450
  )
451
  apply_button = gr.Button(
452
- translations["English"]["apply_settings_button"],
453
- elem_id="apply_settings_button"
454
  )
455
  apply_button.click(
456
  fn=update_all_settings,
@@ -467,5 +546,5 @@ with gr.Blocks(css=None) as demo:
467
  language_radio
468
  ]
469
  )
470
-
471
- demo.launch()
 
5
  api_key = os.getenv("GROQ_API_KEY")
6
  client = Groq(api_key=api_key)
7
 
8
+ # TODO for solin: add new translations
9
+
10
  def chat_with_bot_stream(user_input, conversation_history):
11
  if conversation_history is None:
12
  conversation_history = []
13
 
14
  conversation_history.append({"role": "user", "content": user_input})
15
 
16
+ # Provide an engaging system message and introduce the chatbot with example questions
17
  if len(conversation_history) == 1:
18
  conversation_history.insert(0, {
19
  "role": "system",
20
  "content": (
21
  "You are a music and genre recommendation bot designed to help users discover new music "
22
+ "based on their preferences, mood, or activity.\n\n"
23
+ "Your responses should be engaging, personalized, and insightful. You can recommend:\n\n"
24
+ "- Specific songs, albums, or artists\n"
25
+ "- Genres based on mood, activity, or past preferences\n"
26
+ "- Hidden gems and deep cuts for music enthusiasts\n"
27
+ "- Trending or classic hits based on user taste\n\n"
28
+ "Be conversational, suggest multiple options when possible, and encourage users to explore new sounds. "
29
+ "If requested, provide brief descriptions of artists or genres, and explain why a recommendation might suit the user.\n\n"
30
+ "If asked about visibility on the website, such as font sizing and themes (dark or light mode ONLY), "
31
+ "direct them to the settings tab at the top of the chatbot.\n\n"
32
+ "If asked about the languages you speak, you can say that you speak English, Spanish, and French ONLY. "
33
+ "Direct the user to the settings tab to change the language.\n\n"
34
+ "Limit your responses to music-related inquiries only. Limit your responses to 15 lines or less.\n\n"
35
+ "If you do not recognize an artist/genre/album/song name, try to clarify further, and if you still don't recognize it let the user know that you're not familiar with said artist/genre/album/song.\n\n"
36
+ "Encourage users to continue the conversation. If you suggest music, offer to refine your recommendations further, "
37
+ "such as recommending more similar artists, making a mini playlist, or adjusting based on user feedback.\n\n"
38
  )
39
  })
40
 
 
57
  (msg["content"] if msg["role"] == "user" else None,
58
  msg["content"] if msg["role"] == "assistant" else None)
59
  for msg in conversation_history
60
+ ], conversation_history
61
+
62
 
63
  # word translation
64
  translations = {
 
190
 
191
  def update_styles(theme, font_size):
192
  if theme == "Dark":
193
+ bg = "#18181B"
194
+ text_color = "#EAEAEA"
195
+ button_bg = "#56246B"
196
+ radio_bg = "#444444"
 
 
 
 
197
  border_color = "#444444"
198
  placeholder_color = "#e8e8e8"
199
+ button_text_color = "#ffffff"
200
+ chatbot_bg = "#212125"
201
+ settings_box_bg = chatbot_bg
202
  chatbot_text_color = "#ffffff"
203
  purple="#56246B"
204
  light_purple="#795699"
205
+ settings_heading_color = text_color
206
+ user_msg_bg_color="#333333"
207
+ bot_msg_bg_color="#1F1F1F"
208
+ msg_text_color="#FFFFFF"
209
  else:
210
+ bg = "#F2F2F2"
211
+ text_color = "#1C1C1C"
212
+ button_bg = "#56246B"
213
+ radio_bg = "#CCCCCC"
214
+ border_color = "#B0B0B0"
215
+ placeholder_color = "#EAEAEA"
216
+ button_text_color = "#ffffff"
217
+ chatbot_bg = "#fcfcfc"
218
+ settings_box_bg = chatbot_bg
 
 
219
  chatbot_text_color = "#000000"
220
  purple="#56246B"
221
  light_purple="#795699"
222
+ settings_heading_color = "#333333"
223
+ user_msg_bg_color="#cccccc"
224
+ bot_msg_bg_color="#e0e0e0"
225
+ msg_text_color="#000000"
226
 
227
  if font_size == "Small":
228
  fsize = "0.8rem"
 
240
  style = f"""
241
  <style id="dynamic_styles">
242
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
243
+
244
  body, .gradio-container {{
245
  background-color: {bg} !important;
246
  color: {text_color} !important;
 
268
  margin-bottom: 10px;
269
  }}
270
 
 
 
 
 
 
 
 
 
 
 
271
  #chatbot {{
272
  background-color: {chatbot_bg} !important;
273
  color: {chatbot_text_color} !important;
274
  font-size: {fsize} !important;
275
+ border: {border_color} solid 0.5px;
276
+ }}
277
+
278
+ fieldset {{
279
+ background-color: {settings_box_bg} !important;
280
+ color: {text_color} !important;
281
+ border: none;
282
+ margin: 0;
283
  }}
284
 
285
  div[role="log"] {{
286
+ border: none;
287
  background-color: {chatbot_bg} !important;
288
  color: {chatbot_text_color} !important;
289
+ font-size: {fsize} !important;
290
  }}
291
 
 
 
 
 
292
  #user_input {{
293
  background-color: {purple} !important;
294
+ border: none;
295
  height: 65px;
296
  }}
297
 
298
  #user_input textarea {{
299
  background-color: {light_purple} !important;
300
+ color: {button_text_color} !important;
301
  font-size: {fsize} !important;
302
+ border: 0.5px solid {light_purple} !important;
303
  padding: 10px;
 
304
  }}
305
 
306
  #user_input textarea::placeholder {{
 
309
  font-size: {fsize} !important;
310
  opacity: 0.73;
311
  }}
312
+
313
  #send_button {{
314
+ background-color: {button_bg} !important;
315
+ color: {button_text_color} !important;
316
  font-size: {fsize} !important;
317
+ border: none;
318
+ border-radius: 6px;
319
  margin-left: 1px;
320
  font-family: 'Inter', sans-serif;
321
+ height: 65px;
322
  display: flex;
323
  align-items: center;
324
  justify-content: center;
325
  padding: 0 10px;
326
+ transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
327
+ }}
328
+
329
+ #send_button:hover {{
330
+ background-color: {light_purple} !important;
331
+ }}
332
+
333
+ #apply_button {{
334
+ background-color: {button_bg} !important;
335
+ color: {button_text_color} !important;
336
+ font-size: {fsize} !important;
337
+ border: none;
338
+ border-radius: 6px;
339
+ width: 20%;
340
+ align-self: center;
341
+ font-family: 'Inter', sans-serif;
342
+ transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
343
  }}
344
 
345
+ #apply_button:hover {{
346
+ background-color: {light_purple} !important;
347
+ }}
348
+
349
  #settings_box {{
350
  background-color: {settings_box_bg} !important;
351
+ color: {text_color} !important;
 
352
  font-size: {fsize} !important;
353
+ border: {border_color} solid 0.5px;
354
+ border-radius: 6px;
355
+ padding: 10px;
356
+ margin: 10px;
357
  }}
358
 
359
+ h3 {{
360
+ color: {text_color} !important;
361
+ font-weight: 600;
362
+ text-align: center;
363
+ display: flex;
364
+ justify-content: center;
365
+ align-items: center;
366
+ height: 100%;
367
  font-size: {fsize} !important;
368
  }}
369
+
370
+ .form.svelte-633qhp {{
371
+ border: {border_color} solid 0.5px;
372
+ border-radius: 6px;
373
+ }}
374
+
375
+ label[data-testid] {{
376
+ background-color: {bg} !important;
377
  color: {text_color} !important;
 
378
  }}
379
+
380
+ [data-testid="block-info"] {{
381
+ color: {text_color} !important;
 
 
 
382
  }}
383
+
384
  div[role="tablist"] {{
385
  border: 1px;
386
  border-radius: 8px;
 
388
  height: auto !important;
389
  min-height: 0 !important;
390
  }}
391
+
392
  div[role="tab"] {{
393
  font-size: {fsize} !important;
394
  padding: {tab_padding} !important;
 
396
  }}
397
 
398
  button[role="tab"][aria-selected="true"]::after {{
399
+ background-color:{light_purple} !important;
400
  }}
401
 
402
  button[role="tab"][aria-selected="true"] {{
 
407
  background-color:transparent !important;
408
  color: #616161 !important;
409
  }}
410
+
411
  input[type="radio"] {{
412
  background-color: #795699 !important;
413
  }}
 
417
  border-color: #795699 !important;
418
  }}
419
 
420
+ .user.svelte-pcjl1g.message {{
421
+ background-color: {user_msg_bg_color} !important;
422
+ color: {msg_text_color} !important;
423
  }}
424
 
425
+ .bot.svelte-pcjl1g.message {{
426
+ background-color: {bot_msg_bg_color} !important;
427
+ color: {msg_text_color} !important;
428
  }}
429
 
430
+ .md.svelte-7ddecg.chatbot.prose > p {{
431
+ color: {msg_text_color} !important;
432
+ font-size: {fsize} !important;
433
+ }}
434
+
435
+ #example_questions {{
436
+ display: flex;
437
+ justify-content: center;
438
+ align-items: center;
439
+ flex-wrap: wrap;
440
+ gap: 10px;
441
+ text-align: center;
442
+ }}
443
+
444
+ #example_questions .wrap.svelte-1kzox3m {{
445
+ display: contents;
446
  }}
447
 
448
  </style>
 
457
  with gr.TabItem("💬 Chatbot"):
458
  gr.HTML('<div id="header_section"></div>')
459
  header_desc = gr.HTML('<p id="header_description">' + translations["English"]["header_description"] + '</p>')
460
+ chatbot = gr.Chatbot(label="", height=500, show_label=False, elem_id="chatbot")
461
+
462
  with gr.Row():
463
  user_input = gr.Textbox(
464
  show_label=False,
 
475
  )
476
  conversation_state = gr.State(value=[])
477
 
478
+ gr.HTML("<br>")
479
+ gr.HTML("<h3 style='text-align: center; color: {text_color};'>Need inspiration? Try asking about one of these topics:</h3>")
480
+
481
+ example_questions = gr.Radio(
482
+ choices=[
483
+ "🎵 What's some good background music for studying?",
484
+ "🔥 Which indie artists are blowing up right now?",
485
+ "🎷 Can you recommend iconic jazz albums?",
486
+ "🎶 I need a workout playlist—any suggestions?",
487
+ "💿 What classic rock albums should I listen to?",
488
+ "🎧 Who’s an underrated artist I should check out?"
489
+ ],
490
+ label="",
491
+ interactive=True,
492
+ elem_id="example_questions"
493
+ )
494
+
495
+ example_questions.change(
496
+ fn=lambda x: chat_with_bot_stream(x, conversation_state.value),
497
+ inputs=example_questions,
498
+ outputs=[chatbot, conversation_state]
499
+ )
500
+
501
  send_button.click(
502
  fn=chat_with_bot_stream,
503
  inputs=[user_input, conversation_state],
 
515
  theme_radio = gr.Radio(
516
  choices=translations["English"]["theme_choices"],
517
  value="Dark",
518
+ label=translations["English"]["theme_label"],
519
  )
520
  font_radio = gr.Radio(
521
  choices=translations["English"]["font_size_choices"],
522
  value="Medium",
523
+ label=translations["English"]["font_size_label"],
524
  )
525
  language_radio = gr.Radio(
526
  choices=translations["English"]["language_choices"],
527
  value="English",
528
+ label=translations["English"]["select_language_label"],
529
  )
530
  apply_button = gr.Button(
531
+ translations["English"]["apply_settings_button"],
532
+ elem_id="apply_button",
533
  )
534
  apply_button.click(
535
  fn=update_all_settings,
 
546
  language_radio
547
  ]
548
  )
549
+
550
+ demo.launch()