FRM_Study_chatbot / QCMTest.py
tiantian-paris's picture
test
a0b3a97
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()