File size: 1,647 Bytes
a0b3a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
import gradio as gr
from smolagents import tool, CodeAgent, HfApiModel

# Sample questions and answers
questions = [
    {
        "question": "What is the capital of France?",
        "choices": ["Paris", "London", "Berlin", "Madrid"],
        "answer": "Paris"
    },
    {
        "question": "What is 2 + 2?",
        "choices": ["3", "4", "5", "6"],
        "answer": "4"
    }
]


@tool
def qcm_test(user_answer: str) -> str:
    """
    This tool selects a random question from a predefined list and asks the user to pick an option.
    It returns whether the user's answer is correct or not.

    Args:
        user_answer: The user's selected answer.
    """
    # Select a random question
    question = random.choice(questions)

    # Check if the user's answer is correct
    if user_answer.strip() == question['answer']:
        return "Correct!"
    else:
        return f"Incorrect. The correct answer is {question['answer']}."


# Initialize the agent with the QCM test tool
model = HfApiModel()
agent = CodeAgent(tools=[qcm_test], model=model)


# Define the Gradio interface
def gradio_qcm_test():
    question = random.choice(questions)
    user_answer = gr.Dropdown(label="Select your answer", choices=question['choices'])
    result = qcm_test(user_answer)
    return question['question'], result


# Create the Gradio interface
iface = gr.Interface(
    fn=gradio_qcm_test,
    inputs=None,
    outputs=[gr.Textbox(label="Question"), gr.Textbox(label="Result")],
    title="QCM Test",
    description="Select an answer to the question and see if you are correct."
)

# Launch the Gradio interface
iface.launch()