File size: 2,337 Bytes
d1b18b2
4387bd9
d1b18b2
4387bd9
 
 
 
 
 
 
 
 
 
 
 
 
d1b18b2
4387bd9
 
d1b18b2
4387bd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bacf6db
4387bd9
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import time

# Sample quiz data (can be generated dynamically via Hugging Face)
quiz_data = [
    {
        "question": "What is the capital of France?",
        "options": ["Berlin", "Paris", "Madrid", "Rome"],
        "answer": "Paris"
    },
    {
        "question": "What is 2 + 2?",
        "options": ["3", "4", "5", "6"],
        "answer": "4"
    }
]

st.set_page_config(page_title="AI Quiz App", layout="centered")
st.title("🧠 AI-Powered Quiz with Timer & Scoring")

# Timer per question (in seconds)
QUESTION_TIMER = 15

# Session state to store progress
if 'current_q' not in st.session_state:
    st.session_state.current_q = 0
    st.session_state.score = 0
    st.session_state.start_time = time.time()

current_question = quiz_data[st.session_state.current_q]
elapsed = time.time() - st.session_state.start_time
time_left = max(0, int(QUESTION_TIMER - elapsed))

# Display timer
st.info(f"⏱️ Time Left: {time_left} seconds")
if time_left == 0:
    st.warning("⏰ Time's up! Moving to next question...")
    st.session_state.current_q += 1
    st.session_state.start_time = time.time()
    st.experimental_rerun()

# Card-style container
with st.container():
    st.markdown("### ❓ Question:")
    st.markdown(f"**{current_question['question']}**")
    selected = st.radio(
        "Choose your answer:",
        current_question["options"],
        index=None,
        key=st.session_state.current_q
    )

# Submit button
if st.button("Submit Answer"):
    if selected:
        if selected == current_question["answer"]:
            st.success("βœ… Correct!")
            st.session_state.score += 1
        else:
            st.error(f"❌ Incorrect! Correct answer: {current_question['answer']}")
        
        time.sleep(1.5)
        st.session_state.current_q += 1
        st.session_state.start_time = time.time()
        st.experimental_rerun()
    else:
        st.warning("Please select an option before submitting.")

# Quiz End
if st.session_state.current_q >= len(quiz_data):
    st.balloons()
    st.success(f"πŸŽ‰ Quiz Completed! Your score: {st.session_state.score}/{len(quiz_data)}")
    if st.button("Restart"):
        st.session_state.current_q = 0
        st.session_state.score = 0
        st.session_state.start_time = time.time()
        st.experimental_rerun()