shayekh commited on
Commit
cf74ad5
Β·
verified Β·
1 Parent(s): 84e279c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -14
app.py CHANGED
@@ -454,16 +454,10 @@ CRITICAL: Do NOT overthink. Do NOT deliberate over conditions, edge cases, or re
454
  output_text += new_text2
455
  yield output_text, None
456
 
457
- # Hard auto-force: if total output exceeds 10K chars and
458
- # the text after the last ```json prefix has no closing ] or },
459
- # the model is still rambling β€” force again automatically.
460
- last_json_start = output_text.rfind('```json')
461
- text_after_json = output_text[last_json_start:] if last_json_start >= 0 else ''
462
- has_json_end = (']' in text_after_json or '}' in text_after_json) if text_after_json else False
463
- should_hard_force = (
464
- len(output_text) > HARD_FORCE_CHARS
465
- and not has_json_end
466
- )
467
 
468
  # Allow user to force again OR hard auto-force kicks in
469
  if global_stop_thinking[0] or global_kill_threads[0] or should_hard_force:
@@ -1654,6 +1648,39 @@ def import_deck_fn(json_file, anki_file):
1654
  return "<p style='color:#94a3b8;padding:20px;text-align:center;font-family:Outfit,sans-serif;'>⚠️ Please upload a JSON or Anki (.apkg) file above.</p>", None
1655
 
1656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1657
  def create_demo():
1658
  example_pdf = get_example_pdf()
1659
  example_audio = get_example_audio()
@@ -1831,7 +1858,8 @@ def create_demo():
1831
  with gr.Column(scale=2):
1832
  with gr.Tabs() as output_tabs:
1833
  with gr.Tab("πŸ“– Flashcards"):
1834
- output_html = gr.HTML(label="Flashcards will appear here")
 
1835
  gr.Markdown("**Export current deck:**")
1836
  with gr.Row():
1837
  export_json_btn = gr.Button("πŸ“₯ Export JSON", variant="secondary", size="sm")
@@ -1842,17 +1870,18 @@ def create_demo():
1842
  with gr.Tab("❓ Quiz"):
1843
  gr.Markdown("**Test your knowledge** with a randomized 5-question multiple-choice quiz from the current deck.")
1844
  start_quiz_btn = gr.Button("πŸ§ͺ Start 5-Question Quiz", variant="primary")
1845
- quiz_output_html = gr.HTML(label="Quiz")
 
1846
 
1847
  stream_box = gr.Textbox(label="Live Model Generation 🧠", lines=10, max_lines=20, interactive=False, autoscroll=True, elem_id="stream_box")
1848
 
1849
  with gr.Accordion("πŸ“„ Extracted Source Content", open=True):
1850
- extracted_text_box = gr.Textbox(label="Extracted Text", lines=10, max_lines=15, interactive=False)
1851
  extracted_images_gallery = gr.Gallery(label="Extracted Images", columns=4, height="auto", object_fit="contain")
1852
  extracted_audio_player = gr.Audio(label="Extracted Audio (YouTube / Uploaded)", type="filepath", interactive=False)
1853
 
1854
  last_source_state = gr.State(None)
1855
- last_korean_words_state = gr.State(None)
1856
 
1857
  def reset_btn_text():
1858
  return gr.update(value="⚑ Stop thinking, Generate now"), gr.update(value="πŸ›‘ Stop Generation")
 
454
  output_text += new_text2
455
  yield output_text, None
456
 
457
+ # Hard auto-force: if total output exceeds 10K chars,
458
+ # the model has been rambling too long β€” force again regardless
459
+ # of JSON state (the model may complete JSON then start over).
460
+ should_hard_force = len(output_text) > HARD_FORCE_CHARS
 
 
 
 
 
 
461
 
462
  # Allow user to force again OR hard auto-force kicks in
463
  if global_stop_thinking[0] or global_kill_threads[0] or should_hard_force:
 
1648
  return "<p style='color:#94a3b8;padding:20px;text-align:center;font-family:Outfit,sans-serif;'>⚠️ Please upload a JSON or Anki (.apkg) file above.</p>", None
1649
 
1650
 
