Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,51 +2,71 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
import random
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
st.title("π Text-to-Quiz Generator")
|
8 |
-
st.write("Enter a passage below and let AI generate a quiz for you!")
|
9 |
-
|
10 |
-
# Text input
|
11 |
-
text_input = st.text_area("Paste your text here:", height=200)
|
12 |
-
|
13 |
-
# Load the pipeline
|
14 |
@st.cache_resource
|
15 |
def load_pipeline():
|
16 |
return pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
|
17 |
|
|
|
18 |
qg_pipeline = load_pipeline()
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
return [res['generated_text'] for res in results]
|
25 |
-
|
26 |
-
# Interface
|
27 |
-
if st.button("Generate Quiz") and text_input:
|
28 |
-
st.subheader("π Generated Quiz")
|
29 |
-
|
30 |
-
try:
|
31 |
-
generated_questions = generate_questions(text_input)
|
32 |
-
|
33 |
-
for i, q in enumerate(generated_questions):
|
34 |
-
# You can split question and answer if model formats them well
|
35 |
-
question = q.split("?")[0] + "?"
|
36 |
-
correct_answer = "Answer based on context" # Placeholder
|
37 |
-
choices = [correct_answer, "Random Option A", "Random Option B", "Random Option C"]
|
38 |
-
random.shuffle(choices)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
import random
|
4 |
|
5 |
+
# Load the pipeline with caching
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
@st.cache_resource
|
7 |
def load_pipeline():
|
8 |
return pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
|
9 |
|
10 |
+
# Initialize the pipeline
|
11 |
qg_pipeline = load_pipeline()
|
12 |
|
13 |
+
# UI Header
|
14 |
+
st.markdown("# Text-to-Quiz Generator")
|
15 |
+
st.markdown("Enter a passage below and let AI create an interactive quiz for you!")
|
16 |
+
st.markdown("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Input section
|
19 |
+
passage = st.text_area("Paste your text here:", height=150)
|
20 |
|
21 |
+
if st.button("Generate Quiz"):
|
22 |
+
with st.spinner("Generating your quiz..."):
|
23 |
+
# Generate question-answer pairs (adjust based on actual model output format)
|
24 |
+
generated = qg_pipeline(passage, max_length=512)
|
25 |
+
|
26 |
+
# For this example, assume the model outputs a list of strings like "Q: ... A: ..."
|
27 |
+
# Parse the output into questions and answers (modify this based on your model's actual output)
|
28 |
+
quiz_data = []
|
29 |
+
for item in generated:
|
30 |
+
if "Q:" in item["generated_text"] and "A:" in item["generated_text"]:
|
31 |
+
q, a = item["generated_text"].split("A:")
|
32 |
+
question = q.replace("Q:", "").strip()
|
33 |
+
answer = a.strip()
|
34 |
+
quiz_data.append({"question": question, "answer": answer})
|
35 |
+
|
36 |
+
if not quiz_data:
|
37 |
+
st.error("No valid questions generated. Please try a different passage.")
|
38 |
+
else:
|
39 |
+
# Store quiz data in session state
|
40 |
+
st.session_state["quiz_data"] = quiz_data
|
41 |
+
st.session_state["user_answers"] = {}
|
42 |
|
43 |
+
# Display quiz
|
44 |
+
if "quiz_data" in st.session_state:
|
45 |
+
st.markdown("### Your Quiz")
|
46 |
+
questions = [item["question"] for item in st.session_state["quiz_data"]]
|
47 |
+
answers = [item["answer"] for item in st.session_state["quiz_data"]]
|
48 |
+
|
49 |
+
# Generate distractors for each question
|
50 |
+
for i, question in enumerate(questions):
|
51 |
+
correct_answer = answers[i]
|
52 |
+
distractors = [a for j, a in enumerate(answers) if j != i]
|
53 |
+
options = [correct_answer] + random.sample(distractors, min(3, len(distractors)))
|
54 |
+
random.shuffle(options)
|
55 |
+
|
56 |
+
st.write(f"**Question {i+1}:** {question}")
|
57 |
+
st.session_state["user_answers"][question] = st.radio(
|
58 |
+
"Select your answer:", options, key=f"q{i}"
|
59 |
+
)
|
60 |
+
|
61 |
+
# Submit button
|
62 |
+
if st.button("Submit Answers"):
|
63 |
+
score = 0
|
64 |
+
for i, question in enumerate(questions):
|
65 |
+
user_answer = st.session_state["user_answers"][question]
|
66 |
+
correct_answer = answers[i]
|
67 |
+
if user_answer == correct_answer:
|
68 |
+
st.success(f"**Question {i+1}:** Correct! The answer is '{correct_answer}'.")
|
69 |
+
score += 1
|
70 |
+
else:
|
71 |
+
st.error(f"**Question {i+1}:** Incorrect. The correct answer is '{correct_answer}', not '{user_answer}'.")
|
72 |
+
st.markdown(f"### Your Score: {score}/{len(questions)}")
|