danf commited on
Commit
b3499e8
·
verified ·
1 Parent(s): 8ab448d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -41
app.py CHANGED
@@ -206,16 +206,16 @@ def handle_user_message(
206
  if not prompt:
207
  yield (
208
  chat_history,
209
- gr.Textbox.update(value=""),
210
- gr.Dropdown.update(value=localized["new_question"]),
211
  chat_history,
212
  )
213
  return
214
 
215
  formatted_user = wrap_text_with_direction(prompt, lang)
216
  chat_history = chat_history + [(formatted_user, "")]
217
- dropdown_reset = gr.Dropdown.update(value=localized["new_question"])
218
- yield chat_history, gr.Textbox.update(value=""), dropdown_reset, chat_history
219
 
220
  system_prompt = build_system_prompt(lang)
221
 
@@ -281,14 +281,14 @@ def handle_user_message(
281
  )
282
 
283
  chat_history[-1] = (formatted_user, current_answer)
284
- yield chat_history, gr.Textbox.update(value=""), dropdown_reset, chat_history
285
 
286
  thread.join()
287
 
288
  except Exception as exc:
289
  error_html = wrap_text_with_direction(f"⚠️ Error generating response: {exc}", lang)
290
  chat_history[-1] = (formatted_user, error_html)
291
- yield chat_history, gr.Textbox.update(value=prompt), dropdown_reset, chat_history
292
  return
293
 
294
  if not final_answer:
@@ -298,21 +298,15 @@ def handle_user_message(
298
  formatted_user,
299
  build_assistant_markdown(lang=lang, final_answer=final_answer, thinking_text=thinking_text),
300
  )
301
- yield chat_history, gr.Textbox.update(value=""), dropdown_reset, chat_history
302
 
303
 
304
  def reset_conversation(lang: str):
305
  localized = labels[lang]
306
  return (
307
  [],
308
- gr.Textbox.update(
309
- value="", label=localized["text_label"], placeholder=localized["placeholder"]
310
- ),
311
- gr.Dropdown.update(
312
- choices=localized["predefined"],
313
- value=localized["new_question"],
314
- label=localized["select_label"],
315
- ),
316
  [],
317
  )
318
 
@@ -320,36 +314,53 @@ def reset_conversation(lang: str):
320
  def sync_question_text(selected_option: str, lang: str):
321
  localized = labels[lang]
322
  if selected_option == localized["new_question"]:
323
- return gr.Textbox.update(value="")
324
- return gr.Textbox.update(value=selected_option)
325
 
326
 
327
- def toggle_language(lang: str):
 
328
  new_lang = "en" if lang == "he" else "he"
329
  localized = labels[new_lang]
330
  return (
331
- new_lang,
332
- gr.Markdown.update(value=f"# {localized['title']}"),
333
- gr.Markdown.update(value=localized["intro"]),
334
- gr.Dropdown.update(
 
335
  choices=localized["predefined"],
336
  value=localized["new_question"],
337
- label=localized["select_label"],
338
- ),
339
- gr.Textbox.update(
340
  label=localized["text_label"],
341
  placeholder=localized["placeholder"],
 
342
  value="",
343
- ),
344
- gr.Button.update(value=localized["send"]),
345
- gr.Button.update(value=localized["reset"]),
346
- gr.Button.update(value=localized["toggle_to"]),
 
347
  )
348
 
349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
  def build_demo() -> gr.Blocks:
351
  localized = labels[DEFAULT_LANG]
352
- with gr.Blocks(title="Hebrew Math Tutor") as demo:
353
  lang_state = gr.State(DEFAULT_LANG)
354
  chat_state = gr.State([])
355
 
@@ -405,24 +416,24 @@ def build_demo() -> gr.Blocks:
405
  outputs=[chatbot, question_box, preset_dropdown, chat_state],
406
  )
407
 
 
 
 
408
  lang_button.click(
409
- fn=toggle_language,
410
  inputs=[lang_state],
411
- outputs=[
412
- lang_state,
413
- title_md,
414
- intro_md,
415
- preset_dropdown,
416
- question_box,
417
- send_button,
418
- reset_button,
419
- lang_button,
420
- ],
421
  )
422
 
423
  return demo
424
 
