Spaces:
Sleeping
Sleeping
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() | |