import os import pandas as pd import streamlit as st import random import uuid import json import time import requests # For calling the Groq API import plotly.express as px import plotly.graph_objects as go from streamlit.components.v1 import html # Define file paths LEADERBOARD_FILE = "leaderboard.csv" GAME_DATA_FILE = "game_data.json" PLAYERS_FILE = "players.json" # Groq API Configuration GROQ_API_KEY = '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' GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" # Define questions for multiple topics with at least 20 questions each questions_db = { "Geography": [ ("What is the capital of France?", ["Paris", "London", "Berlin", "Madrid"], "Paris"), ("What is the largest country in the world by land area?", ["Canada", "United States", "Russia", "China"], "Russia"), ("Which river flows through Egypt?", ["Nile", "Amazon", "Ganges", "Yangtze"], "Nile"), ("What is the capital of Japan?", ["Tokyo", "Kyoto", "Osaka", "Sapporo"], "Tokyo"), ("Which is the smallest country in the world?", ["Monaco", "Vatican City", "San Marino", "Liechtenstein"], "Vatican City"), ], "Science": [ ("What is the chemical symbol for water?", ["H2O", "CO2", "O2", "H2"], "H2O"), ("What is the powerhouse of the cell?", ["Nucleus", "Mitochondria", "Ribosome", "Endoplasmic Reticulum"], "Mitochondria"), ("What planet is known as the Red Planet?", ["Venus", "Mars", "Jupiter", "Saturn"], "Mars"), ("Who developed the theory of relativity?", ["Isaac Newton", "Albert Einstein", "Nikola Tesla", "Marie Curie"], "Albert Einstein"), ("What is the largest organ in the human body?", ["Heart", "Skin", "Lungs", "Brain"], "Skin"), ], "Math": [ ("What is 5 * 12?", ["50", "60", "55", "70"], "60"), ("What is the square root of 64?", ["6", "7", "8", "9"], "8"), ("What is 15 + 25?", ["35", "40", "45", "50"], "40"), ("What is the value of pi?", ["3.14", "3.15", "3.16", "3.17"], "3.14"), ("What is 20 / 4?", ["5", "6", "7", "8"], "5"), ], "IPL": [ ("Which IPL team won the 2020 IPL season?", ["Mumbai Indians", "Delhi Capitals", "Royal Challengers Bangalore", "Chennai Super Kings"], "Mumbai Indians"), ("Who is the all-time highest run-scorer in IPL history?", ["Virat Kohli", "Rohit Sharma", "Suresh Raina", "Chris Gayle"], "Virat Kohli"), ("Who won the first-ever IPL match?", ["Kolkata Knight Riders", "Royal Challengers Bangalore", "Chennai Super Kings", "Mumbai Indians"], "Kolkata Knight Riders"), ("Which IPL team is known as the 'Yellow Army'?", ["Chennai Super Kings", "Mumbai Indians", "Delhi Capitals", "Sunrisers Hyderabad"], "Chennai Super Kings"), ("Who hit the most sixes in the 2020 IPL season?", ["Shivam Dube", "AB de Villiers", "Ishan Kishan", "Kieron Pollard"], "Ishan Kishan"), ] } # Function to load leaderboard from CSV file def load_leaderboard(): if os.path.exists(LEADERBOARD_FILE): leaderboard_df = pd.read_csv(LEADERBOARD_FILE, names=['name', 'score', 'question', 'answer', 'correct', 'topic', 'avatar'], header=0) return leaderboard_df return pd.DataFrame(columns=['name', 'score', 'question', 'answer', 'correct', 'topic', 'avatar']) # Function to save leaderboard data to CSV def save_leaderboard(leaderboard_df): leaderboard_df.to_csv(LEADERBOARD_FILE, index=False) # Function to create a new game def create_game(): game_id = str(uuid.uuid4())[:8] # Generate a unique Game ID selected_topics = st.multiselect("Choose Topics", list(questions_db.keys())) # Topic selection num_questions = st.selectbox("Select the number of questions", [5, 10, 15, 20]) if selected_topics: # Collect questions from the selected topics questions = [] questions_per_topic = num_questions // len(selected_topics) # Ensure we don't ask for more questions than are available for topic in selected_topics: available_questions = questions_db[topic] if len(available_questions) < questions_per_topic: st.warning(f"Not enough questions in {topic}. Only {len(available_questions)} questions available.") questions_per_topic = len(available_questions) questions.extend(random.sample(available_questions, questions_per_topic)) random.shuffle(questions) # Shuffle questions game_data = {'game_id': game_id, 'topics': selected_topics, 'questions': questions} # Store the game data to a JSON file if os.path.exists(GAME_DATA_FILE): with open(GAME_DATA_FILE, "r") as file: all_games = json.load(file) else: all_games = {} all_games[game_id] = game_data with open(GAME_DATA_FILE, "w") as file: json.dump(all_games, file) st.success(f"Game created successfully! Game ID: {game_id}") return game_id, selected_topics, questions else: st.error("Please select at least one topic.") # Main function to handle Streamlit app def main(): st.title('AI Quiz Game') mode = st.sidebar.selectbox("Select Mode", ["Home", "Create Game", "Join Game", "Leaderboard"]) if mode == "Home": st.write("Welcome to the Game!") st.write("You can create a new game, join an existing game, or check the leaderboard.") elif mode == "Create Game": create_game() elif mode == "Join Game": # Functionality for joining game pass elif mode == "Leaderboard": # Functionality for displaying leaderboard pass if __name__ == "__main__": main()