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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -115
app.py CHANGED
@@ -1,123 +1,97 @@
1
  import gradio as gr
 
2
  import random
3
- import csv
4
- import io
5
-
6
- def parse_csv(file, vocabulary):
7
- if file is None:
8
- return vocabulary
9
- file_content = file.read().decode('utf-8').strip()
10
- reader = csv.reader(io.StringIO(file_content))
11
- for row in reader:
12
- if len(row) >= 2:
13
- word = row[0].strip()
14
- definition = ",".join(row[1:]).strip()
15
- if word and definition:
16
- vocabulary.append({"word": word, "definition": definition})
17
- return vocabulary
18
-
19
- def add_manual_word(vocabulary, word, definition):
20
- word = word.strip()
21
- definition = definition.strip()
 
 
22
  if word and definition:
23
- vocabulary.append({"word": word, "definition": definition})
24
- return vocabulary, f'Added "{word}" to your vocabulary.' if word and definition else "Please enter both a word and a definition."
 
 
25
 
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]
32
- random.shuffle(defs)
33
- return gr.update(visible=True), "Quiz started! Match words with definitions.", vocabulary, words, defs, 0
34
-
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:
41
- correct_def = v["definition"]
42
- break
43
- if correct_def is None:
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):
51
- return f"Correct! All {matched} matches completed!", words, defs, matched
52
- else:
53
- return f"Correct! {matched}/{len(vocabulary)} matched.", words, defs, matched
54
- else:
55
- return f"Incorrect! '{selected_word}' does not match the selected definition.", words, defs, matched
56
 
57
  def reset_quiz():
58
- return gr.update(visible=False), None, [], [], [], 0, None
59
-
60
- with gr.Blocks() as demo:
61
- gr.Markdown("# 📚 Personal English Vocabulary App")
62
-
63
- vocabulary_state = gr.State([])
64
- words_state = gr.State([])
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:")
78
- add_word_btn = gr.Button("Add Word")
79
- add_status = gr.Markdown("")
80
-
81
- start_quiz_btn = gr.Button("Start Quiz")
82
-
83
- quiz_section = gr.Group(visible=False)
84
- with quiz_section:
85
- quiz_status = gr.Markdown("")
86
  with gr.Row():
87
- selected_word = gr.Dropdown(label="Words")
88
- selected_def = gr.Dropdown(label="Definitions")
89
- match_btn = gr.Button("Check Match")
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],
97
- outputs=[vocabulary_state]
98
- ).then(
99
- lambda v: f"{len(v)} words loaded." if v else "No words loaded.",
100
- inputs=[vocabulary_state],
101
- outputs=status_text
102
- )
103
-
104
- add_word_btn.click(
105
- add_manual_word,
106
- inputs=[vocabulary_state, manual_word, manual_def],
107
- outputs=[vocabulary_state, add_status]
108
- )
109
-
110
- start_quiz_btn.click(
111
- start_quiz,
112
- inputs=[vocabulary_state],
113
- outputs=[quiz_section, quiz_status, vocabulary_state, words_state, defs_state, matched_state]
114
- ).then(
115
- lambda words, defs: (gr.update(choices=words, value=None), gr.update(choices=defs, value=None)),
116
- inputs=[words_state, defs_state],
117
- outputs=[selected_word, selected_def]
118
- )
119
-
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_
 
1
  import gradio as gr
2
+ import pandas as pd
3
  import random
4
+
5
+ vocabulary = []
6
+ matches = {}
7
+ score = 0
8
+
9
+
10
+ def parse_csv(file):
11
+ global vocabulary
12
+ try:
13
+ df = pd.read_csv(file.name, header=None)
14
+ vocabulary = df.to_dict('records')
15
+ for item in vocabulary:
16
+ item['word'] = item[0]
17
+ item['definition'] = item[1]
18
+ return "CSV file successfully uploaded! Ready to start the quiz!"
19
+ except Exception as e:
20
+ return f"Error parsing file: {e}"
21
+
22
+
23
+ def add_word(word, definition):
24
+ global vocabulary
25
  if word and definition:
26
+ vocabulary.append({'word': word.strip(), 'definition': definition.strip()})
27
+ return f'Word "{word}" added to vocabulary!'
28
+ return "Please provide both word and definition."
29
+
30
 
31
+ def start_quiz():
32
  if not vocabulary:
33
+ return "Vocabulary is empty. Please upload a file or add words manually.", [], []
34
+
35
+ shuffle_vocabulary = vocabulary[:]
36
+ random.shuffle(shuffle_vocabulary)
37
+ words = [item['word'] for item in shuffle_vocabulary]
38
+ definitions = [item['definition'] for item in shuffle_vocabulary]
39
+ random.shuffle(definitions)
40
+ return "Quiz started! Match words to definitions.", words, definitions
41
+
42
+
43
+ def check_match(word, definition):
44
+ global score
45
+ for item in vocabulary:
46
+ if item['word'] == word and item['definition'] == definition:
47
+ score += 1
48
+ return f"Correct! '{word}' matches its definition. Total score: {score}", True
49
+ return f"Incorrect! '{word}' does not match the definition.", False
50
+
 
 
 
 
 
 
 
 
 
 
51
 
52
  def reset_quiz():
53
+ global vocabulary, matches, score
54
+ vocabulary = []
55
+ matches = {}
56
+ score = 0
57
+ return "Quiz reset! Ready for a new start."
58
+
59
+
60
+ with gr.Blocks() as app:
61
+ with gr.Row():
62
+ gr.Markdown("# 📚 Personal English Vocabulary App")
63
+
64
+ with gr.Tab("Upload Vocabulary"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  with gr.Row():
66
+ file_input = gr.File(label="Upload Vocabulary CSV")
67
+ file_output = gr.Textbox(label="Upload Status")
68
+ upload_button = gr.Button("Upload")
69
+
70
+ upload_button.click(parse_csv, inputs=file_input, outputs=file_output)
71
+
72
+ with gr.Tab("Add Vocabulary"):
73
+ word_input = gr.Textbox(label="Word")
74
+ definition_input = gr.Textbox(label="Definition")
75
+ add_button = gr.Button("Add Word")
76
+ add_status = gr.Textbox(label="Status")
77
+
78
+ add_button.click(add_word, inputs=[word_input, definition_input], outputs=add_status)
79
+
80
+ with gr.Tab("Quiz"):
81
+ quiz_status = gr.Textbox(label="Status")
82
+ start_button = gr.Button("Start Quiz")
83
+ words_list = gr.Dropdown(label="Select Word")
84
+ definitions_list = gr.Dropdown(label="Select Definition")
85
+ check_button = gr.Button("Check Match")
86
+ match_status = gr.Textbox(label="Result")
87
+
88
+ start_button.click(start_quiz, inputs=None, outputs=[quiz_status, words_list, definitions_list])
89
+ check_button.click(check_match, inputs=[words_list, definitions_list], outputs=match_status)
90
+
91
+ with gr.Row():
92
+ reset_button = gr.Button("Reset Quiz")
93
+ reset_status = gr.Textbox(label="Reset Status")
94
+
95
+ reset_button.click(reset_quiz, inputs=None, outputs=reset_status)
96
+
97
+ app.launch()