Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import streamlit as st
|
|
| 2 |
import random
|
| 3 |
|
| 4 |
# ------------------------------
|
| 5 |
-
# Quiz Data
|
| 6 |
# ------------------------------
|
| 7 |
quizzes = {
|
| 8 |
"Science": [
|
|
@@ -55,137 +55,101 @@ quizzes = {
|
|
| 55 |
{"question": "What does AI stand for?",
|
| 56 |
"options": ["Auto Input", "Artificial Intelligence", "Auto Intelligence", "Android Interface"],
|
| 57 |
"answer": "Artificial Intelligence"},
|
| 58 |
-
],
|
| 59 |
-
"Literature": [
|
| 60 |
-
{"question": "Who wrote 'Hamlet'?",
|
| 61 |
-
"options": ["Shakespeare", "Chaucer", "Shelley", "Austen"],
|
| 62 |
-
"answer": "Shakespeare"},
|
| 63 |
-
{"question": "What is the main language in 'Les MisΓ©rables'?",
|
| 64 |
-
"options": ["Spanish", "English", "French", "German"],
|
| 65 |
-
"answer": "French"},
|
| 66 |
-
{"question": "Which novel starts with 'Call me Ishmael'?",
|
| 67 |
-
"options": ["Moby Dick", "The Odyssey", "Ulysses", "Don Quixote"],
|
| 68 |
-
"answer": "Moby Dick"},
|
| 69 |
-
{"question": "Who wrote 'Pride and Prejudice'?",
|
| 70 |
-
"options": ["Jane Austen", "Emily BrontΓ«", "George Eliot", "Mary Shelley"],
|
| 71 |
-
"answer": "Jane Austen"},
|
| 72 |
-
{"question": "Which author created Sherlock Holmes?",
|
| 73 |
-
"options": ["Agatha Christie", "J.K. Rowling", "Arthur Conan Doyle", "Dan Brown"],
|
| 74 |
-
"answer": "Arthur Conan Doyle"},
|
| 75 |
-
],
|
| 76 |
-
"History": [
|
| 77 |
-
{"question": "Who was the first President of the USA?",
|
| 78 |
-
"options": ["Thomas Jefferson", "Abraham Lincoln", "George Washington", "John Adams"],
|
| 79 |
-
"answer": "George Washington"},
|
| 80 |
-
{"question": "In which year did World War II end?",
|
| 81 |
-
"options": ["1940", "1945", "1939", "1950"],
|
| 82 |
-
"answer": "1945"},
|
| 83 |
-
{"question": "Which civilization built the pyramids?",
|
| 84 |
-
"options": ["Romans", "Greeks", "Egyptians", "Mayans"],
|
| 85 |
-
"answer": "Egyptians"},
|
| 86 |
-
{"question": "Who discovered America?",
|
| 87 |
-
"options": ["Vasco da Gama", "Christopher Columbus", "Marco Polo", "Ferdinand Magellan"],
|
| 88 |
-
"answer": "Christopher Columbus"},
|
| 89 |
-
{"question": "Where did the Industrial Revolution begin?",
|
| 90 |
-
"options": ["USA", "Germany", "England", "France"],
|
| 91 |
-
"answer": "England"},
|
| 92 |
-
],
|
| 93 |
-
"General Knowledge": [
|
| 94 |
-
{"question": "What is the capital of Japan?",
|
| 95 |
-
"options": ["Kyoto", "Tokyo", "Osaka", "Hiroshima"],
|
| 96 |
-
"answer": "Tokyo"},
|
| 97 |
-
{"question": "Which ocean is the largest?",
|
| 98 |
-
"options": ["Atlantic", "Indian", "Pacific", "Arctic"],
|
| 99 |
-
"answer": "Pacific"},
|
| 100 |
-
{"question": "How many continents are there?",
|
| 101 |
-
"options": ["5", "6", "7", "8"],
|
| 102 |
-
"answer": "7"},
|
| 103 |
-
{"question": "Which country is known as the Land of the Rising Sun?",
|
| 104 |
-
"options": ["China", "Japan", "India", "South Korea"],
|
| 105 |
-
"answer": "Japan"},
|
| 106 |
-
{"question": "How many days are there in a leap year?",
|
| 107 |
-
"options": ["364", "365", "366", "367"],
|
| 108 |
-
"answer": "366"},
|
| 109 |
]
|
|
|
|
| 110 |
}
|
| 111 |
|
| 112 |
# ------------------------------
|
| 113 |
-
#
|
| 114 |
# ------------------------------
|
| 115 |
def load_questions(category):
|
| 116 |
return random.sample(quizzes[category], 5)
|
| 117 |
|
| 118 |
-
def evaluate(questions
|
| 119 |
score = 0
|
| 120 |
results = []
|
| 121 |
-
for
|
|
|
|
| 122 |
correct = q["answer"]
|
| 123 |
-
is_correct =
|
| 124 |
if is_correct:
|
| 125 |
score += 1
|
| 126 |
-
results.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
return score, results
|
| 128 |
|
| 129 |
def show_feedback(score):
|
| 130 |
if score == 5:
|
| 131 |
-
st.success("π
|
| 132 |
elif score >= 3:
|
| 133 |
-
st.info(f"π Good
|
| 134 |
else:
|
| 135 |
-
st.warning(f"
|
| 136 |
|
| 137 |
# ------------------------------
|
| 138 |
-
#
|
| 139 |
# ------------------------------
|
| 140 |
st.set_page_config(page_title="BrainQuest AI", layout="centered")
|
| 141 |
|
| 142 |
with st.sidebar:
|
| 143 |
st.title("βΉοΈ About")
|
| 144 |
st.markdown("""
|
| 145 |
-
**π BrainQuest AI**
|
| 146 |
-
|
| 147 |
-
-
|
| 148 |
-
-
|
| 149 |
-
-
|
|
|
|
| 150 |
""")
|
| 151 |
|
| 152 |
-
st.
|
| 153 |
st.subheader("Test your knowledge across various fields")
|
| 154 |
st.markdown("---")
|
| 155 |
|
| 156 |
-
#
|
| 157 |
if "questions" not in st.session_state:
|
| 158 |
st.session_state.questions = []
|
| 159 |
-
st.session_state.
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
category = st.selectbox("
|
| 163 |
|
| 164 |
if st.button("π Start Quiz"):
|
| 165 |
st.session_state.questions = load_questions(category)
|
| 166 |
-
st.session_state.
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
st.markdown("---")
|
| 174 |
|
| 175 |
if st.button("Submit Answers"):
|
| 176 |
-
|
|
|
|
| 177 |
show_feedback(score)
|
| 178 |
|
| 179 |
-
st.markdown("##
|
| 180 |
-
for i,
|
| 181 |
-
emoji = "β
" if
|
| 182 |
-
st.markdown(f"**Q{i+1}:** {question}")
|
| 183 |
-
st.markdown(f"- Your answer: {
|
| 184 |
-
if not
|
| 185 |
-
st.markdown(f"- Correct answer: {correct}")
|
| 186 |
st.markdown("---")
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
| 2 |
import random
|
| 3 |
|
| 4 |
# ------------------------------
|
| 5 |
+
# Quiz Data
|
| 6 |
# ------------------------------
|
| 7 |
quizzes = {
|
| 8 |
"Science": [
|
|
|
|
| 55 |
{"question": "What does AI stand for?",
|
| 56 |
"options": ["Auto Input", "Artificial Intelligence", "Auto Intelligence", "Android Interface"],
|
| 57 |
"answer": "Artificial Intelligence"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
]
|
| 59 |
+
# Add more categories if needed
|
| 60 |
}
|
| 61 |
|
| 62 |
# ------------------------------
|
| 63 |
+
# Functions
|
| 64 |
# ------------------------------
|
| 65 |
def load_questions(category):
|
| 66 |
return random.sample(quizzes[category], 5)
|
| 67 |
|
| 68 |
+
def evaluate(questions):
|
| 69 |
score = 0
|
| 70 |
results = []
|
| 71 |
+
for i, q in enumerate(questions):
|
| 72 |
+
selected = st.session_state.get(f"answer_{i}")
|
| 73 |
correct = q["answer"]
|
| 74 |
+
is_correct = selected == correct
|
| 75 |
if is_correct:
|
| 76 |
score += 1
|
| 77 |
+
results.append({
|
| 78 |
+
"question": q["question"],
|
| 79 |
+
"selected": selected,
|
| 80 |
+
"correct": correct,
|
| 81 |
+
"is_correct": is_correct
|
| 82 |
+
})
|
| 83 |
return score, results
|
| 84 |
|
| 85 |
def show_feedback(score):
|
| 86 |
if score == 5:
|
| 87 |
+
st.success("π Perfect! You got 5/5! π")
|
| 88 |
elif score >= 3:
|
| 89 |
+
st.info(f"π Good Job! You scored {score}/5")
|
| 90 |
else:
|
| 91 |
+
st.warning(f"π Keep practicing. You scored {score}/5")
|
| 92 |
|
| 93 |
# ------------------------------
|
| 94 |
+
# UI Setup
|
| 95 |
# ------------------------------
|
| 96 |
st.set_page_config(page_title="BrainQuest AI", layout="centered")
|
| 97 |
|
| 98 |
with st.sidebar:
|
| 99 |
st.title("βΉοΈ About")
|
| 100 |
st.markdown("""
|
| 101 |
+
**π BrainQuest AI**
|
| 102 |
+
|
| 103 |
+
- Educational Quiz App
|
| 104 |
+
- Built with β€οΈ by **Sabir Ali**
|
| 105 |
+
- Responsive & Offline
|
| 106 |
+
- No login, No database
|
| 107 |
""")
|
| 108 |
|
| 109 |
+
st.title("π BrainQuest AI")
|
| 110 |
st.subheader("Test your knowledge across various fields")
|
| 111 |
st.markdown("---")
|
| 112 |
|
| 113 |
+
# Session Initialization
|
| 114 |
if "questions" not in st.session_state:
|
| 115 |
st.session_state.questions = []
|
| 116 |
+
st.session_state.submitted = False
|
| 117 |
|
| 118 |
+
# Category Selection
|
| 119 |
+
category = st.selectbox("π Choose a Category", list(quizzes.keys()))
|
| 120 |
|
| 121 |
if st.button("π Start Quiz"):
|
| 122 |
st.session_state.questions = load_questions(category)
|
| 123 |
+
st.session_state.submitted = False
|
| 124 |
+
for i in range(5):
|
| 125 |
+
st.session_state[f"answer_{i}"] = None # Reset answers
|
| 126 |
+
|
| 127 |
+
# Display Quiz Questions
|
| 128 |
+
if st.session_state.questions and not st.session_state.submitted:
|
| 129 |
+
for i, q in enumerate(st.session_state.questions):
|
| 130 |
+
st.markdown(f"**Q{i+1}:** {q['question']}")
|
| 131 |
+
st.radio(
|
| 132 |
+
"Your Answer:",
|
| 133 |
+
q["options"],
|
| 134 |
+
key=f"answer_{i}",
|
| 135 |
+
label_visibility="collapsed"
|
| 136 |
+
)
|
| 137 |
st.markdown("---")
|
| 138 |
|
| 139 |
if st.button("Submit Answers"):
|
| 140 |
+
st.session_state.submitted = True
|
| 141 |
+
score, results = evaluate(st.session_state.questions)
|
| 142 |
show_feedback(score)
|
| 143 |
|
| 144 |
+
st.markdown("## π Result Summary")
|
| 145 |
+
for i, r in enumerate(results):
|
| 146 |
+
emoji = "β
" if r["is_correct"] else "β"
|
| 147 |
+
st.markdown(f"**Q{i+1}:** {r['question']}")
|
| 148 |
+
st.markdown(f"- Your answer: `{r['selected']}` {emoji}")
|
| 149 |
+
if not r["is_correct"]:
|
| 150 |
+
st.markdown(f"- Correct answer: `{r['correct']}`")
|
| 151 |
st.markdown("---")
|
| 152 |
|
| 153 |
+
if st.button("π Try Another Quiz"):
|
| 154 |
+
st.session_state.questions = []
|
| 155 |
+
st.session_state.submitted = False
|
|
|