Quiz_Generator / app.py
Amandeep01's picture
Update app.py
4a01c06 verified
import gradio as gr
import random
# Predefined quiz data for different topics
questions_data = {
"geography": [
{"question": "What is the capital of France?", "options": ["Berlin", "Madrid", "Paris", "Rome"], "answer": "Paris"},
{"question": "Which is the largest continent?", "options": ["Asia", "Africa", "Europe", "Australia"], "answer": "Asia"},
{"question": "What is the tallest mountain?", "options": ["Mount Everest", "K2", "Kangchenjunga", "Makalu"], "answer": "Mount Everest"}
],
"science": [
{"question": "What is the chemical symbol for water?", "options": ["O2", "CO2", "H2O", "HO2"], "answer": "H2O"},
{"question": "What planet is known as the Red Planet?", "options": ["Mars", "Venus", "Jupiter", "Saturn"], "answer": "Mars"},
{"question": "What gas do plants absorb from the atmosphere?", "options": ["Oxygen", "Nitrogen", "Carbon Dioxide", "Hydrogen"], "answer": "Carbon Dioxide"}
],
"math": [
{"question": "What is 5 + 5?", "options": ["10", "12", "14", "15"], "answer": "10"},
{"question": "What is 12 * 12?", "options": ["144", "100", "130", "110"], "answer": "144"},
{"question": "What is the square root of 64?", "options": ["6", "8", "10", "12"], "answer": "8"}
]
}
# Function to generate quiz based on selected topic
def generate_quiz(topic):
# Normalize the topic input to lowercase for comparison
topic = topic.strip().lower()
# Check if the topic exists in predefined questions
if topic in questions_data:
quiz_questions = random.sample(questions_data[topic], 3)
else:
# If topic is not predefined, allow user-defined content (Example: Glacier)
quiz_questions = [{"question": f"Custom question for {topic}", "options": ["Option A", "Option B", "Option C", "Option D"], "answer": "Option A"}]
quiz_result = []
for q in quiz_questions:
quiz_result.append({
"question": q["question"],
"options": q["options"],
"correct_answer": q["answer"]
})
return quiz_result
# Gradio interface setup
iface = gr.Interface(
fn=generate_quiz,
inputs=gr.Textbox(placeholder="Enter a topic for 10th standard 'Geography', 'Science', 'Math', or custom like 'Glacier'", label="Topic"),
outputs=gr.JSON(label="Quiz Questions"),
title="Particular Subjectwise Quiz Generator",
description="Enter a topic and get a quiz. If the topic is not predefined, custom questions will be generated."
)
iface.launch()