Spaces:
Sleeping
Sleeping
import streamlit as st | |
from langchain_groq import ChatGroq | |
import json | |
# Application Title and Subheader | |
st.title('AI Quiz Generator') | |
st.subheader('Test Yourself!') | |
# Input Fields for Standard and Topic | |
A = st.text_input('Enter standard') | |
B = st.text_input('Enter a topic') | |
# Initialize the ChatGroq Model (replace with your actual API key) | |
model = ChatGroq( | |
temperature=0.6, | |
groq_api_key='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' | |
) | |
def generate_quiz(standard, topic, difficulty): | |
""" | |
Generate a quiz with 5 multiple choice questions based on the difficulty level. | |
Returns structured JSON output. | |
""" | |
prompt = ( | |
f"Generate a quiz with exactly 5 multiple choice questions for a {standard} student on the topic of {topic}. " | |
"Each question should have four options and one correct answer. " | |
"Return the output in JSON format with the following structure: " | |
"{ \"questions\": [ {\"question\": \"...\", \"options\": [\"...\", \"...\", \"...\", \"...\"], \"answer\": \"...\"} ] }" | |
) | |
response = model.predict(prompt) | |
try: | |
return json.loads(response) # Safely parse JSON response | |
except json.JSONDecodeError: | |
st.error("Failed to parse quiz. Please try again.") | |
return None | |
def calculate_score(user_answers, correct_answers): | |
"""Compare user answers with the correct answers and calculate the score.""" | |
return sum(1 for q, ans in user_answers.items() if correct_answers.get(q) == ans) | |
# Session state to store quiz data | |
if 'quiz_data' not in st.session_state: | |
st.session_state.quiz_data = None | |
if 'user_answers' not in st.session_state: | |
st.session_state.user_answers = {} | |
# Difficulty Selection | |
difficulty_levels = ["Low", "Medium", "Hard"] | |
difficulty = st.radio("Select Difficulty Level", difficulty_levels) | |
# Generate Quiz Button | |
if st.button("Generate Quiz"): | |
if A and B: | |
st.session_state.quiz_data = generate_quiz(A, B, difficulty) | |
if st.session_state.quiz_data: | |
st.session_state.user_answers = {} # Reset user answers | |
st.success("Quiz Generated!") | |
else: | |
st.error("Please enter both the standard and topic.") | |
# Display Quiz if Available | |
if st.session_state.quiz_data: | |
st.write("### Quiz Questions:") | |
questions = st.session_state.quiz_data.get("questions", []) | |
for i, q in enumerate(questions[:5]): # Limit to 5 questions | |
st.write(f"**Q{i+1}: {q['question']}**") | |
user_answer = st.radio( | |
f"Select your answer for Question {i+1}", | |
options=q["options"], | |
key=f"question_{i+1}" | |
) | |
st.session_state.user_answers[f"question_{i+1}"] = user_answer | |
# Submit Quiz Button | |
if st.button("Submit Quiz"): | |
if st.session_state.quiz_data: | |
correct_answers = {f"question_{i+1}": q["answer"] for i, q in enumerate(st.session_state.quiz_data["questions"][:5])} | |
score = calculate_score(st.session_state.user_answers, correct_answers) | |
st.write("### Quiz Results") | |
st.write(f"Your answers: {st.session_state.user_answers}") | |
st.write(f"Correct answers: {correct_answers}") | |
st.write(f"Your score: {score}/5") | |
st.success("Great job! Keep practicing!") | |
else: | |
st.error("Quiz data not available. Please regenerate the quiz.") | |