|
import random |
|
import gradio as gr |
|
from smolagents import tool, CodeAgent, HfApiModel |
|
|
|
|
|
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. |
|
""" |
|
|
|
question = random.choice(questions) |
|
|
|
|
|
if user_answer.strip() == question['answer']: |
|
return "Correct!" |
|
else: |
|
return f"Incorrect. The correct answer is {question['answer']}." |
|
|
|
|
|
|
|
model = HfApiModel() |
|
agent = CodeAgent(tools=[qcm_test], model=model) |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
iface.launch() |