Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import random | |
| # ------------------------------ | |
| # Quiz Data (10 categories Γ 5 questions each) | |
| # ------------------------------ | |
| quizzes = { | |
| "Science": [ | |
| {"question": "What planet is known as the Red Planet?", "options": ["Earth", "Mars", "Venus", "Jupiter"], "answer": "Mars"}, | |
| {"question": "What gas do plants absorb?", "options": ["Oxygen", "Carbon Dioxide", "Nitrogen", "Hydrogen"], "answer": "Carbon Dioxide"}, | |
| {"question": "Water freezes at what temperature (C)?", "options": ["0", "32", "-10", "100"], "answer": "0"}, | |
| {"question": "What organ produces insulin?", "options": ["Liver", "Heart", "Pancreas", "Lung"], "answer": "Pancreas"}, | |
| {"question": "What is the chemical symbol for gold?", "options": ["Au", "Ag", "Go", "Gd"], "answer": "Au"}, | |
| ], | |
| "Artificial Intelligence (AI)": [ | |
| {"question": "What does AI stand for?", "options": ["Artificial Intelligence", "Automatic Integration", "Automated Interface", "Actual Intelligence"], "answer": "Artificial Intelligence"}, | |
| {"question": "Which company developed ChatGPT?", "options": ["Google", "Meta", "OpenAI", "Microsoft"], "answer": "OpenAI"}, | |
| {"question": "Which model is used for generating human-like text?", "options": ["CNN", "GPT", "RNN", "SVM"], "answer": "GPT"}, | |
| {"question": "Turing Test is used to test?", "options": ["Intelligence", "Speed", "Memory", "Computation"], "answer": "Intelligence"}, | |
| {"question": "Which is not a type of AI?", "options": ["Narrow AI", "General AI", "Super AI", "Organic AI"], "answer": "Organic AI"}, | |
| ], | |
| "Machine Learning (ML)": [ | |
| {"question": "Which of the following is a supervised learning algorithm?", "options": ["K-Means", "Linear Regression", "PCA", "DBSCAN"], "answer": "Linear Regression"}, | |
| {"question": "ML uses what to make predictions?", "options": ["Data", "Rules", "Hardware", "Code"], "answer": "Data"}, | |
| {"question": "Which library is used in ML?", "options": ["NumPy", "Pandas", "Scikit-learn", "OpenCV"], "answer": "Scikit-learn"}, | |
| {"question": "Which is not an ML algorithm?", "options": ["SVM", "CNN", "Decision Tree", "KNN"], "answer": "CNN"}, | |
| {"question": "What type of data does supervised learning require?", "options": ["Unlabeled", "Categorical", "Labeled", "Binary"], "answer": "Labeled"}, | |
| ], | |
| "Deep Learning (DL)": [ | |
| {"question": "Deep Learning is a subset of?", "options": ["Machine Learning", "Data Mining", "Statistics", "Robotics"], "answer": "Machine Learning"}, | |
| {"question": "Which architecture is common in DL?", "options": ["CNN", "SVM", "KNN", "PCA"], "answer": "CNN"}, | |
| {"question": "Which library is used for DL?", "options": ["TensorFlow", "NumPy", "Matplotlib", "OpenCV"], "answer": "TensorFlow"}, | |
| {"question": "RNNs are best for what type of data?", "options": ["Images", "Time Series", "Tables", "Graphs"], "answer": "Time Series"}, | |
| {"question": "Which function is used in neural networks?", "options": ["Activation", "Loss", "Gradient", "Bias"], "answer": "Activation"}, | |
| ], | |
| "Computer Vision (CV)": [ | |
| {"question": "What is the goal of CV?", "options": ["Understand images", "Translate text", "Play games", "Store files"], "answer": "Understand images"}, | |
| {"question": "Which library is popular in CV?", "options": ["OpenCV", "Pandas", "Flask", "Numpy"], "answer": "OpenCV"}, | |
| {"question": "What technique is used for object detection?", "options": ["YOLO", "RNN", "PCA", "SVM"], "answer": "YOLO"}, | |
| {"question": "What file format is NOT an image?", "options": ["JPG", "PNG", "TXT", "GIF"], "answer": "TXT"}, | |
| {"question": "Which model type is used for image classification?", "options": ["CNN", "RNN", "LSTM", "DBSCAN"], "answer": "CNN"}, | |
| ], | |
| "Health": [ | |
| {"question": "Which vitamin is gained from sunlight?", "options": ["A", "B12", "C", "D"], "answer": "D"}, | |
| {"question": "What is the normal human body temperature?", "options": ["36.5Β°C", "37Β°C", "38Β°C", "39Β°C"], "answer": "37Β°C"}, | |
| {"question": "Which organ pumps blood?", "options": ["Lungs", "Liver", "Heart", "Kidney"], "answer": "Heart"}, | |
| {"question": "Which disease affects lungs?", "options": ["Asthma", "Diabetes", "Arthritis", "Jaundice"], "answer": "Asthma"}, | |
| {"question": "What nutrient builds muscle?", "options": ["Protein", "Carbohydrate", "Fat", "Sugar"], "answer": "Protein"}, | |
| ], | |
| "Environment": [ | |
| {"question": "Which gas causes global warming?", "options": ["CO2", "O2", "N2", "H2"], "answer": "CO2"}, | |
| {"question": "What is the main source of air pollution?", "options": ["Vehicles", "Trees", "Wind", "Mountains"], "answer": "Vehicles"}, | |
| {"question": "Which is a renewable resource?", "options": ["Solar", "Coal", "Oil", "Gas"], "answer": "Solar"}, | |
| {"question": "Which layer protects us from UV rays?", "options": ["Ozone", "Clouds", "Stratosphere", "Troposphere"], "answer": "Ozone"}, | |
| {"question": "Which ocean is the largest?", "options": ["Atlantic", "Pacific", "Indian", "Arctic"], "answer": "Pacific"}, | |
| ], | |
| "Mathematics": [ | |
| {"question": "What is 12 Γ 8?", "options": ["96", "108", "88", "104"], "answer": "96"}, | |
| {"question": "Square root of 64?", "options": ["6", "7", "8", "9"], "answer": "8"}, | |
| {"question": "Ο (Pi) value to 2 decimal places?", "options": ["3.12", "3.14", "3.16", "3.18"], "answer": "3.14"}, | |
| {"question": "What is 15% of 200?", "options": ["30", "25", "20", "35"], "answer": "30"}, | |
| {"question": "What is 100 Γ· 5?", "options": ["20", "15", "10", "25"], "answer": "20"}, | |
| ], | |
| "Literature": [ | |
| {"question": "Who wrote 'Hamlet'?", "options": ["Shakespeare", "Chaucer", "Shelley", "Austen"], "answer": "Shakespeare"}, | |
| {"question": "'Pride and Prejudice' author?", "options": ["Jane Austen", "Bronte", "Eliot", "Shelley"], "answer": "Jane Austen"}, | |
| {"question": "Sherlock Holmes creator?", "options": ["Doyle", "Christie", "Rowling", "Brown"], "answer": "Doyle"}, | |
| {"question": "Which book starts with 'Call me Ishmael'?", "options": ["Moby Dick", "Odyssey", "Ulysses", "Don Quixote"], "answer": "Moby Dick"}, | |
| {"question": "Language of 'Les MisΓ©rables'?", "options": ["French", "English", "German", "Spanish"], "answer": "French"}, | |
| ], | |
| "History": [ | |
| {"question": "First President of USA?", "options": ["Lincoln", "Washington", "Adams", "Jefferson"], "answer": "Washington"}, | |
| {"question": "Year WWII ended?", "options": ["1945", "1939", "1950", "1940"], "answer": "1945"}, | |
| {"question": "Pyramids built by?", "options": ["Egyptians", "Romans", "Greeks", "Mayans"], "answer": "Egyptians"}, | |
| {"question": "Who discovered America?", "options": ["Columbus", "Da Gama", "Magellan", "Marco Polo"], "answer": "Columbus"}, | |
| {"question": "Start of Industrial Revolution?", "options": ["England", "France", "USA", "Germany"], "answer": "England"}, | |
| ] | |
| } | |
| # ------------------------------ | |
| # Functions | |
| # ------------------------------ | |
| def load_questions(category): | |
| return random.sample(quizzes[category], 5) | |
| def evaluate(questions): | |
| score = 0 | |
| results = [] | |
| for i, q in enumerate(questions): | |
| selected = st.session_state.get(f"answer_{i}") | |
| correct = q["answer"] | |
| is_correct = selected == correct | |
| if is_correct: | |
| score += 1 | |
| results.append({"question": q["question"], "selected": selected, "correct": correct, "is_correct": is_correct}) | |
| return score, results | |
| def show_feedback(score): | |
| if score == 5: | |
| st.success("π Perfect! You got 5/5! π") | |
| elif score >= 3: | |
| st.info(f"π Good Job! You scored {score}/5") | |
| else: | |
| st.warning(f"π Keep practicing. You scored {score}/5") | |
| # ------------------------------ | |
| # Streamlit UI | |
| # ------------------------------ | |
| st.set_page_config(page_title="BrainQuest AI", layout="centered") | |
| # First page welcome screen | |
| if "started" not in st.session_state: | |
| st.session_state.started = False | |
| if not st.session_state.started: | |
| st.markdown(""" | |
| <div style='text-align:center;'> | |
| <h1 style='font-size:48px;'>π Welcome to BrainQuest AI</h1> | |
| <p style='font-size:24px;'>Test your knowledge across 10 powerful fields</p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| if st.button("π Get Started Now", key="start_button", help="Click to begin the quiz experience"): | |
| st.session_state.started = True | |
| st.stop() | |
| # Sidebar | |
| with st.sidebar: | |
| st.title("βΉοΈ About") | |
| st.markdown(""" | |
| **π BrainQuest AI** | |
| - 10 Dynamic Categories | |
| - Built with β€οΈ by **Sabir Ali** | |
| - Fully Offline, Fast & Engaging | |
| """) | |
| st.title("π§ Choose a Quiz Category") | |
| category = st.selectbox("π Select Category", list(quizzes.keys())) | |
| if st.button("Start Quiz"): | |
| st.session_state.questions = load_questions(category) | |
| st.session_state.submitted = False | |
| for i in range(5): | |
| st.session_state[f"answer_{i}"] = None | |
| if "questions" in st.session_state and not st.session_state.submitted: | |
| for i, q in enumerate(st.session_state.questions): | |
| st.markdown(f"**Q{i+1}:** {q['question']}") | |
| st.radio("Your Answer:", q["options"], key=f"answer_{i}", label_visibility="collapsed") | |
| st.markdown("---") | |
| if st.button("Submit Answers"): | |
| st.session_state.submitted = True | |
| score, results = evaluate(st.session_state.questions) | |
| show_feedback(score) | |
| st.markdown("## π Results Summary") | |
| for i, r in enumerate(results): | |
| emoji = "β " if r["is_correct"] else "β" | |
| st.markdown(f"**Q{i+1}:** {r['question']}") | |
| st.markdown(f"- Your answer: `{r['selected']}` {emoji}") | |
| if not r["is_correct"]: | |
| st.markdown(f"- Correct answer: `{r['correct']}`") | |
| st.markdown("---") | |
| if st.button("π Try Another Quiz"): | |
| st.session_state.questions = [] | |
| st.session_state.submitted = False | |