issamlaradji commited on
Commit
2d2c645
·
verified ·
1 Parent(s): 49f315d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import time
4
+ import html
5
+
6
+
7
+ class TriviaQuiz:
8
+ def __init__(self):
9
+ self.current_question = None
10
+ self.current_answer = None
11
+ self.score = 0
12
+ self.total_questions = 0
13
+
14
+ # Sample trivia questions
15
+ self.questions = [
16
+ {"question": "What is the capital of France?", "answer": "Paris"},
17
+ {"question": "Which planet is known as the Red Planet?", "answer": "Mars"},
18
+ {"question": "What is the largest mammal?", "answer": "Blue Whale"},
19
+ {
20
+ "question": "Who wrote 'Romeo and Juliet'?",
21
+ "answer": "William Shakespeare",
22
+ },
23
+ {"question": "What is the chemical symbol for gold?", "answer": "Au"},
24
+ {
25
+ "question": "Which country is home to the kangaroo?",
26
+ "answer": "Australia",
27
+ },
28
+ {
29
+ "question": "What is the tallest mountain in the world?",
30
+ "answer": "Mount Everest",
31
+ },
32
+ {"question": "How many sides does a hexagon have?", "answer": "6"},
33
+ {
34
+ "question": "What is the largest organ in the human body?",
35
+ "answer": "Skin",
36
+ },
37
+ {
38
+ "question": "Which element has the chemical symbol 'O'?",
39
+ "answer": "Oxygen",
40
+ },
41
+ ]
42
+
43
+ def get_new_question(self):
44
+ if not self.questions:
45
+ return "No more questions available!", ""
46
+
47
+ question_data = random.choice(self.questions)
48
+ self.questions.remove(question_data)
49
+
50
+ self.current_question = question_data["question"]
51
+ self.current_answer = question_data["answer"].lower()
52
+ self.total_questions += 1
53
+
54
+ return self.current_question, ""
55
+
56
+ def check_answer(self, user_answer):
57
+ if not self.current_question:
58
+ return "Please get a new question first!", self.score, self.total_questions
59
+
60
+ user_answer = user_answer.lower().strip()
61
+ correct = user_answer == self.current_answer.lower()
62
+
63
+ if correct:
64
+ self.score += 1
65
+ result = f"Correct! The answer is {self.current_answer}."
66
+ else:
67
+ result = (
68
+ f"Sorry, that's incorrect. The correct answer is {self.current_answer}."
69
+ )
70
+
71
+ self.current_question = None
72
+ return result, self.score, self.total_questions
73
+
74
+
75
+ def create_trivia_app():
76
+ quiz = TriviaQuiz()
77
+
78
+ with gr.Blocks() as app:
79
+ gr.Markdown("# Trivia Quiz")
80
+ gr.Markdown("Test your knowledge with these trivia questions!")
81
+
82
+ with gr.Row():
83
+ with gr.Column(scale=2):
84
+ question_display = gr.Textbox(label="Question", interactive=False)
85
+ answer_input = gr.Textbox(
86
+ label="Your Answer", placeholder="Type your answer here"
87
+ )
88
+ result_display = gr.Textbox(label="Result", interactive=False)
89
+
90
+ with gr.Column(scale=1):
91
+ score_display = gr.Number(label="Score", value=0, interactive=False)
92
+ total_display = gr.Number(
93
+ label="Total Questions", value=0, interactive=False
94
+ )
95
+
96
+ with gr.Row():
97
+ new_question_btn = gr.Button("Get New Question")
98
+ submit_btn = gr.Button("Submit Answer", variant="primary")
99
+ reset_btn = gr.Button("Reset Quiz")
100
+
101
+ # Event handlers
102
+ new_question_btn.click(
103
+ quiz.get_new_question, outputs=[question_display, answer_input]
104
+ )
105
+
106
+ submit_btn.click(
107
+ quiz.check_answer,
108
+ inputs=[answer_input],
109
+ outputs=[result_display, score_display, total_display],
110
+ )
111
+
112
+ def reset_quiz():
113
+ quiz.__init__()
114
+ return "", "", "", 0, 0
115
+
116
+ reset_btn.click(
117
+ reset_quiz,
118
+ outputs=[
119
+ question_display,
120
+ answer_input,
121
+ result_display,
122
+ score_display,
123
+ total_display,
124
+ ],
125
+ )
126
+
127
+ return app
128
+
129
+
130
+ if __name__ == "__main__":
131
+ app = create_trivia_app()
132
+ app.launch()