Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import random
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def load_data():
|
| 6 |
+
url = "https://raw.githubusercontent.com/MK316/Myapps/main/data/IPA.csv"
|
| 7 |
+
return pd.read_csv(url, encoding='utf-8')
|
| 8 |
+
|
| 9 |
+
df = load_data()
|
| 10 |
+
|
| 11 |
+
properties = ['Voicing', 'Place', 'Centrality', 'Oro-nasal', 'Manner']
|
| 12 |
+
used_pairs = set()
|
| 13 |
+
|
| 14 |
+
def generate_question():
|
| 15 |
+
total_questions = len(df) * len(properties)
|
| 16 |
+
if len(used_pairs) >= total_questions:
|
| 17 |
+
return "All IPA symbols have been used. Quiz will restart.", "", 0
|
| 18 |
+
|
| 19 |
+
# Generate a unique pair that has not been used yet
|
| 20 |
+
while True:
|
| 21 |
+
current_ipa = df.sample(1).iloc[0]
|
| 22 |
+
current_property_index = random.randint(0, len(properties) - 1)
|
| 23 |
+
property_name = properties[current_property_index]
|
| 24 |
+
pair = (current_ipa['IPA'], property_name)
|
| 25 |
+
|
| 26 |
+
if pair not in used_pairs:
|
| 27 |
+
used_pairs.add(pair)
|
| 28 |
+
question = f"IPA Symbol: {current_ipa['IPA']}\nWhat is the {property_name.lower()} of this IPA symbol?"
|
| 29 |
+
answer = current_ipa[property_name].lower()
|
| 30 |
+
return question, answer, current_property_index
|
| 31 |
+
|
| 32 |
+
def quiz_function(user_answer, score, trials, correct_answer):
|
| 33 |
+
if user_answer.lower() == correct_answer:
|
| 34 |
+
score[0] += 1
|
| 35 |
+
result = f"Correct! The answer was '{correct_answer}'."
|
| 36 |
+
else:
|
| 37 |
+
result = f"Wrong! The correct answer was '{correct_answer}'."
|
| 38 |
+
trials[0] += 1
|
| 39 |
+
return result
|
| 40 |
+
|
| 41 |
+
def gradio_interface():
|
| 42 |
+
score = [0]
|
| 43 |
+
trials = [0]
|
| 44 |
+
question_info = {'question': '', 'answer': '', 'property_index': 0}
|
| 45 |
+
user_name = [''] # Use a list to hold the user name to persist changes
|
| 46 |
+
|
| 47 |
+
def start_quiz(name):
|
| 48 |
+
user_name[0] = name # Store the user's name
|
| 49 |
+
question, answer, property_index = generate_question()
|
| 50 |
+
question_info.update({'question': question, 'answer': answer, 'property_index': property_index})
|
| 51 |
+
return question
|
| 52 |
+
|
| 53 |
+
def submit_answer(user_answer):
|
| 54 |
+
result = quiz_function(user_answer, score, trials, question_info['answer'])
|
| 55 |
+
new_question, new_answer, new_property_index = generate_question()
|
| 56 |
+
if new_question == "All IPA symbols have been used. Quiz will restart.":
|
| 57 |
+
used_pairs.clear() # Clear the set to restart the quiz
|
| 58 |
+
question_info.update({'question': new_question, 'answer': new_answer, 'property_index': new_property_index})
|
| 59 |
+
return result, "", new_question
|
| 60 |
+
|
| 61 |
+
def quit_quiz():
|
| 62 |
+
# Use the user's name stored in user_name list
|
| 63 |
+
return f"Quiz ended. Well done, {user_name[0]}! Your total score: {score[0]}/{trials[0]} points."
|
| 64 |
+
|
| 65 |
+
with gr.Blocks() as app:
|
| 66 |
+
with gr.Column():
|
| 67 |
+
name_input = gr.Textbox(label="Enter your name", placeholder="Your name")
|
| 68 |
+
start_button = gr.Button("Start Quiz")
|
| 69 |
+
question_label = gr.Textbox(label="Question", interactive=False)
|
| 70 |
+
answer_input = gr.Textbox(label="Your Answer", placeholder="Type your answer here")
|
| 71 |
+
submit_button = gr.Button("Submit")
|
| 72 |
+
output_label = gr.Textbox(label="Result", interactive=False)
|
| 73 |
+
quit_button = gr.Button("Quit")
|
| 74 |
+
|
| 75 |
+
start_button.click(fn=start_quiz, inputs=[name_input], outputs=[question_label])
|
| 76 |
+
submit_button.click(fn=submit_answer, inputs=[answer_input], outputs=[output_label, answer_input, question_label])
|
| 77 |
+
quit_button.click(fn=quit_quiz, outputs=[output_label])
|
| 78 |
+
|
| 79 |
+
return app
|
| 80 |
+
|
| 81 |
+
app = gradio_interface()
|
| 82 |
+
app.launch()
|