Update app.py
Browse files
app.py
CHANGED
|
@@ -1,123 +1,97 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import random
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
if word and definition:
|
| 23 |
-
vocabulary.append({
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def start_quiz(
|
| 27 |
if not vocabulary:
|
| 28 |
-
return
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 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 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
inputs=
|
| 117 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|