Fasty commited on
Commit
9ddddcb
·
1 Parent(s): 8c3d3ac

added accessibility settings, translations are in code in a dict

Browse files
Files changed (1) hide show
  1. tempCodeRunnerFile.py +418 -0
tempCodeRunnerFile.py ADDED
@@ -0,0 +1,418 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from groq import Groq
4
+
5
+ api_key = os.getenv("GROQ_API_KEY")
6
+ client = Groq(api_key=api_key)
7
+
8
+ conversation_history = []
9
+
10
+ def chat_with_bot_stream(user_input):
11
+ global conversation_history
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
+ )
27
+ })
28
+
29
+ completion = client.chat.completions.create(
30
+ model="llama3-70b-8192",
31
+ messages=conversation_history,
32
+ temperature=1,
33
+ max_tokens=1024,
34
+ top_p=1,
35
+ stream=True,
36
+ )
37
+
38
+ response_content = ""
39
+ for chunk in completion:
40
+ response_content += chunk.choices[0].delta.content or ""
41
+
42
+ conversation_history.append({"role": "assistant", "content": response_content})
43
+
44
+ return [
45
+ (msg["content"] if msg["role"] == "user" else None,
46
+ msg["content"] if msg["role"] == "assistant" else None)
47
+ for msg in conversation_history
48
+ ]
49
+
50
+ # word translation
51
+ translations = {
52
+ "English": {
53
+ "header_description": "Your own personal music discovery assistant!",
54
+ "user_input_placeholder": "Enter your music-related questions here!",
55
+ "send_button": "Send",
56
+ "settings_markdown": "### Settings",
57
+ "apply_settings_button": "Apply Settings",
58
+ "select_language_label": "🌐 Language",
59
+ "theme_label": "Theme",
60
+ "theme_choices": ["Dark", "Light"],
61
+ "font_size_label": "Font Size",
62
+ "font_size_choices": ["Small", "Medium", "Large"],
63
+ "language_choices": ["English", "Spanish", "French"]
64
+ },
65
+ "Spanish": {
66
+ "header_description": "¡Tu asistente personal para descubrir música!",
67
+ "user_input_placeholder": "¡Introduce tus preguntas relacionadas con la música!",
68
+ "send_button": "Enviar",
69
+ "settings_markdown": "### Configuración",
70
+ "apply_settings_button": "Aplicar Configuración",
71
+ "select_language_label": "🌐 Idioma",
72
+ "theme_label": "Tema",
73
+ "theme_choices": ["Oscuro", "Claro"],
74
+ "font_size_label": "Tamaño de Fuente",
75
+ "font_size_choices": ["Pequeño", "Mediano", "Grande"],
76
+ "language_choices": ["Inglés", "Español", "Francés"]
77
+ },
78
+ "French": {
79
+ "header_description": "Votre assistant personnel pour découvrir de la musique !",
80
+ "user_input_placeholder": "Entrez vos questions sur la musique ici !",
81
+ "send_button": "Envoyer",
82
+ "settings_markdown": "### Paramètres",
83
+ "apply_settings_button": "Appliquer les paramètres",
84
+ "select_language_label": "🌐 Langue",
85
+ "theme_label": "Thème",
86
+ "theme_choices": ["Sombre", "Clair"],
87
+ "font_size_label": "Taille de police",
88
+ "font_size_choices": ["Petit", "Moyen", "Grand"],
89
+ "language_choices": ["Anglais", "Espagnol", "Français"]
90
+ }
91
+ }
92
+
93
+ # translations for theme
94
+ english_to_spanish_theme = {"Dark": "Oscuro", "Light": "Claro"}
95
+ spanish_to_english_theme = {"Oscuro": "Dark", "Claro": "Light"}
96
+ english_to_french_theme = {"Dark": "Sombre", "Light": "Clair"}
97
+ french_to_english_theme = {"Sombre": "Dark", "Clair": "Light"}
98
+
99
+ english_to_spanish_font = {"Small": "Pequeño", "Medium": "Mediano", "Large": "Grande"}
100
+ spanish_to_english_font = {"Pequeño": "Small", "Mediano": "Medium", "Grande": "Large"}
101
+ english_to_french_font = {"Small": "Petit", "Medium": "Moyen", "Large": "Grand"}
102
+ french_to_english_font = {"Petit": "Small", "Moyen": "Medium", "Grand": "Large"}
103
+
104
+ # I wanted to include the c in Fraincais and the spanish e and n in Spanish
105
+ def normalize_language(lang):
106
+ if lang in ["English", "Inglés", "Anglais"]:
107
+ return "English"
108
+ elif lang in ["Spanish", "Español", "Espagnol"]:
109
+ return "Spanish"
110
+ elif lang in ["French", "Français"]:
111
+ return "French"
112
+ else:
113
+ return "English"
114
+
115
+ def update_all_settings(theme, font_size, language):
116
+ target_lang = normalize_language(language)
117
+ t = translations[target_lang]
118
+
119
+ if target_lang == "Spanish":
120
+ theme_for_ui = english_to_spanish_theme.get(theme, theme) if theme in english_to_spanish_theme else theme if theme in spanish_to_english_theme else "Oscuro"
121
+ elif target_lang == "French":
122
+ theme_for_ui = english_to_french_theme.get(theme, theme) if theme in english_to_french_theme else theme if theme in french_to_english_theme else "Sombre"
123
+ else: # target is English
124
+ theme_for_ui = spanish_to_english_theme.get(theme, theme) if theme in spanish_to_english_theme else theme if theme in english_to_spanish_theme else "Dark"
125
+
126
+ if target_lang == "Spanish":
127
+ font_for_ui = english_to_spanish_font.get(font_size, font_size) if font_size in english_to_spanish_font else font_size if font_size in spanish_to_english_font else "Mediano"
128
+ elif target_lang == "French":
129
+ 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"
130
+ else:
131
+ font_for_ui = spanish_to_english_font.get(font_size, font_size) if font_size in spanish_to_english_font else font_size if theme in english_to_spanish_font else "Medium"
132
+
133
+ if theme in ["Dark", "Light"]:
134
+ theme_for_style = theme
135
+ elif theme in spanish_to_english_theme:
136
+ theme_for_style = spanish_to_english_theme.get(theme, "Dark")
137
+ elif theme in french_to_english_theme:
138
+ theme_for_style = french_to_english_theme.get(theme, "Dark")
139
+ else:
140
+ theme_for_style = "Dark"
141
+
142
+ if font_size in ["Small", "Medium", "Large"]:
143
+ font_for_style = font_size
144
+ elif font_size in spanish_to_english_font:
145
+ font_for_style = spanish_to_english_font.get(font_size, "Medium")
146
+ elif font_size in french_to_english_font:
147
+ font_for_style = french_to_english_font.get(font_size, "Medium")
148
+ else:
149
+ font_for_style = "Medium"
150
+
151
+ dynamic_style = update_styles(theme_for_style, font_for_style)
152
+
153
+ #Languages
154
+ if target_lang == "English":
155
+ language_value = "English"
156
+ elif target_lang == "Spanish":
157
+ language_value = "Español"
158
+ elif target_lang == "French":
159
+ language_value = "Français"
160
+ header_html = f'<p id="header_description">{t["header_description"]}</p>'
161
+
162
+ return (
163
+ dynamic_style,
164
+ gr.update(value=header_html),
165
+ gr.update(placeholder=t["user_input_placeholder"]),
166
+ gr.update(value=t["send_button"]),
167
+ gr.update(value=t["settings_markdown"]),
168
+ gr.update(value=t["apply_settings_button"]),
169
+ gr.update(label=t["theme_label"], choices=t["theme_choices"], value=theme_for_ui),
170
+ gr.update(label=t["font_size_label"], choices=t["font_size_choices"], value=font_for_ui),
171
+ gr.update(label=t["select_language_label"], choices=t["language_choices"], value=language_value)
172
+ )
173
+
174
+
175
+ # DARK ADN LIGHT MODE
176
+ def update_styles(theme, font_size):
177
+ if theme == "Dark":
178
+ bg = "#18181B"
179
+ text_color = "#ffffff"
180
+ input_bg = "#AFA79F"
181
+ button_bg = "#56246B"
182
+ settings_box_bg = "#555555"
183
+ apply_button_bg = "#666666"
184
+ radio_bg = "#555555"
185
+ settings_heading_color = text_color
186
+ else:
187
+ bg = "#ffffff"
188
+ text_color = "#000000"
189
+ input_bg = "#f2f2f2"
190
+ button_bg = "#dddddd"
191
+ settings_box_bg = "#eeeeee"
192
+ apply_button_bg = "#e0e0e0"
193
+ radio_bg = "#e0e0e0"
194
+ settings_heading_color = "#333333"
195
+
196
+ # Font, still havent decided if i want a slider but this is cleaner design in my op
197
+ if font_size == "Small":
198
+ fsize = "0.8rem"
199
+ tab_padding = "4px 8px"
200
+ elif font_size == "Medium":
201
+ fsize = "1.2rem"
202
+ tab_padding = "6px 12px"
203
+ elif font_size == "Large":
204
+ fsize = "1.8rem"
205
+ tab_padding = "8px 16px"
206
+ else:
207
+ fsize = "1rem"
208
+ tab_padding = "5px 10px"
209
+
210
+ style = f"""
211
+ <style id="dynamic_styles">
212
+
213
+ body, .gradio-container {{
214
+ background-color: {bg} !important;
215
+ color: {text_color} !important;
216
+ font-size: {fsize} !important;
217
+ }}
218
+
219
+ #header_description {{
220
+ color: {text_color} !important;
221
+ font-size: {fsize} !important;
222
+ }}
223
+
224
+ #chatbot {{
225
+ background-color: {button_bg} !important;
226
+ font-size: {fsize} !important;
227
+ }}
228
+ #chatbot .chatbot-message,
229
+ #chatbot .message {{
230
+ background-color: {button_bg} !important;
231
+ color: {text_color} !important;
232
+ font-size: {fsize} !important;
233
+ }}
234
+
235
+ #user_input {{
236
+ background-color: {button_bg} !important;
237
+ }}
238
+ #user_input textarea {{
239
+ background-color: {input_bg} !important;
240
+ color: {text_color} !important;
241
+ font-size: {fsize} !important;
242
+ }}
243
+ #user_input textarea::placeholder {{
244
+ color: {text_color} !important;
245
+ font-size: {fsize} !important;
246
+ }}
247
+
248
+ #send_button {{
249
+ background-color: {button_bg} !important;
250
+ color: {text_color} !important;
251
+ font-size: {fsize} !important;
252
+ }}
253
+
254
+ #settings_box {{
255
+ background-color: {settings_box_bg} !important;
256
+ padding: 10px;
257
+ border-radius: 8px;
258
+ font-size: {fsize} !important;
259
+ }}
260
+ #settings_box .gr-markdown h3 {{
261
+ color: {settings_heading_color} !important;
262
+ font-size: {fsize} !important;
263
+ }}
264
+
265
+ #settings_box .gr-radio-group,
266
+ #settings_box .gr-radio-group * {{
267
+ background-color: {radio_bg} !important;
268
+ color: {text_color} !important;
269
+ font-size: {fsize} !important;
270
+ }}
271
+
272
+ /* Apply Settings button */
273
+ #apply_settings_button {{
274
+ background-color: {apply_button_bg} !important;
275
+ color: {text_color} !important;
276
+ font-size: {fsize} !important;
277
+ }}
278
+
279
+ div[role="tablist"] {{
280
+ background-color: #8A2BE2 !important;
281
+ border-radius: 8px;
282
+ padding: {tab_padding} !important;
283
+ height: auto !important;
284
+ min-height: 0 !important;
285
+ }}
286
+ div[role="tab"] {{
287
+ font-size: {fsize} !important;
288
+ padding: {tab_padding} !important;
289
+ }}
290
+ </style>
291
+ """
292
+ return style
293
+
294
+ custom_css = """
295
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
296
+ body, .gradio-container {
297
+ margin: 0;
298
+ padding: 0;
299
+ font-family: 'Inter', sans-serif;
300
+ }
301
+ #header_section {
302
+ width: 400px;
303
+ height: 100px;
304
+ background: url("https://huggingface.co/spaces/MusifyLTD/Musify/resolve/main/logo.png")
305
+ no-repeat center center;
306
+ background-size: contain;
307
+ margin: 0px auto;
308
+ margin-bottom: -10px;
309
+ }
310
+ #header_description {
311
+ text-align: center;
312
+ font-size: 20px;
313
+ margin-top: -10px;
314
+ margin-bottom: 10px;
315
+ }
316
+ #user_input textarea {
317
+ border: none;
318
+ padding: 10px;
319
+ font-size: 1rem;
320
+ }
321
+ #user_input textarea::placeholder {
322
+ font-size: 1rem;
323
+ opacity: 0.73;
324
+ }
325
+ #user_input {
326
+ height: 65px;
327
+ border: none;
328
+ }
329
+ #send_button {
330
+ margin-left: 1px;
331
+ border: none;
332
+ font-family: 'Inter', sans-serif;
333
+ height: 67px;
334
+ display: flex;
335
+ align-items: center;
336
+ justify-content: center;
337
+ padding: 0 10px;
338
+ font-size: 1rem;
339
+ }
340
+ div[role="tablist"] {
341
+ background-color: #8A2BE2 !important;
342
+ border-radius: 8px;
343
+ padding: 5px;
344
+ }
345
+ """
346
+ # Gradio build stuff actually starts here
347
+ with gr.Blocks(css=custom_css) as demo:
348
+ dynamic_styles = gr.HTML(value=update_styles("Dark", "Medium"))
349
+
350
+ with gr.Tabs():
351
+ with gr.TabItem("💬 Chatbot"):
352
+ gr.HTML('<div id="header_section"></div>')
353
+ header_desc = gr.HTML('<p id="header_description">' + translations["English"]["header_description"] + '</p>')
354
+ chatbot = gr.Chatbot(label="", height=400, show_label=False, elem_id="chatbot")
355
+ with gr.Row():
356
+ user_input = gr.Textbox(
357
+ show_label=False,
358
+ lines=1,
359
+ placeholder=translations["English"]["user_input_placeholder"],
360
+ interactive=True,
361
+ elem_id="user_input",
362
+ scale=15
363
+ )
364
+ send_button = gr.Button(
365
+ value=translations["English"]["send_button"],
366
+ elem_id="send_button",
367
+ scale=1
368
+ )
369
+ send_button.click(
370
+ fn=chat_with_bot_stream,
371
+ inputs=user_input,
372
+ outputs=chatbot
373
+ ).then(
374
+ fn=lambda: "",
375
+ inputs=None,
376
+ outputs=user_input
377
+ )
378
+
379
+ # SETTINGS PAGE
380
+ with gr.TabItem("⚙️ Settings"):
381
+ with gr.Column(elem_id="settings_box"):
382
+ settings_md = gr.Markdown(translations["English"]["settings_markdown"])
383
+ theme_radio = gr.Radio(
384
+ choices=translations["English"]["theme_choices"],
385
+ value="Dark",
386
+ label=translations["English"]["theme_label"]
387
+ )
388
+ font_radio = gr.Radio(
389
+ choices=translations["English"]["font_size_choices"],
390
+ value="Medium",
391
+ label=translations["English"]["font_size_label"]
392
+ )
393
+ language_radio = gr.Radio(
394
+ choices=translations["English"]["language_choices"],
395
+ value="English",
396
+ label=translations["English"]["select_language_label"]
397
+ )
398
+ apply_button = gr.Button(
399
+ translations["English"]["apply_settings_button"],
400
+ elem_id="apply_settings_button"
401
+ )
402
+ apply_button.click(
403
+ fn=update_all_settings,
404
+ inputs=[theme_radio, font_radio, language_radio],
405
+ outputs=[
406
+ dynamic_styles,
407
+ header_desc,
408
+ user_input,
409
+ send_button,
410
+ settings_md,
411
+ apply_button,
412
+ theme_radio,
413
+ font_radio,
414
+ language_radio
415
+ ]
416
+ )
417
+
418
+ demo.launch()