Update app.py
Browse files
app.py
CHANGED
|
@@ -10,90 +10,84 @@ ssl._create_default_https_context = ssl._create_unverified_context
|
|
| 10 |
vocabulary = []
|
| 11 |
matches = {}
|
| 12 |
score = 0
|
|
|
|
| 13 |
|
| 14 |
# Helper Functions
|
| 15 |
def parse_csv(file):
|
|
|
|
| 16 |
try:
|
| 17 |
data = pd.read_csv(file.name, header=None)
|
| 18 |
vocab = data.dropna().values.tolist()
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
return
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
global
|
| 25 |
-
if word and definition:
|
| 26 |
-
vocabulary.append({"word": word.strip(), "definition": definition.strip()})
|
| 27 |
-
return f'Word "{word}" added successfully!', vocabulary
|
| 28 |
-
else:
|
| 29 |
-
return "Both word and definition are required.", vocabulary
|
| 30 |
-
|
| 31 |
-
def start_quiz():
|
| 32 |
-
global matches, score
|
| 33 |
-
score = 0
|
| 34 |
if len(vocabulary) < 1:
|
| 35 |
-
return "Please add
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
random.shuffle(vocabulary)
|
| 38 |
words = [item['word'] for item in vocabulary]
|
| 39 |
definitions = [item['definition'] for item in vocabulary]
|
| 40 |
random.shuffle(definitions)
|
| 41 |
|
| 42 |
matches = {item['word']: item['definition'] for item in vocabulary}
|
| 43 |
-
return "Match
|
| 44 |
|
| 45 |
def check_match(word, definition):
|
| 46 |
-
global matches, score
|
| 47 |
if matches.get(word) == definition:
|
| 48 |
score += 1
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
-
return f"Incorrect! {word} does not match {definition}.", score
|
| 52 |
|
| 53 |
-
def
|
| 54 |
-
global vocabulary, matches, score
|
| 55 |
-
vocabulary
|
| 56 |
-
matches
|
| 57 |
score = 0
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
# Gradio Interface
|
| 61 |
def main():
|
| 62 |
with gr.Blocks() as app:
|
| 63 |
-
gr.Markdown("#
|
| 64 |
|
| 65 |
with gr.Tab("Upload Vocabulary"):
|
| 66 |
csv_upload = gr.File(label="Upload CSV (word, definition)")
|
| 67 |
-
upload_button = gr.Button("Upload")
|
| 68 |
upload_output = gr.Textbox(label="Upload Status")
|
| 69 |
|
| 70 |
-
with gr.Tab("
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
add_button = gr.Button("Add Word")
|
| 74 |
-
add_output = gr.Textbox(label="Add Status")
|
| 75 |
-
|
| 76 |
-
with gr.Tab("Start Quiz"):
|
| 77 |
-
quiz_status = gr.Textbox(label="Quiz Status")
|
| 78 |
-
start_button = gr.Button("Start Quiz")
|
| 79 |
-
|
| 80 |
-
with gr.Tab("Matching Game"):
|
| 81 |
word_dropdown = gr.Dropdown(label="Select Word")
|
| 82 |
definition_dropdown = gr.Dropdown(label="Select Definition")
|
| 83 |
match_button = gr.Button("Check Match")
|
| 84 |
match_output = gr.Textbox(label="Match Result")
|
| 85 |
score_output = gr.Textbox(label="Score")
|
|
|
|
| 86 |
|
| 87 |
-
with gr.Tab("Reset
|
| 88 |
reset_button = gr.Button("Reset")
|
| 89 |
reset_output = gr.Textbox(label="Reset Status")
|
| 90 |
|
| 91 |
# Actions
|
| 92 |
upload_button.click(parse_csv, inputs=[csv_upload], outputs=[upload_output])
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
reset_button.click(reset_app, outputs=[reset_output])
|
| 97 |
|
| 98 |
return app
|
| 99 |
|
|
|
|
| 10 |
vocabulary = []
|
| 11 |
matches = {}
|
| 12 |
score = 0
|
| 13 |
+
remaining_pairs = 0
|
| 14 |
|
| 15 |
# Helper Functions
|
| 16 |
def parse_csv(file):
|
| 17 |
+
global remaining_pairs
|
| 18 |
try:
|
| 19 |
data = pd.read_csv(file.name, header=None)
|
| 20 |
vocab = data.dropna().values.tolist()
|
| 21 |
+
vocabulary.clear()
|
| 22 |
+
vocabulary.extend([{"word": row[0].strip(), "definition": row[1].strip()} for row in vocab])
|
| 23 |
+
remaining_pairs = len(vocabulary)
|
| 24 |
+
return f"Uploaded successfully! {remaining_pairs} pairs added.", vocabulary
|
| 25 |
except Exception as e:
|
| 26 |
+
return "Error reading the file. Ensure it's a valid CSV.", vocabulary
|
| 27 |
|
| 28 |
+
def start_game():
|
| 29 |
+
global matches, score, remaining_pairs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if len(vocabulary) < 1:
|
| 31 |
+
return "Please upload a vocabulary file or add words first.", [], []
|
| 32 |
+
|
| 33 |
+
score = 0
|
| 34 |
+
remaining_pairs = len(vocabulary)
|
| 35 |
random.shuffle(vocabulary)
|
| 36 |
words = [item['word'] for item in vocabulary]
|
| 37 |
definitions = [item['definition'] for item in vocabulary]
|
| 38 |
random.shuffle(definitions)
|
| 39 |
|
| 40 |
matches = {item['word']: item['definition'] for item in vocabulary}
|
| 41 |
+
return "Match words with their definitions to eliminate pairs.", words, definitions
|
| 42 |
|
| 43 |
def check_match(word, definition):
|
| 44 |
+
global matches, score, remaining_pairs
|
| 45 |
if matches.get(word) == definition:
|
| 46 |
score += 1
|
| 47 |
+
remaining_pairs -= 1
|
| 48 |
+
if remaining_pairs == 0:
|
| 49 |
+
return "Correct! You've matched all pairs! 🎉 Game Over.", score, remaining_pairs
|
| 50 |
+
return f"Correct! {word} matches {definition}. Remaining pairs: {remaining_pairs}", score, remaining_pairs
|
| 51 |
else:
|
| 52 |
+
return f"Incorrect! {word} does not match {definition}. Try again.", score, remaining_pairs
|
| 53 |
|
| 54 |
+
def reset_game():
|
| 55 |
+
global vocabulary, matches, score, remaining_pairs
|
| 56 |
+
vocabulary.clear()
|
| 57 |
+
matches.clear()
|
| 58 |
score = 0
|
| 59 |
+
remaining_pairs = 0
|
| 60 |
+
return "Game reset. Upload a new file or add words to start again.", vocabulary
|
| 61 |
|
| 62 |
# Gradio Interface
|
| 63 |
def main():
|
| 64 |
with gr.Blocks() as app:
|
| 65 |
+
gr.Markdown("# Vocabulary Matching Game")
|
| 66 |
|
| 67 |
with gr.Tab("Upload Vocabulary"):
|
| 68 |
csv_upload = gr.File(label="Upload CSV (word, definition)")
|
| 69 |
+
upload_button = gr.Button("Upload File")
|
| 70 |
upload_output = gr.Textbox(label="Upload Status")
|
| 71 |
|
| 72 |
+
with gr.Tab("Play Matching Game"):
|
| 73 |
+
game_status = gr.Textbox(label="Game Status")
|
| 74 |
+
start_button = gr.Button("Start Game")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
word_dropdown = gr.Dropdown(label="Select Word")
|
| 76 |
definition_dropdown = gr.Dropdown(label="Select Definition")
|
| 77 |
match_button = gr.Button("Check Match")
|
| 78 |
match_output = gr.Textbox(label="Match Result")
|
| 79 |
score_output = gr.Textbox(label="Score")
|
| 80 |
+
remaining_output = gr.Textbox(label="Remaining Pairs")
|
| 81 |
|
| 82 |
+
with gr.Tab("Reset Game"):
|
| 83 |
reset_button = gr.Button("Reset")
|
| 84 |
reset_output = gr.Textbox(label="Reset Status")
|
| 85 |
|
| 86 |
# Actions
|
| 87 |
upload_button.click(parse_csv, inputs=[csv_upload], outputs=[upload_output])
|
| 88 |
+
start_button.click(start_game, outputs=[game_status, word_dropdown, definition_dropdown])
|
| 89 |
+
match_button.click(check_match, inputs=[word_dropdown, definition_dropdown], outputs=[match_output, score_output, remaining_output])
|
| 90 |
+
reset_button.click(reset_game, outputs=[reset_output])
|
|
|
|
| 91 |
|
| 92 |
return app
|
| 93 |
|