AamerAkhter commited on
Commit
4387bd9
Β·
verified Β·
1 Parent(s): f7e0a5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -15
app.py CHANGED
@@ -1,20 +1,77 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
 
4
- # Load the question generation model
5
- qg_pipeline = pipeline("text2text-generation", model="iarfmoose/t5-base-question-generator")
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- st.title("AI-Powered Quiz Generator")
8
- text = st.text_area("Enter a paragraph to generate quiz questions:")
9
 
10
- if st.button("Generate Questions"):
11
- if text.strip() == "":
12
- st.warning("Please enter some text to generate questions.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  else:
14
- with st.spinner("Generating questions..."):
15
- result = qg_pipeline(f"generate questions: {text}", max_length=512, do_sample=True, temperature=0.7)
16
- questions = result[0]['generated_text'].split("\n")
17
- st.subheader("Generated Questions:")
18
- for i, q in enumerate(questions):
19
- if q.strip():
20
- st.markdown(f"**Q{i+1}: {q.strip()}**")
 
 
 
 
 
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()