added translation code before we reverted commit cuz of api key public
Browse files
app.py
CHANGED
|
@@ -5,8 +5,6 @@ from groq import Groq
|
|
| 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 = []
|
|
@@ -35,6 +33,9 @@ def chat_with_bot_stream(user_input, conversation_history):
|
|
| 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 |
|
|
@@ -66,6 +67,7 @@ translations = {
|
|
| 66 |
"header_description": "Your own personal music discovery assistant!",
|
| 67 |
"user_input_placeholder": "Enter your music-related questions here!",
|
| 68 |
"send_button": "↗️ Send",
|
|
|
|
| 69 |
"settings_tab_item": "⚙️ Settings",
|
| 70 |
"settings_markdown": "### Settings",
|
| 71 |
"apply_settings_button": "Apply Settings",
|
|
@@ -74,12 +76,22 @@ translations = {
|
|
| 74 |
"theme_choices": ["Dark", "Light"],
|
| 75 |
"font_size_label": "🗛 Font Size",
|
| 76 |
"font_size_choices": ["Small", "Medium", "Large"],
|
| 77 |
-
"language_choices": ["English", "Español", "Français"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
},
|
| 79 |
"Spanish": {
|
| 80 |
"header_description": "¡Tu asistente personal para descubrir música!",
|
| 81 |
"user_input_placeholder": "¡Introduce tus preguntas relacionadas con la música!",
|
| 82 |
"send_button": "↗️ Enviar",
|
|
|
|
| 83 |
"settings_tab_item": "⚙️ Configuración",
|
| 84 |
"settings_markdown": "### Configuración",
|
| 85 |
"apply_settings_button": "Aplicar Configuración",
|
|
@@ -88,12 +100,22 @@ translations = {
|
|
| 88 |
"theme_choices": ["Oscuro", "Claro"],
|
| 89 |
"font_size_label": "🗛 Tamaño de Fuente",
|
| 90 |
"font_size_choices": ["Pequeño", "Mediano", "Grande"],
|
| 91 |
-
"language_choices": ["English", "Español", "Français"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
},
|
| 93 |
"French": {
|
| 94 |
"header_description": "Votre assistant personnel pour découvrir de la musique !",
|
| 95 |
"user_input_placeholder": "Entrez vos questions sur la musique ici !",
|
| 96 |
"send_button": "↗️ Envoyer",
|
|
|
|
| 97 |
"settings_tab_item": "⚙️ Paramètres",
|
| 98 |
"settings_markdown": "### Paramètres",
|
| 99 |
"apply_settings_button": "Appliquer les paramètres",
|
|
@@ -102,7 +124,16 @@ translations = {
|
|
| 102 |
"theme_choices": ["Sombre", "Clair"],
|
| 103 |
"font_size_label": "🗛 Taille de police",
|
| 104 |
"font_size_choices": ["Petit", "Moyen", "Grand"],
|
| 105 |
-
"language_choices": ["English", "Español", "Français"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
}
|
| 108 |
|
|
@@ -144,7 +175,7 @@ def update_all_settings(theme, font_size, language):
|
|
| 144 |
elif target_lang == "French":
|
| 145 |
font_for_ui = english_to_french_font.get(font_size, font_size) if font_size in english_to_french_font else font_size if font_size in french_to_english_font else "Moyen"
|
| 146 |
else:
|
| 147 |
-
font_for_ui = spanish_to_english_font.get(font_size, font_size) if font_size in spanish_to_english_font else font_size if
|
| 148 |
|
| 149 |
if theme in ["Dark", "Light"]:
|
| 150 |
theme_for_style = theme
|
|
@@ -166,14 +197,17 @@ def update_all_settings(theme, font_size, language):
|
|
| 166 |
|
| 167 |
dynamic_style = update_styles(theme_for_style, font_for_style)
|
| 168 |
|
| 169 |
-
#Languages
|
| 170 |
if target_lang == "English":
|
| 171 |
language_value = "English"
|
| 172 |
elif target_lang == "Spanish":
|
| 173 |
language_value = "Español"
|
| 174 |
elif target_lang == "French":
|
| 175 |
language_value = "Français"
|
|
|
|
| 176 |
header_html = f'<p id="header_description">{t["header_description"]}</p>'
|
|
|
|
|
|
|
| 177 |
|
| 178 |
return (
|
| 179 |
dynamic_style,
|
|
@@ -184,7 +218,10 @@ def update_all_settings(theme, font_size, language):
|
|
| 184 |
gr.update(value=t["apply_settings_button"]),
|
| 185 |
gr.update(label=t["theme_label"], choices=t["theme_choices"], value=theme_for_ui),
|
| 186 |
gr.update(label=t["font_size_label"], choices=t["font_size_choices"], value=font_for_ui),
|
| 187 |
-
gr.update(label=t["select_language_label"], choices=t["language_choices"], value=language_value)
|
|
|
|
|
|
|
|
|
|
| 188 |
)
|
| 189 |
|
| 190 |
|
|
@@ -336,7 +373,7 @@ def update_styles(theme, font_size):
|
|
| 336 |
font-size: {fsize} !important;
|
| 337 |
border: none;
|
| 338 |
border-radius: 6px;
|
| 339 |
-
width:
|
| 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;
|
|
@@ -433,12 +470,12 @@ def update_styles(theme, font_size):
|
|
| 433 |
}}
|
| 434 |
|
| 435 |
#example_questions {{
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
}}
|
| 443 |
|
| 444 |
#example_questions .wrap.svelte-1kzox3m {{
|
|
@@ -463,7 +500,7 @@ def load_chat_history(chat_history):
|
|
| 463 |
# Gradio stuff
|
| 464 |
with gr.Blocks(css=None) as demo:
|
| 465 |
dynamic_styles = gr.HTML(value=update_styles("Dark", "Medium"))
|
| 466 |
-
|
| 467 |
with gr.Tabs():
|
| 468 |
with gr.TabItem("💬 Chatbot"):
|
| 469 |
gr.HTML('<div id="header_section"></div>')
|
|
@@ -472,7 +509,7 @@ with gr.Blocks(css=None) as demo:
|
|
| 472 |
|
| 473 |
with gr.Row():
|
| 474 |
user_input = gr.Textbox(
|
| 475 |
-
show_label=False,
|
| 476 |
lines=1,
|
| 477 |
placeholder=translations["English"]["user_input_placeholder"],
|
| 478 |
interactive=True,
|
|
@@ -484,23 +521,22 @@ with gr.Blocks(css=None) as demo:
|
|
| 484 |
elem_id="send_button",
|
| 485 |
scale=2
|
| 486 |
)
|
| 487 |
-
clear_button = gr.Button(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
|
| 489 |
conversation_state = gr.BrowserState([])
|
| 490 |
demo.load(load_chat_history, inputs=[conversation_state], outputs=[chatbot])
|
| 491 |
|
| 492 |
gr.HTML("<br>")
|
| 493 |
-
|
| 494 |
-
|
|
|
|
|
|
|
| 495 |
example_questions = gr.Radio(
|
| 496 |
-
choices=[
|
| 497 |
-
"🎵 What's some good background music for studying?",
|
| 498 |
-
"🔥 Which indie artists are blowing up right now?",
|
| 499 |
-
"🎷 Can you recommend iconic jazz albums?",
|
| 500 |
-
"🎶 I need a workout playlist—any suggestions?",
|
| 501 |
-
"💿 What classic rock albums should I listen to?",
|
| 502 |
-
"🎧 Who’s an underrated artist I should check out?"
|
| 503 |
-
],
|
| 504 |
label="",
|
| 505 |
interactive=True,
|
| 506 |
elem_id="example_questions"
|
|
@@ -537,7 +573,7 @@ with gr.Blocks(css=None) as demo:
|
|
| 537 |
inputs=None,
|
| 538 |
outputs=[chatbot, conversation_state]
|
| 539 |
)
|
| 540 |
-
|
| 541 |
# SETTINGS PAGE
|
| 542 |
with gr.TabItem(translations["English"]["settings_tab_item"]):
|
| 543 |
with gr.Column(elem_id="settings_box"):
|
|
@@ -573,7 +609,10 @@ with gr.Blocks(css=None) as demo:
|
|
| 573 |
apply_button,
|
| 574 |
theme_radio,
|
| 575 |
font_radio,
|
| 576 |
-
language_radio
|
|
|
|
|
|
|
|
|
|
| 577 |
]
|
| 578 |
)
|
| 579 |
|
|
|
|
| 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 = []
|
|
|
|
| 33 |
"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"
|
| 34 |
"Encourage users to continue the conversation. If you suggest music, offer to refine your recommendations further, "
|
| 35 |
"such as recommending more similar artists, making a mini playlist, or adjusting based on user feedback.\n\n"
|
| 36 |
+
"If you're unable to provide a recommendation, let the user know and suggest they try rephrasing their question.\n\n"
|
| 37 |
+
"Remember to always be polite, engaging, and helpful. Make sure to keep the conversation flowing and the user engaged."
|
| 38 |
+
"Make sure to match the user's tone and energy, and always be respectful and professional."
|
| 39 |
)
|
| 40 |
})
|
| 41 |
|
|
|
|
| 67 |
"header_description": "Your own personal music discovery assistant!",
|
| 68 |
"user_input_placeholder": "Enter your music-related questions here!",
|
| 69 |
"send_button": "↗️ Send",
|
| 70 |
+
"clear_button": "🗑️ Clear",
|
| 71 |
"settings_tab_item": "⚙️ Settings",
|
| 72 |
"settings_markdown": "### Settings",
|
| 73 |
"apply_settings_button": "Apply Settings",
|
|
|
|
| 76 |
"theme_choices": ["Dark", "Light"],
|
| 77 |
"font_size_label": "🗛 Font Size",
|
| 78 |
"font_size_choices": ["Small", "Medium", "Large"],
|
| 79 |
+
"language_choices": ["English", "Español", "Français"],
|
| 80 |
+
"inspiration_heading": "Need inspiration? Try asking about one of these topics:",
|
| 81 |
+
"example_questions": [
|
| 82 |
+
"🎵 What's some good background music for studying?",
|
| 83 |
+
"🔥 Which indie artists are blowing up right now?",
|
| 84 |
+
"🎷 Can you recommend iconic jazz albums?",
|
| 85 |
+
"🎶 I need a workout playlist—any suggestions?",
|
| 86 |
+
"💿 What classic rock albums should I listen to?",
|
| 87 |
+
"🎧 Who’s an underrated artist I should check out?"
|
| 88 |
+
]
|
| 89 |
},
|
| 90 |
"Spanish": {
|
| 91 |
"header_description": "¡Tu asistente personal para descubrir música!",
|
| 92 |
"user_input_placeholder": "¡Introduce tus preguntas relacionadas con la música!",
|
| 93 |
"send_button": "↗️ Enviar",
|
| 94 |
+
"clear_button": "🗑️ Limpiar",
|
| 95 |
"settings_tab_item": "⚙️ Configuración",
|
| 96 |
"settings_markdown": "### Configuración",
|
| 97 |
"apply_settings_button": "Aplicar Configuración",
|
|
|
|
| 100 |
"theme_choices": ["Oscuro", "Claro"],
|
| 101 |
"font_size_label": "🗛 Tamaño de Fuente",
|
| 102 |
"font_size_choices": ["Pequeño", "Mediano", "Grande"],
|
| 103 |
+
"language_choices": ["English", "Español", "Français"],
|
| 104 |
+
"inspiration_heading": "¿Necesitas inspiración? Prueba preguntar sobre uno de estos temas:",
|
| 105 |
+
"example_questions": [
|
| 106 |
+
"🎵 ¿Qué buena música ambiental recomiendas para estudiar?",
|
| 107 |
+
"🔥 ¿Qué artistas indie están en auge ahora mismo?",
|
| 108 |
+
"🎷 ¿Puedes recomendar álbumes icónicos de jazz?",
|
| 109 |
+
"🎶 Necesito una lista de reproducción para entrenar, ¿alguna sugerencia?",
|
| 110 |
+
"💿 ¿Qué álbumes de rock clásico debería escuchar?",
|
| 111 |
+
"🎧 ¿Qué artista poco reconocido debería escuchar?"
|
| 112 |
+
]
|
| 113 |
},
|
| 114 |
"French": {
|
| 115 |
"header_description": "Votre assistant personnel pour découvrir de la musique !",
|
| 116 |
"user_input_placeholder": "Entrez vos questions sur la musique ici !",
|
| 117 |
"send_button": "↗️ Envoyer",
|
| 118 |
+
"clear_button": "🗑️ Effacer",
|
| 119 |
"settings_tab_item": "⚙️ Paramètres",
|
| 120 |
"settings_markdown": "### Paramètres",
|
| 121 |
"apply_settings_button": "Appliquer les paramètres",
|
|
|
|
| 124 |
"theme_choices": ["Sombre", "Clair"],
|
| 125 |
"font_size_label": "🗛 Taille de police",
|
| 126 |
"font_size_choices": ["Petit", "Moyen", "Grand"],
|
| 127 |
+
"language_choices": ["English", "Español", "Français"],
|
| 128 |
+
"inspiration_heading": "Besoin d'inspiration ? Essayez de poser l'une de ces questions :",
|
| 129 |
+
"example_questions": [
|
| 130 |
+
"🎵 Quelle bonne musique d'ambiance pour étudier ?",
|
| 131 |
+
"🔥 Quels artistes indie font sensation en ce moment ?",
|
| 132 |
+
"🎷 Peux-tu recommander des albums de jazz emblématiques ?",
|
| 133 |
+
"�� J'ai besoin d'une playlist pour m'entraîner, des suggestions ?",
|
| 134 |
+
"💿 Quels albums de rock classique devrais-je écouter ?",
|
| 135 |
+
"🎧 Quel artiste sous-estimé devrais-je découvrir ?"
|
| 136 |
+
]
|
| 137 |
}
|
| 138 |
}
|
| 139 |
|
|
|
|
| 175 |
elif target_lang == "French":
|
| 176 |
font_for_ui = english_to_french_font.get(font_size, font_size) if font_size in english_to_french_font else font_size if font_size in french_to_english_font else "Moyen"
|
| 177 |
else:
|
| 178 |
+
font_for_ui = spanish_to_english_font.get(font_size, font_size) if font_size in spanish_to_english_font else font_size if font_size in english_to_spanish_font else "Medium"
|
| 179 |
|
| 180 |
if theme in ["Dark", "Light"]:
|
| 181 |
theme_for_style = theme
|
|
|
|
| 197 |
|
| 198 |
dynamic_style = update_styles(theme_for_style, font_for_style)
|
| 199 |
|
| 200 |
+
# Languages
|
| 201 |
if target_lang == "English":
|
| 202 |
language_value = "English"
|
| 203 |
elif target_lang == "Spanish":
|
| 204 |
language_value = "Español"
|
| 205 |
elif target_lang == "French":
|
| 206 |
language_value = "Français"
|
| 207 |
+
|
| 208 |
header_html = f'<p id="header_description">{t["header_description"]}</p>'
|
| 209 |
+
clear_button_text = t["clear_button"]
|
| 210 |
+
inspiration_html = f'<h3 style="text-align: center;">{t["inspiration_heading"]}</h3>'
|
| 211 |
|
| 212 |
return (
|
| 213 |
dynamic_style,
|
|
|
|
| 218 |
gr.update(value=t["apply_settings_button"]),
|
| 219 |
gr.update(label=t["theme_label"], choices=t["theme_choices"], value=theme_for_ui),
|
| 220 |
gr.update(label=t["font_size_label"], choices=t["font_size_choices"], value=font_for_ui),
|
| 221 |
+
gr.update(label=t["select_language_label"], choices=t["language_choices"], value=language_value),
|
| 222 |
+
gr.update(value=clear_button_text),
|
| 223 |
+
gr.update(value=inspiration_html),
|
| 224 |
+
gr.update(choices=t["example_questions"])
|
| 225 |
)
|
| 226 |
|
| 227 |
|
|
|
|
| 373 |
font-size: {fsize} !important;
|
| 374 |
border: none;
|
| 375 |
border-radius: 6px;
|
| 376 |
+
width: 13%;
|
| 377 |
align-self: center;
|
| 378 |
font-family: 'Inter', sans-serif;
|
| 379 |
transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
|
|
|
|
| 470 |
}}
|
| 471 |
|
| 472 |
#example_questions {{
|
| 473 |
+
display: flex;
|
| 474 |
+
justify-content: center;
|
| 475 |
+
align-items: center;
|
| 476 |
+
flex-wrap: wrap;
|
| 477 |
+
gap: 10px;
|
| 478 |
+
text-align: center;
|
| 479 |
}}
|
| 480 |
|
| 481 |
#example_questions .wrap.svelte-1kzox3m {{
|
|
|
|
| 500 |
# Gradio stuff
|
| 501 |
with gr.Blocks(css=None) as demo:
|
| 502 |
dynamic_styles = gr.HTML(value=update_styles("Dark", "Medium"))
|
| 503 |
+
|
| 504 |
with gr.Tabs():
|
| 505 |
with gr.TabItem("💬 Chatbot"):
|
| 506 |
gr.HTML('<div id="header_section"></div>')
|
|
|
|
| 509 |
|
| 510 |
with gr.Row():
|
| 511 |
user_input = gr.Textbox(
|
| 512 |
+
show_label=False,
|
| 513 |
lines=1,
|
| 514 |
placeholder=translations["English"]["user_input_placeholder"],
|
| 515 |
interactive=True,
|
|
|
|
| 521 |
elem_id="send_button",
|
| 522 |
scale=2
|
| 523 |
)
|
| 524 |
+
clear_button = gr.Button(
|
| 525 |
+
value=translations["English"]["clear_button"],
|
| 526 |
+
elem_id="send_button",
|
| 527 |
+
scale=2
|
| 528 |
+
)
|
| 529 |
|
| 530 |
conversation_state = gr.BrowserState([])
|
| 531 |
demo.load(load_chat_history, inputs=[conversation_state], outputs=[chatbot])
|
| 532 |
|
| 533 |
gr.HTML("<br>")
|
| 534 |
+
# Create a new HTML element for the inspiration heading using translations
|
| 535 |
+
inspiration_heading = gr.HTML('<h3 style="text-align: center;">' + translations["English"]["inspiration_heading"] + '</h3>')
|
| 536 |
+
gr.HTML("<br>")
|
| 537 |
+
|
| 538 |
example_questions = gr.Radio(
|
| 539 |
+
choices=translations["English"]["example_questions"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
label="",
|
| 541 |
interactive=True,
|
| 542 |
elem_id="example_questions"
|
|
|
|
| 573 |
inputs=None,
|
| 574 |
outputs=[chatbot, conversation_state]
|
| 575 |
)
|
| 576 |
+
|
| 577 |
# SETTINGS PAGE
|
| 578 |
with gr.TabItem(translations["English"]["settings_tab_item"]):
|
| 579 |
with gr.Column(elem_id="settings_box"):
|
|
|
|
| 609 |
apply_button,
|
| 610 |
theme_radio,
|
| 611 |
font_radio,
|
| 612 |
+
language_radio,
|
| 613 |
+
clear_button,
|
| 614 |
+
inspiration_heading,
|
| 615 |
+
example_questions
|
| 616 |
]
|
| 617 |
)
|
| 618 |
|