Sabir55 commited on
Commit
f95c35e
Β·
verified Β·
1 Parent(s): 78ab8dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -90
app.py CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
2
  import random
3
 
4
  # ------------------------------
5
- # Quiz Data (at least 5 per category)
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
- # App Functions
114
  # ------------------------------
115
  def load_questions(category):
116
  return random.sample(quizzes[category], 5)
117
 
118
- def evaluate(questions, user_answers):
119
  score = 0
120
  results = []
121
- for q, ans in zip(questions, user_answers):
 
122
  correct = q["answer"]
123
- is_correct = ans == correct
124
  if is_correct:
125
  score += 1
126
- results.append((q["question"], correct, ans, is_correct))
 
 
 
 
 
127
  return score, results
128
 
129
  def show_feedback(score):
130
  if score == 5:
131
- st.success("🌟 Excellent! 5/5 πŸŽ‰")
132
  elif score >= 3:
133
- st.info(f"πŸ‘ Good job! {score}/5")
134
  else:
135
- st.warning(f"πŸ“š Keep practicing! {score}/5")
136
 
137
  # ------------------------------
138
- # Streamlit UI
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** is a smart quiz platform to test your knowledge across multiple fields.
146
-
147
- - Built with ❀️ by **Sabir Ali**
148
- - No login needed. No database.
149
- - 100% Free and Educational
 
150
  """)
151
 
152
- st.markdown("# πŸŽ“ BrainQuest AI")
153
  st.subheader("Test your knowledge across various fields")
154
  st.markdown("---")
155
 
156
- # Quiz State
157
  if "questions" not in st.session_state:
158
  st.session_state.questions = []
159
- st.session_state.answers = []
160
 
161
- # Select Category
162
- category = st.selectbox("πŸ“‚ Choose a Quiz Category", list(quizzes.keys()))
163
 
164
  if st.button("πŸš€ Start Quiz"):
165
  st.session_state.questions = load_questions(category)
166
- st.session_state.answers = []
167
-
168
- if st.session_state.questions:
169
- for idx, q in enumerate(st.session_state.questions):
170
- st.markdown(f"**Question {idx+1} of 5:** {q['question']}")
171
- answer = st.radio(f"Select answer:", q["options"], key=f"q_{idx}")
172
- st.session_state.answers.append(answer)
 
 
 
 
 
 
 
173
  st.markdown("---")
174
 
175
  if st.button("Submit Answers"):
176
- score, results = evaluate(st.session_state.questions, st.session_state.answers)
 
177
  show_feedback(score)
178
 
179
- st.markdown("## πŸ“Š Result Summary")
180
- for i, (question, correct, user, correct_bool) in enumerate(results):
181
- emoji = "βœ…" if correct_bool else "❌"
182
- st.markdown(f"**Q{i+1}:** {question}")
183
- st.markdown(f"- Your answer: {user} {emoji}")
184
- if not correct_bool:
185
- st.markdown(f"- Correct answer: {correct}")
186
  st.markdown("---")
187
 
188
- # Reset
189
- st.session_state.questions = []
190
- st.session_state.answers = []
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