Spaces:
Running
Running
Upload 6 files
Browse files- app.py +72 -0
- images/blue_square.png +0 -0
- images/green_triangle.png +0 -0
- images/red_circle.png +0 -0
- images/yellow_star.png +0 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import random
|
4 |
+
|
5 |
+
questions = [
|
6 |
+
{"image": "images/red_circle.png", "sentence": "This is a red circle.", "answer": True},
|
7 |
+
{"image": "images/red_circle.png", "sentence": "This is a blue square.", "answer": False},
|
8 |
+
{"image": "images/blue_square.png", "sentence": "This is a blue square.", "answer": True},
|
9 |
+
{"image": "images/blue_square.png", "sentence": "This is a yellow star.", "answer": False},
|
10 |
+
{"image": "images/green_triangle.png", "sentence": "This is a green triangle.", "answer": True},
|
11 |
+
{"image": "images/green_triangle.png", "sentence": "This is a red circle.", "answer": False},
|
12 |
+
{"image": "images/yellow_star.png", "sentence": "This is a yellow star.", "answer": True},
|
13 |
+
{"image": "images/yellow_star.png", "sentence": "This is a green triangle.", "answer": False},
|
14 |
+
{"image": "images/red_circle.png", "sentence": "This is a red square.", "answer": False},
|
15 |
+
{"image": "images/blue_square.png", "sentence": "This is a blue circle.", "answer": False},
|
16 |
+
]
|
17 |
+
|
18 |
+
score = {"correct": 0, "wrong": 0, "index": 0}
|
19 |
+
|
20 |
+
def check_answer(user_answer, correct_answer):
|
21 |
+
if score["index"] >= len(questions):
|
22 |
+
return "", None, "", "π Game over!", score["correct"], score["wrong"]
|
23 |
+
|
24 |
+
is_correct = str(user_answer) == correct_answer
|
25 |
+
if is_correct:
|
26 |
+
feedback = "β
Correct!"
|
27 |
+
score["correct"] += 1
|
28 |
+
else:
|
29 |
+
feedback = "β Wrong!"
|
30 |
+
score["wrong"] += 1
|
31 |
+
|
32 |
+
score["index"] += 1
|
33 |
+
|
34 |
+
if score["index"] >= len(questions):
|
35 |
+
feedback += f" π Game over! Final score: {score['correct']} correct / {score['wrong']} wrong"
|
36 |
+
return "", None, "", feedback, score["correct"], score["wrong"]
|
37 |
+
|
38 |
+
next_q = questions[score["index"]]
|
39 |
+
return next_q["sentence"], next_q["image"], str(next_q["answer"]), feedback, score["correct"], score["wrong"]
|
40 |
+
|
41 |
+
def reset_game():
|
42 |
+
score["correct"] = 0
|
43 |
+
score["wrong"] = 0
|
44 |
+
score["index"] = 0
|
45 |
+
random.shuffle(questions)
|
46 |
+
q = questions[0]
|
47 |
+
return q["sentence"], q["image"], str(q["answer"]), "", 0, 0
|
48 |
+
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown("## π¨ True or False Game β Visual Feedback Only")
|
51 |
+
sentence = gr.Textbox(label="Sentence", interactive=False)
|
52 |
+
image = gr.Image(type="filepath", label="Image")
|
53 |
+
correct_answer = gr.Textbox(visible=False)
|
54 |
+
|
55 |
+
with gr.Row():
|
56 |
+
btn_true = gr.Button("β
True")
|
57 |
+
btn_false = gr.Button("β False")
|
58 |
+
|
59 |
+
result = gr.Textbox(label="Feedback", interactive=False, elem_classes=["feedback-box"])
|
60 |
+
score_correct = gr.Number(label="Correct", value=0, interactive=False)
|
61 |
+
score_wrong = gr.Number(label="Wrong", value=0, interactive=False)
|
62 |
+
|
63 |
+
btn_true.click(fn=lambda x: check_answer(True, x), inputs=correct_answer,
|
64 |
+
outputs=[sentence, image, correct_answer, result, score_correct, score_wrong])
|
65 |
+
btn_false.click(fn=lambda x: check_answer(False, x), inputs=correct_answer,
|
66 |
+
outputs=[sentence, image, correct_answer, result, score_correct, score_wrong])
|
67 |
+
|
68 |
+
demo.load(fn=reset_game,
|
69 |
+
outputs=[sentence, image, correct_answer, result, score_correct, score_wrong])
|
70 |
+
|
71 |
+
demo.css = ".feedback-box {font-size: 24px; font-weight: bold; text-align: center; height: 60px;}"
|
72 |
+
demo.launch()
|
images/blue_square.png
ADDED
![]() |
images/green_triangle.png
ADDED
![]() |
images/red_circle.png
ADDED
![]() |
images/yellow_star.png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
gradio
|