File size: 1,791 Bytes
41a41ff
 
 
 
 
 
 
 
 
 
27874a2
 
41a41ff
27874a2
 
 
41a41ff
 
 
 
 
 
 
 
27874a2
 
 
 
 
41a41ff
 
 
24c1fa1
41a41ff
 
 
 
24c1fa1
 
 
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
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()