Ilayda-j's picture
Update app.py
27874a2
raw
history blame
1.79 kB
import gradio as gr
questions = [
{"question": "Who is the narrator in 'The Road Not Taken'?", "options": ["A young boy", "An old man", "A traveler", "None of the above"], "correct_option": 2},
{"question": "What is the setting of the poem?", "options": ["A city", "A forest", "A beach", "A mountain"], "correct_option": 1},
{"question": "What is the underlying message of the poem?", "options": ["The importance of making choices", "The beauty of nature", "The joy of youth", "The value of time"], "correct_option": 0}
]
def interactive_quiz(user_response=None, continue_quiz='yes', question_index=0):
output_messages = []
# Get the current question
current_question = questions[question_index]
output_messages.append(f"Question {question_index + 1}: {current_question['question']}")
for i, option in enumerate(current_question['options']):
output_messages.append(f"{i}. {option}")
if user_response is not None:
user_response = int(user_response)
if user_response == current_question['correct_option']:
output_messages.append("Correct!")
else:
output_messages.append(f"Incorrect. The correct answer was: {current_question['options'][current_question['correct_option']]}")
if continue_quiz.lower() == 'no':
output_messages.append("Thank you for participating in the quiz!")
question_index = 0 # Reset the question index for the next session
else:
question_index = (question_index + 1) % len(questions) # Move to the next question
return "\n".join(output_messages), question_index
iface = gr.Interface(
fn=interactive_quiz,
inputs=["text", "text", "number"],
outputs=["text", "number"],
live=True
)
iface.launch()