Spaces:
Running
Running
File size: 4,448 Bytes
4d0f22a 471642c fb0dcea 4105a9d 471642c 4105a9d 471642c 4d0f22a 471642c b66e4e3 471642c b66e4e3 471642c 4105a9d fb0dcea 4105a9d |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import streamlit as st
from mcq_generator import AdvancedMCQGenerator
from short_answer_generator import QuestionGenerator
from truefalse_quiz import generate_true_false
import io
# Set page config at the top
st.set_page_config(page_title="QuizCraft AI", layout="centered")
# App title and intro
st.title("π QuizCraft AI")
st.markdown("Generate intelligent quizzes from any context using AI. Choose the type, level, and number of questions!")
# Input section
context = st.text_area("π Enter your context/text here:", height=100)
col1, col2 = st.columns(2)
question_type = col1.selectbox("Question Type", ["Multiple Choice", "Short Answer", "True/False"])
difficulty = col2.selectbox("Difficulty", ["easy", "medium", "hard"])
num_questions = st.slider("π’ Number of Questions", min_value=1, max_value=10, value=3)
#<<<<<<< main
#=======
#>>>>>>> main
# Generate button
if st.button("β‘ Generate Quiz"):
if not context.strip():
st.warning("Please enter some context/text to generate questions.")
else:
with st.spinner("Generating quiz..."):
output = io.StringIO() # For optional export
questions = []
if question_type == "Multiple Choice":
generator = AdvancedMCQGenerator()
try:
questions = generator.generate_mcq(context, num_questions=num_questions, difficulty=difficulty)
st.subheader("π Multiple Choice Questions")
for idx, q in enumerate(questions, 1):
st.markdown(f"**Q{idx}: {q['question']}**")
for i, option in enumerate(q['options']):
st.markdown(f"- {chr(65+i)}. {option}")
st.markdown(f"π’ **Answer:** {chr(65 + q['correct_answer'])}\n\n---")
# Export text
output.write(f"Q{idx}: {q['question']}\n")
for i, option in enumerate(q['options']):
output.write(f" {chr(65+i)}. {option}\n")
output.write(f"Answer: {chr(65 + q['correct_answer'])}\n\n")
except Exception as e:
st.error(f"β Failed to generate MCQs: {str(e)}")
elif question_type == "Short Answer":
try:
generator = QuestionGenerator()
questions = generator.generate_questions(context, num_questions=num_questions, difficulty=difficulty)
st.subheader("π Short Answer Questions")
for idx, q in enumerate(questions, 1):
st.markdown(f"**Q{idx}: {q['question']}**")
st.markdown(f"π’ **Expected Keyword:** {q['answer']}")
st.markdown("---")
# Export text
output.write(f"Q{idx}: {q['question']}\nExpected keyword: {q['answer']}\n\n")
except Exception as e:
st.error(f"β Failed to generate short answer questions: {str(e)}")
elif question_type == "True/False":
try:
st.subheader("β
True/False Questions")
tf_generator = generate_true_false() # Initialize the class
sentences = tf_generator.validate_inputs(context, num_questions, difficulty)
questions = tf_generator.generate_statements(context, num_questions, difficulty, sentences)
for idx, (statement, label) in enumerate(questions, 1):
st.markdown(f"**Q{idx}: {statement}**")
st.markdown(f"π’ **Answer:** {'True' if label == 'ENTAILMENT' else 'False'}")
st.markdown("---")
# Export text
output.write(f"Q{idx}: {statement}\nAnswer: {'True' if label == 'ENTAILMENT' else 'False'}\n\n")
except Exception as e:
st.error(f"β Failed to generate true/false questions: {str(e)}")
# Download button if questions were generated
if questions:
st.download_button("β¬οΈ Download Quiz as PDF", output.getvalue(), file_name="quizcraft_quiz.pdf")
#<<<<<<< main
#=======
#>>>>>>> main
|