AamerAkhter commited on
Commit
8c7402e
Β·
verified Β·
1 Parent(s): dd2c412

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -61
app.py CHANGED
@@ -1,77 +1,92 @@
1
  import streamlit as st
 
2
  import time
 
3
 
4
- # Sample quiz data (can be generated dynamically via Hugging Face)
5
- quiz_data = [
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 Quiz App", layout="centered")
19
- st.title("🧠 AI-Powered Quiz with Timer & Scoring")
20
 
21
- # Timer per question (in seconds)
22
- QUESTION_TIMER = 15
23
 
24
- # Session state to store progress
25
- if 'current_q' not in st.session_state:
26
- st.session_state.current_q = 0
27
- st.session_state.score = 0
28
- st.session_state.start_time = time.time()
 
 
 
29
 
30
- current_question = quiz_data[st.session_state.current_q]
31
- elapsed = time.time() - st.session_state.start_time
32
- time_left = max(0, int(QUESTION_TIMER - elapsed))
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # Display timer
35
- st.info(f"⏱️ Time Left: {time_left} seconds")
36
- if time_left == 0:
37
- st.warning("⏰ Time's up! Moving to next question...")
38
- st.session_state.current_q += 1
39
- st.session_state.start_time = time.time()
40
- st.experimental_rerun()
41
 
42
- # Card-style container
43
- with st.container():
44
- st.markdown("### ❓ Question:")
45
- st.markdown(f"**{current_question['question']}**")
46
- selected = st.radio(
47
- "Choose your answer:",
48
- current_question["options"],
49
- index=None,
50
- key=st.session_state.current_q
51
- )
52
 
53
- # Submit button
54
- if st.button("Submit Answer"):
55
- if selected:
56
- if selected == current_question["answer"]:
57
- st.success("βœ… Correct!")
58
- st.session_state.score += 1
59
- else:
60
- st.error(f"❌ Incorrect! Correct answer: {current_question['answer']}")
61
-
62
- time.sleep(1.5)
 
 
 
 
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
- # Quiz End
70
- if st.session_state.current_q >= len(quiz_data):
71
- st.balloons()
72
- st.success(f"πŸŽ‰ Quiz Completed! Your score: {st.session_state.score}/{len(quiz_data)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  if st.button("Restart"):
74
- st.session_state.current_q = 0
75
- st.session_state.score = 0
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()