ReneeHWT commited on
Commit
6270671
·
verified ·
1 Parent(s): e179409

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -33
app.py CHANGED
@@ -3,12 +3,6 @@ import random
3
  import csv
4
  import io
5
 
6
- # Global states (to be stored in Gradio states)
7
- # vocabulary: list of dicts [{'word': '...', 'definition': '...'}, ...]
8
- # matched: number of correct matches made
9
- # available_words: list of words currently un-matched
10
- # available_definitions: list of definitions currently un-matched
11
-
12
  def parse_csv(file, vocabulary):
13
  if file is None:
14
  return vocabulary
@@ -32,7 +26,6 @@ def add_manual_word(vocabulary, word, definition):
32
  def start_quiz(vocabulary):
33
  if not vocabulary:
34
  return gr.update(visible=False), "No vocabulary available. Please upload CSV or add words.", None, None, None, None
35
- # Shuffle the vocabulary for a quiz
36
  random.shuffle(vocabulary)
37
  words = [v["word"] for v in vocabulary]
38
  defs = [v["definition"] for v in vocabulary]
@@ -42,7 +35,6 @@ def start_quiz(vocabulary):
42
  def check_match(selected_word, selected_def, vocabulary, words, defs, matched):
43
  if not selected_word or not selected_def:
44
  return "Please select both a word and a definition.", words, defs, matched
45
- # Find the correct definition for the selected word
46
  correct_def = None
47
  for v in vocabulary:
48
  if v["word"] == selected_word:
@@ -52,9 +44,7 @@ def check_match(selected_word, selected_def, vocabulary, words, defs, matched):
52
  return "Error: Word not found in vocabulary.", words, defs, matched
53
 
54
  if correct_def == selected_def:
55
- # Correct match
56
  matched += 1
57
- # Remove matched word and definition
58
  words = [w for w in words if w != selected_word]
59
  defs = [d for d in defs if d != selected_def]
60
  if matched == len(vocabulary):
@@ -75,13 +65,13 @@ with gr.Blocks() as demo:
75
  defs_state = gr.State([])
76
  matched_state = gr.State(0)
77
 
78
- with gr.Box():
79
  gr.Markdown("## Upload Your Vocabulary (CSV format: word, definition)")
80
  file_input = gr.File(label="Upload CSV")
81
  upload_btn = gr.Button("Parse CSV")
82
  status_text = gr.Markdown("")
83
 
84
- with gr.Box():
85
  gr.Markdown("## Add a Vocabulary Word Manually")
86
  manual_word = gr.Textbox(label="Word:")
87
  manual_def = gr.Textbox(label="Definition:")
@@ -100,7 +90,7 @@ with gr.Blocks() as demo:
100
  match_result = gr.Markdown("")
101
  reset_btn = gr.Button("Reset Quiz")
102
 
103
- # Button actions
104
  upload_btn.click(
105
  parse_csv,
106
  inputs=[file_input, vocabulary_state],
@@ -130,23 +120,4 @@ with gr.Blocks() as demo:
130
  match_btn.click(
131
  check_match,
132
  inputs=[selected_word, selected_def, vocabulary_state, words_state, defs_state, matched_state],
133
- outputs=[match_result, words_state, defs_state, matched_state]
134
- ).then(
135
- lambda words, defs: (gr.update(choices=words, value=None), gr.update(choices=defs, value=None)),
136
- inputs=[words_state, defs_state],
137
- outputs=[selected_word, selected_def]
138
- )
139
-
140
- reset_btn.click(
141
- reset_quiz,
142
- inputs=[],
143
- outputs=[quiz_section, quiz_status, vocabulary_state, words_state, defs_state, matched_state, match_result]
144
- ).then(
145
- lambda: "",
146
- outputs=add_status
147
- ).then(
148
- lambda: "",
149
- outputs=status_text
150
- )
151
-
152
- demo.launch()
 
3
  import csv
4
  import io
5
 
 
 
 
 
 
 
6
  def parse_csv(file, vocabulary):
7
  if file is None:
8
  return vocabulary
 
26
  def start_quiz(vocabulary):
27
  if not vocabulary:
28
  return gr.update(visible=False), "No vocabulary available. Please upload CSV or add words.", None, None, None, None
 
29
  random.shuffle(vocabulary)
30
  words = [v["word"] for v in vocabulary]
31
  defs = [v["definition"] for v in vocabulary]
 
35
  def check_match(selected_word, selected_def, vocabulary, words, defs, matched):
36
  if not selected_word or not selected_def:
37
  return "Please select both a word and a definition.", words, defs, matched
 
38
  correct_def = None
39
  for v in vocabulary:
40
  if v["word"] == selected_word:
 
44
  return "Error: Word not found in vocabulary.", words, defs, matched
45
 
46
  if correct_def == selected_def:
 
47
  matched += 1
 
48
  words = [w for w in words if w != selected_word]
49
  defs = [d for d in defs if d != selected_def]
50
  if matched == len(vocabulary):
 
65
  defs_state = gr.State([])
66
  matched_state = gr.State(0)
67
 
68
+ with gr.Group():
69
  gr.Markdown("## Upload Your Vocabulary (CSV format: word, definition)")
70
  file_input = gr.File(label="Upload CSV")
71
  upload_btn = gr.Button("Parse CSV")
72
  status_text = gr.Markdown("")
73
 
74
+ with gr.Group():
75
  gr.Markdown("## Add a Vocabulary Word Manually")
76
  manual_word = gr.Textbox(label="Word:")
77
  manual_def = gr.Textbox(label="Definition:")
 
90
  match_result = gr.Markdown("")
91
  reset_btn = gr.Button("Reset Quiz")
92
 
93
+ # Button interactions
94
  upload_btn.click(
95
  parse_csv,
96
  inputs=[file_input, vocabulary_state],
 
120
  match_btn.click(
121
  check_match,
122
  inputs=[selected_word, selected_def, vocabulary_state, words_state, defs_state, matched_state],
123
+ outputs=[match_result, words_state, defs_