Update app.py
Browse files
app.py
CHANGED
|
@@ -6,20 +6,15 @@ 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.
|
| 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:
|
|
@@ -27,7 +22,6 @@ def add_word(word, definition):
|
|
| 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.", [], []
|
|
@@ -39,7 +33,6 @@ def start_quiz():
|
|
| 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:
|
|
@@ -48,15 +41,12 @@ def check_match(word, definition):
|
|
| 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")
|
|
@@ -80,12 +70,16 @@ with gr.Blocks() as app:
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
check_button.click(check_match, inputs=[words_list, definitions_list], outputs=match_status)
|
| 90 |
|
| 91 |
with gr.Row():
|
|
|
|
| 6 |
matches = {}
|
| 7 |
score = 0
|
| 8 |
|
|
|
|
| 9 |
def parse_csv(file):
|
| 10 |
global vocabulary
|
| 11 |
try:
|
| 12 |
df = pd.read_csv(file.name, header=None)
|
| 13 |
+
vocabulary = [{'word': row[0], 'definition': row[1]} for _, row in df.iterrows()]
|
|
|
|
|
|
|
|
|
|
| 14 |
return "CSV file successfully uploaded! Ready to start the quiz!"
|
| 15 |
except Exception as e:
|
| 16 |
return f"Error parsing file: {e}"
|
| 17 |
|
|
|
|
| 18 |
def add_word(word, definition):
|
| 19 |
global vocabulary
|
| 20 |
if word and definition:
|
|
|
|
| 22 |
return f'Word "{word}" added to vocabulary!'
|
| 23 |
return "Please provide both word and definition."
|
| 24 |
|
|
|
|
| 25 |
def start_quiz():
|
| 26 |
if not vocabulary:
|
| 27 |
return "Vocabulary is empty. Please upload a file or add words manually.", [], []
|
|
|
|
| 33 |
random.shuffle(definitions)
|
| 34 |
return "Quiz started! Match words to definitions.", words, definitions
|
| 35 |
|
|
|
|
| 36 |
def check_match(word, definition):
|
| 37 |
global score
|
| 38 |
for item in vocabulary:
|
|
|
|
| 41 |
return f"Correct! '{word}' matches its definition. Total score: {score}", True
|
| 42 |
return f"Incorrect! '{word}' does not match the definition.", False
|
| 43 |
|
|
|
|
| 44 |
def reset_quiz():
|
| 45 |
global vocabulary, matches, score
|
|
|
|
| 46 |
matches = {}
|
| 47 |
score = 0
|
| 48 |
return "Quiz reset! Ready for a new start."
|
| 49 |
|
|
|
|
| 50 |
with gr.Blocks() as app:
|
| 51 |
with gr.Row():
|
| 52 |
gr.Markdown("# 📚 Personal English Vocabulary App")
|
|
|
|
| 70 |
with gr.Tab("Quiz"):
|
| 71 |
quiz_status = gr.Textbox(label="Status")
|
| 72 |
start_button = gr.Button("Start Quiz")
|
| 73 |
+
words_list = gr.Dropdown(label="Select Word", choices=[]) # Initialize dropdowns as empty
|
| 74 |
+
definitions_list = gr.Dropdown(label="Select Definition", choices=[])
|
| 75 |
check_button = gr.Button("Check Match")
|
| 76 |
match_status = gr.Textbox(label="Result")
|
| 77 |
|
| 78 |
+
def refresh_quiz():
|
| 79 |
+
quiz_message, words, definitions = start_quiz()
|
| 80 |
+
return quiz_message, gr.update(choices=words), gr.update(choices=definitions)
|
| 81 |
+
|
| 82 |
+
start_button.click(refresh_quiz, inputs=None, outputs=[quiz_status, words_list, definitions_list])
|
| 83 |
check_button.click(check_match, inputs=[words_list, definitions_list], outputs=match_status)
|
| 84 |
|
| 85 |
with gr.Row():
|