Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
|
4 |
+
# Define questions and answers
|
5 |
+
questions = ["colorful", "worried", "excited", "showing", "respected", "doubters", "success"]
|
6 |
+
answers = ["vibrant", "anxious", "thrilled", "depicting", "admired", "skeptics", "achievement"]
|
7 |
+
|
8 |
+
answered_questions = set() # Track which questions have been answered
|
9 |
+
|
10 |
+
def get_options(question):
|
11 |
+
index = questions.index(question)
|
12 |
+
correct_answer = answers[index]
|
13 |
+
options = set([correct_answer])
|
14 |
+
while len(options) < 4:
|
15 |
+
options.add(random.choice(answers))
|
16 |
+
options = list(options)
|
17 |
+
random.shuffle(options)
|
18 |
+
return ", ".join(options), correct_answer, index
|
19 |
+
|
20 |
+
def check_answer(user_input, correct_answer, score, question_index):
|
21 |
+
if question_index in answered_questions:
|
22 |
+
return f"You have already answered this question. Your score remains {score}.", score
|
23 |
+
if user_input.strip().lower() == correct_answer.lower():
|
24 |
+
score += 1
|
25 |
+
feedback = f"Correct! Your score is now {score}."
|
26 |
+
else:
|
27 |
+
feedback = f"Incorrect. The correct answer was '{correct_answer}'. Your score remains {score}."
|
28 |
+
answered_questions.add(question_index)
|
29 |
+
return feedback, score
|
30 |
+
|
31 |
+
with gr.Blocks() as app:
|
32 |
+
with gr.Row():
|
33 |
+
question_dropdown = gr.Dropdown(choices=questions, label="Select a question", value=questions[0])
|
34 |
+
show_options_button = gr.Button("Show me options")
|
35 |
+
options_output = gr.Textbox(label="Options", interactive=False)
|
36 |
+
correct_answer_store = gr.State()
|
37 |
+
question_index_store = gr.State()
|
38 |
+
user_input = gr.Textbox(label="Type your answer here", visible=True)
|
39 |
+
submit_button = gr.Button("Submit Answer", visible=True)
|
40 |
+
feedback_label = gr.Label()
|
41 |
+
score_store = gr.State(value=0)
|
42 |
+
complete_button = gr.Button("Complete and Show Total Score", visible=True)
|
43 |
+
|
44 |
+
def show_options(question):
|
45 |
+
options, correct, index = get_options(question)
|
46 |
+
options_output.value = options
|
47 |
+
correct_answer_store.value = correct
|
48 |
+
question_index_store.value = index
|
49 |
+
return options, correct, index
|
50 |
+
|
51 |
+
show_options_button.click(
|
52 |
+
fn=show_options,
|
53 |
+
inputs=question_dropdown,
|
54 |
+
outputs=[options_output, correct_answer_store, question_index_store]
|
55 |
+
)
|
56 |
+
|
57 |
+
submit_button.click(
|
58 |
+
fn=lambda user_input, correct_answer_store=correct_answer_store, score_store=score_store, question_index_store=question_index_store: submit_response(user_input, correct_answer_store.value, score_store.value, question_index_store.value),
|
59 |
+
inputs=user_input,
|
60 |
+
outputs=[feedback_label, score_store]
|
61 |
+
)
|
62 |
+
|
63 |
+
def submit_response(user_input, correct_answer, score, question_index):
|
64 |
+
feedback, updated_score = check_answer(user_input, correct_answer, score, question_index)
|
65 |
+
score_store.value = updated_score # Ensure the score is updated in the state
|
66 |
+
return feedback, updated_score
|
67 |
+
|
68 |
+
complete_button.click(
|
69 |
+
fn=lambda score_store=score_store: f"Session Complete. Your final score is {score_store.value}.",
|
70 |
+
inputs=[],
|
71 |
+
outputs=feedback_label
|
72 |
+
)
|
73 |
+
|
74 |
+
app.launch()
|