Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,92 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import time
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
{
|
| 7 |
-
"question": "What is the capital of France?",
|
| 8 |
-
"options": ["Berlin", "Paris", "Madrid", "Rome"],
|
| 9 |
-
"answer": "Paris"
|
| 10 |
-
},
|
| 11 |
-
{
|
| 12 |
-
"question": "What is 2 + 2?",
|
| 13 |
-
"options": ["3", "4", "5", "6"],
|
| 14 |
-
"answer": "4"
|
| 15 |
-
}
|
| 16 |
-
]
|
| 17 |
|
| 18 |
-
st.set_page_config(page_title="AI
|
| 19 |
-
st.title("π§
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
if
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
st.session_state.start_time = time.time()
|
| 40 |
-
st.experimental_rerun()
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
current_question["options"],
|
| 49 |
-
index=None,
|
| 50 |
-
key=st.session_state.current_q
|
| 51 |
-
)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
if st.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
st.session_state.current_q += 1
|
| 64 |
st.session_state.start_time = time.time()
|
| 65 |
st.experimental_rerun()
|
| 66 |
-
else:
|
| 67 |
-
st.warning("Please select an option before submitting.")
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
st.
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if st.button("Restart"):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
st.session_state.start_time = time.time()
|
| 77 |
st.experimental_rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
import time
|
| 4 |
+
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# Load question generation pipeline
|
| 7 |
+
qg_pipeline = pipeline("text2text-generation", model="iarfmoose/t5-base-question-generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
st.set_page_config(page_title="AI MCQ Quiz", layout="centered")
|
| 10 |
+
st.title("π§ Auto-Generated AI Quiz")
|
| 11 |
|
| 12 |
+
# Step 1: User input
|
| 13 |
+
input_text = st.text_area("Enter a topic or paragraph:")
|
| 14 |
|
| 15 |
+
if st.button("Generate Quiz"):
|
| 16 |
+
if not input_text.strip():
|
| 17 |
+
st.warning("Please enter some text first.")
|
| 18 |
+
else:
|
| 19 |
+
with st.spinner("Generating quiz questions..."):
|
| 20 |
+
result = qg_pipeline(f"generate questions: {input_text}", max_length=512, do_sample=True)
|
| 21 |
+
raw_questions = result[0]['generated_text'].split("\n")
|
| 22 |
+
quiz_data = []
|
| 23 |
|
| 24 |
+
for q in raw_questions:
|
| 25 |
+
if q.strip() and "?" in q:
|
| 26 |
+
question = q.strip()
|
| 27 |
+
correct = "Not provided" # Placeholder (you can use a separate model for answer extraction)
|
| 28 |
+
options = [correct]
|
| 29 |
+
# Generate 3 fake options (distractors)
|
| 30 |
+
fake_answers = [
|
| 31 |
+
"Option A", "Option B", "Option C", "Option D", "None of these",
|
| 32 |
+
"Random guess", "All of the above", "Incorrect answer"
|
| 33 |
+
]
|
| 34 |
+
while len(options) < 4:
|
| 35 |
+
option = random.choice(fake_answers)
|
| 36 |
+
if option not in options:
|
| 37 |
+
options.append(option)
|
| 38 |
+
random.shuffle(options)
|
| 39 |
|
| 40 |
+
quiz_data.append({
|
| 41 |
+
"question": question,
|
| 42 |
+
"options": options,
|
| 43 |
+
"answer": correct # For now this is unknown
|
| 44 |
+
})
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
if quiz_data:
|
| 47 |
+
st.session_state.quiz = quiz_data
|
| 48 |
+
st.session_state.current_q = 0
|
| 49 |
+
st.session_state.score = 0
|
| 50 |
+
st.session_state.start_time = time.time()
|
| 51 |
+
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Proceed to quiz if it's generated
|
| 54 |
+
if "quiz" in st.session_state and st.session_state.current_q < len(st.session_state.quiz):
|
| 55 |
+
quiz = st.session_state.quiz
|
| 56 |
+
q_index = st.session_state.current_q
|
| 57 |
+
q_data = quiz[q_index]
|
| 58 |
+
|
| 59 |
+
st.markdown(f"### β Question {q_index + 1}:")
|
| 60 |
+
st.markdown(f"**{q_data['question']}**")
|
| 61 |
+
|
| 62 |
+
time_left = max(0, 15 - int(time.time() - st.session_state.start_time))
|
| 63 |
+
st.info(f"β±οΈ Time left: {time_left} seconds")
|
| 64 |
+
|
| 65 |
+
if time_left == 0:
|
| 66 |
+
st.warning("β° Time's up! Moving to next question...")
|
| 67 |
st.session_state.current_q += 1
|
| 68 |
st.session_state.start_time = time.time()
|
| 69 |
st.experimental_rerun()
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
selected = st.radio("Your Answer:", q_data["options"], index=None, key=q_index)
|
| 72 |
+
|
| 73 |
+
if st.button("Submit Answer"):
|
| 74 |
+
if selected:
|
| 75 |
+
if selected == q_data["answer"]:
|
| 76 |
+
st.success("β
Correct!")
|
| 77 |
+
st.session_state.score += 1
|
| 78 |
+
else:
|
| 79 |
+
st.error(f"β Incorrect! Correct: {q_data['answer']}")
|
| 80 |
+
time.sleep(1.5)
|
| 81 |
+
st.session_state.current_q += 1
|
| 82 |
+
st.session_state.start_time = time.time()
|
| 83 |
+
st.experimental_rerun()
|
| 84 |
+
else:
|
| 85 |
+
st.warning("Please choose an option.")
|
| 86 |
+
|
| 87 |
+
elif "quiz" in st.session_state and st.session_state.current_q >= len(st.session_state.quiz):
|
| 88 |
+
st.success(f"π Quiz completed! Score: {st.session_state.score}/{len(st.session_state.quiz)}")
|
| 89 |
if st.button("Restart"):
|
| 90 |
+
for key in ["quiz", "current_q", "score", "start_time"]:
|
| 91 |
+
st.session_state.pop(key, None)
|
|
|
|
| 92 |
st.experimental_rerun()
|