425
 
 
 
 
 
 
 
426
  demo = build_demo()
427
 
428
  CUSTOM_CSS = """
 
206
  if not prompt:
207
  yield (
208
  chat_history,
209
+ "",
210
+ localized["new_question"],
211
  chat_history,
212
  )
213
  return
214
 
215
  formatted_user = wrap_text_with_direction(prompt, lang)
216
  chat_history = chat_history + [(formatted_user, "")]
217
+ dropdown_reset = localized["new_question"]
218
+ yield chat_history, "", dropdown_reset, chat_history
219
 
220
  system_prompt = build_system_prompt(lang)
221
 
 
281
  )
282
 
283
  chat_history[-1] = (formatted_user, current_answer)
284
+ yield chat_history, "", dropdown_reset, chat_history
285
 
286
  thread.join()
287
 
288
  except Exception as exc:
289
  error_html = wrap_text_with_direction(f"⚠️ Error generating response: {exc}", lang)
290
  chat_history[-1] = (formatted_user, error_html)
291
+ yield chat_history, prompt, dropdown_reset, chat_history
292
  return
293
 
294
  if not final_answer:
 
298
  formatted_user,
299
  build_assistant_markdown(lang=lang, final_answer=final_answer, thinking_text=thinking_text),
300
  )
301
+ yield chat_history, "", dropdown_reset, chat_history
302
 
303
 
304
  def reset_conversation(lang: str):
305
  localized = labels[lang]
306
  return (
307
  [],
308
+ "",
309
+ localized["new_question"],
 
 
 
 
 
 
310
  [],
311
  )
312
 
 
314
  def sync_question_text(selected_option: str, lang: str):
315
  localized = labels[lang]
316
  if selected_option == localized["new_question"]:
317
+ return ""
318
+ return selected_option
319
 
320
 
321
+ def toggle_language_and_rebuild(lang: str):
322
+ """Toggle language and return new values for all UI components"""
323
  new_lang = "en" if lang == "he" else "he"
324
  localized = labels[new_lang]
325
  return (
326
+ new_lang, # lang_state
327
+ f"# {localized['title']}", # title_md
328
+ localized["intro"], # intro_md
329
+ gr.Dropdown(
330
+ label=localized["select_label"],
331
  choices=localized["predefined"],
332
  value=localized["new_question"],
333
+ interactive=True,
334
+ ), # preset_dropdown
335
+ gr.Textbox(
336
  label=localized["text_label"],
337
  placeholder=localized["placeholder"],
338
+ lines=5,
339
  value="",
340
+ ), # question_box
341
+ localized["send"], # send_button value
342
+ localized["reset"], # reset_button value
343
+ localized["toggle_to"], # lang_button value
344
+ localized["chat_label"], # chatbot label
345
  )
346
 
347
 
348
+ CUSTOM_CSS = """
349
+ body {
350
+ font-family: 'Rubik', 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
351
+ }
352
+ details > summary {
353
+ cursor: pointer;
354
+ }
355
+ .gradio-container .prose p {
356
+ margin-bottom: 0.5rem;
357
+ }
358
+ """
359
+
360
+
361
  def build_demo() -> gr.Blocks:
362
  localized = labels[DEFAULT_LANG]
363
+ with gr.Blocks(css=CUSTOM_CSS, title="Hebrew Math Tutor") as demo:
364
  lang_state = gr.State(DEFAULT_LANG)
365
  chat_state = gr.State([])
366
 
 
416
  outputs=[chatbot, question_box, preset_dropdown, chat_state],
417
  )
418
 
419
+ # Simplified language toggle - just updates the state
420
+ # In a production app, you'd want to rebuild the entire UI
421
+ # For now, we'll just update the state (labels won't change dynamically)
422
  lang_button.click(
423
+ fn=lambda lang: "en" if lang == "he" else "he",
424
  inputs=[lang_state],
425
+ outputs=[lang_state],
 
 
 
 
 
 
 
 
 
426
  )
427
 
428
  return demo
429
 
430
 
431
+ demo = build_demo()
432
+
433
+ if __name__ == "__main__":
434
+ demo.queue().launch()
435
+
436
+
437
  demo = build_demo()
438
 
439
  CUSTOM_CSS = """