1651
+ # ─── Bootstrap demo content (shown before any generation) ───
1652
+ BOOTSTRAP_VOCAB = [
1653
+ {"korean": "μ•ˆλ…•ν•˜μ„Έμš”", "translation": "Hello", "transliteration": "an-nyeong-ha-se-yo", "explanation": "The most common formal greeting in Korean, used when meeting someone.", "audio_uri": None},
1654
+ {"korean": "κ°μ‚¬ν•©λ‹ˆλ‹€", "translation": "Thank you", "transliteration": "gam-sa-ham-ni-da", "explanation": "The standard polite way to express gratitude.", "audio_uri": None},
1655
+ {"korean": "μ‚¬λž‘", "translation": "Love", "transliteration": "sa-rang", "explanation": "A fundamental word for love or affection, used in many K-pop songs.", "audio_uri": None},
1656
+ {"korean": "학ꡐ", "translation": "School", "transliteration": "hak-gyo", "explanation": "Refers to a school or educational institution. ν•™ means 'study', ꡐ means 'teach'.", "audio_uri": None},
1657
+ {"korean": "μŒμ‹", "translation": "Food", "transliteration": "eum-sik", "explanation": "General word for food or cuisine. Korean μŒμ‹ (food) is world-famous!", "audio_uri": None},
1658
+ {"korean": "친ꡬ", "translation": "Friend", "transliteration": "chin-gu", "explanation": "Means a friend or buddy. In Korean culture, 친ꡬ specifically refers to someone the same age.", "audio_uri": None},
1659
+ {"korean": "λ¬Ό", "translation": "Water", "transliteration": "mul", "explanation": "Essential vocabulary β€” λ¬Ό μ£Όμ„Έμš” (mul ju-se-yo) means 'Water, please'.", "audio_uri": None},
1660
+ {"korean": "μ‹œκ°„", "translation": "Time", "transliteration": "si-gan", "explanation": "Means time or hour. μ‹œ (si) = hour, κ°„ (gan) = interval.", "audio_uri": None},
1661
+ {"korean": "행볡", "translation": "Happiness", "transliteration": "haeng-bok", "explanation": "Means happiness or bliss. ν–‰λ³΅ν•˜λ‹€ (haeng-bok-ha-da) = to be happy.", "audio_uri": None},
1662
+ {"korean": "μ—¬ν–‰", "translation": "Travel", "transliteration": "yeo-haeng", "explanation": "Means travel or trip. μ—¬ν–‰ν•˜λ‹€ (yeo-haeng-ha-da) = to travel.", "audio_uri": None},
1663
+ ]
1664
+
1665
+ DEMO_EXTRACTED_TEXT = """[Demo Content β€” Common Korean Words]
1666
+
1667
+ μ•ˆλ…•ν•˜μ„Έμš”! μ˜€λŠ˜μ€ ν•œκ΅­μ–΄λ₯Ό λ°°μ›Œλ΄…μ‹œλ‹€.
1668
+ (Hello! Let's learn Korean today.)
1669
+
1670
+ ν•œκ΅­ μŒμ‹μ€ 정말 λ§›μžˆμŠ΅λ‹ˆλ‹€. μΉœκ΅¬μ™€ ν•¨κ»˜ 학ꡐ 근처 μ‹λ‹Ήμ—μ„œ 점심을 λ¨Ήμ—ˆμŠ΅λ‹ˆλ‹€.
1671
+ (Korean food is really delicious. I had lunch at a restaurant near the school with a friend.)
1672
+
1673
+ 여행을 κ°€λ©΄ 항상 ν–‰λ³΅ν•©λ‹ˆλ‹€. μƒˆλ‘œμš΄ κ³³μ—μ„œ μƒˆλ‘œμš΄ μ‚¬λžŒλ“€μ„ λ§Œλ‚˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
1674
+ (I'm always happy when I travel. I enjoy meeting new people in new places.)
1675
+
1676
+ λ¬Ό μ£Όμ„Έμš”! κ°μ‚¬ν•©λ‹ˆλ‹€.
1677
+ (Water, please! Thank you.)
1678
+
1679
+ μ‹œκ°„μ΄ 빨리 κ°‘λ‹ˆλ‹€. μ‚¬λž‘ν•˜λŠ” μ‚¬λžŒκ³Ό ν•¨κ»˜ν•˜λ©΄ 더 빨리 κ°‘λ‹ˆλ‹€.
1680
+ (Time flies. It goes even faster when you're with someone you love.)
1681
+ """
1682
+
1683
+
1684
  def create_demo():
1685
  example_pdf = get_example_pdf()
1686
  example_audio = get_example_audio()
 
1858
  with gr.Column(scale=2):
1859
  with gr.Tabs() as output_tabs:
1860
  with gr.Tab("πŸ“– Flashcards"):
1861
+ bootstrap_html = build_flashcard_html(BOOTSTRAP_VOCAB)
1862
+ output_html = gr.HTML(label="Flashcards will appear here", value=bootstrap_html)
1863
  gr.Markdown("**Export current deck:**")
1864
  with gr.Row():
1865
  export_json_btn = gr.Button("πŸ“₯ Export JSON", variant="secondary", size="sm")
 
1870
  with gr.Tab("❓ Quiz"):
1871
  gr.Markdown("**Test your knowledge** with a randomized 5-question multiple-choice quiz from the current deck.")
1872
  start_quiz_btn = gr.Button("πŸ§ͺ Start 5-Question Quiz", variant="primary")
1873
+ bootstrap_quiz_html = build_quiz_html(BOOTSTRAP_VOCAB)
1874
+ quiz_output_html = gr.HTML(label="Quiz", value=bootstrap_quiz_html)
1875
 
1876
  stream_box = gr.Textbox(label="Live Model Generation 🧠", lines=10, max_lines=20, interactive=False, autoscroll=True, elem_id="stream_box")
1877
 
1878
  with gr.Accordion("πŸ“„ Extracted Source Content", open=True):
1879
+ extracted_text_box = gr.Textbox(label="Extracted Text", lines=10, max_lines=15, interactive=False, value=DEMO_EXTRACTED_TEXT)
1880
  extracted_images_gallery = gr.Gallery(label="Extracted Images", columns=4, height="auto", object_fit="contain")
1881
  extracted_audio_player = gr.Audio(label="Extracted Audio (YouTube / Uploaded)", type="filepath", interactive=False)
1882
 
1883
  last_source_state = gr.State(None)
1884
+ last_korean_words_state = gr.State(BOOTSTRAP_VOCAB)
1885
 
1886
  def reset_btn_text():
1887
  return gr.update(value="⚑ Stop thinking, Generate now"), gr.update(value="πŸ›‘ Stop Generation")