File size: 4,226 Bytes
2d2c645
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import gradio as gr
import random
import time
import html


class TriviaQuiz:
    def __init__(self):
        self.current_question = None
        self.current_answer = None
        self.score = 0
        self.total_questions = 0

        # Sample trivia questions
        self.questions = [
            {"question": "What is the capital of France?", "answer": "Paris"},
            {"question": "Which planet is known as the Red Planet?", "answer": "Mars"},
            {"question": "What is the largest mammal?", "answer": "Blue Whale"},
            {
                "question": "Who wrote 'Romeo and Juliet'?",
                "answer": "William Shakespeare",
            },
            {"question": "What is the chemical symbol for gold?", "answer": "Au"},
            {
                "question": "Which country is home to the kangaroo?",
                "answer": "Australia",
            },
            {
                "question": "What is the tallest mountain in the world?",
                "answer": "Mount Everest",
            },
            {"question": "How many sides does a hexagon have?", "answer": "6"},
            {
                "question": "What is the largest organ in the human body?",
                "answer": "Skin",
            },
            {
                "question": "Which element has the chemical symbol 'O'?",
                "answer": "Oxygen",
            },
        ]

    def get_new_question(self):
        if not self.questions:
            return "No more questions available!", ""

        question_data = random.choice(self.questions)
        self.questions.remove(question_data)

        self.current_question = question_data["question"]
        self.current_answer = question_data["answer"].lower()
        self.total_questions += 1

        return self.current_question, ""

    def check_answer(self, user_answer):
        if not self.current_question:
            return "Please get a new question first!", self.score, self.total_questions

        user_answer = user_answer.lower().strip()
        correct = user_answer == self.current_answer.lower()

        if correct:
            self.score += 1
            result = f"Correct! The answer is {self.current_answer}."
        else:
            result = (
                f"Sorry, that's incorrect. The correct answer is {self.current_answer}."
            )

        self.current_question = None
        return result, self.score, self.total_questions


def create_trivia_app():
    quiz = TriviaQuiz()

    with gr.Blocks() as app:
        gr.Markdown("# Trivia Quiz")
        gr.Markdown("Test your knowledge with these trivia questions!")

        with gr.Row():
            with gr.Column(scale=2):
                question_display = gr.Textbox(label="Question", interactive=False)
                answer_input = gr.Textbox(
                    label="Your Answer", placeholder="Type your answer here"
                )
                result_display = gr.Textbox(label="Result", interactive=False)

            with gr.Column(scale=1):
                score_display = gr.Number(label="Score", value=0, interactive=False)
                total_display = gr.Number(
                    label="Total Questions", value=0, interactive=False
                )

        with gr.Row():
            new_question_btn = gr.Button("Get New Question")
            submit_btn = gr.Button("Submit Answer", variant="primary")
            reset_btn = gr.Button("Reset Quiz")

        # Event handlers
        new_question_btn.click(
            quiz.get_new_question, outputs=[question_display, answer_input]
        )

        submit_btn.click(
            quiz.check_answer,
            inputs=[answer_input],
            outputs=[result_display, score_display, total_display],
        )

        def reset_quiz():
            quiz.__init__()
            return "", "", "", 0, 0

        reset_btn.click(
            reset_quiz,
            outputs=[
                question_display,
                answer_input,
                result_display,
                score_display,
                total_display,
            ],
        )

    return app


if __name__ == "__main__":
    app = create_trivia_app()
    app.launch()