Spaces:
Running
Running
Update app.py (#3)
Browse files- Update app.py (b8991da6c4095829d5ca9cf17e34f02ea1c56fb2)
app.py
CHANGED
|
@@ -2,16 +2,13 @@ import os
|
|
| 2 |
import pandas as pd
|
| 3 |
import streamlit as st
|
| 4 |
import random
|
| 5 |
-
import requests
|
| 6 |
import plotly.express as px
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Groq API Endpoint and Key (Replace with your actual endpoint and API Key)
|
| 10 |
-
GROQ_API_KEY = "sk_9VSobViSwaWOpFGSWP5lWGdyb3FYIneUIyae233sZflxIOo1TW7m"
|
| 11 |
-
GROQ_API_URL = "https://api.groq.com/v1/query" # Example URL (you'll need to update based on the API documentation)
|
| 12 |
|
| 13 |
# File to store leaderboard data
|
| 14 |
LEADERBOARD_FILE = "leaderboard"
|
|
|
|
| 15 |
|
| 16 |
# Define questions for multiple topics
|
| 17 |
questions_db = {
|
|
@@ -95,33 +92,84 @@ def display_winner_charts(leaderboard_df):
|
|
| 95 |
else:
|
| 96 |
st.write("No data available to generate charts.")
|
| 97 |
|
| 98 |
-
# Function to
|
| 99 |
-
def
|
| 100 |
-
#
|
| 101 |
topic = st.selectbox("Choose a Topic", list(questions_db.keys()))
|
| 102 |
num_questions = st.selectbox("Select the number of questions", [5, 10, 15, 20])
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Get player's name
|
| 114 |
username = st.text_input("Enter your name:")
|
| 115 |
-
|
| 116 |
if username:
|
| 117 |
# Store answers in session state
|
| 118 |
if 'answers' not in st.session_state:
|
| 119 |
st.session_state['answers'] = [""] * len(questions) # Initialize answers list
|
| 120 |
|
| 121 |
score = 0
|
| 122 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
for i, (question, correct_answer) in enumerate(questions):
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# Submit button after all answers are entered
|
| 127 |
submit_button = st.button("Submit Answers")
|
|
@@ -155,11 +203,14 @@ def start_game():
|
|
| 155 |
# Main function to handle Streamlit app
|
| 156 |
def main():
|
| 157 |
st.title('Kahoot-like Game')
|
| 158 |
-
|
| 159 |
-
mode = st.sidebar.selectbox("Select Mode", ["Game", "Dashboard"])
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
elif mode == "Dashboard":
|
| 165 |
leaderboard_df = load_leaderboard()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import streamlit as st
|
| 4 |
import random
|
|
|
|
| 5 |
import plotly.express as px
|
| 6 |
+
import uuid
|
| 7 |
+
import json
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# File to store leaderboard data
|
| 10 |
LEADERBOARD_FILE = "leaderboard"
|
| 11 |
+
GAME_DATA_FILE = "game_data.json" # Store game data
|
| 12 |
|
| 13 |
# Define questions for multiple topics
|
| 14 |
questions_db = {
|
|
|
|
| 92 |
else:
|
| 93 |
st.write("No data available to generate charts.")
|
| 94 |
|
| 95 |
+
# Function to create a game with a unique game ID
|
| 96 |
+
def create_game():
|
| 97 |
+
game_id = str(uuid.uuid4())[:8] # Generate a short random ID
|
| 98 |
topic = st.selectbox("Choose a Topic", list(questions_db.keys()))
|
| 99 |
num_questions = st.selectbox("Select the number of questions", [5, 10, 15, 20])
|
| 100 |
|
| 101 |
+
questions = random.sample(questions_db[topic], num_questions)
|
| 102 |
+
|
| 103 |
+
# Store the game data with the unique game ID
|
| 104 |
+
game_data = {
|
| 105 |
+
'game_id': game_id,
|
| 106 |
+
'topic': topic,
|
| 107 |
+
'questions': questions
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
# Save the game data to the file or database (optional: could use a JSON or DB)
|
| 111 |
+
if os.path.exists(GAME_DATA_FILE):
|
| 112 |
+
with open(GAME_DATA_FILE, "r") as file:
|
| 113 |
+
all_games = json.load(file)
|
| 114 |
+
else:
|
| 115 |
+
all_games = {}
|
| 116 |
|
| 117 |
+
all_games[game_id] = game_data
|
| 118 |
+
|
| 119 |
+
with open(GAME_DATA_FILE, "w") as file:
|
| 120 |
+
json.dump(all_games, file)
|
| 121 |
+
|
| 122 |
+
st.success(f"Game created successfully! Game ID: {game_id}")
|
| 123 |
+
return game_id, topic, questions
|
| 124 |
+
|
| 125 |
+
# Function to join a game using game ID
|
| 126 |
+
def join_game():
|
| 127 |
+
game_id = st.text_input("Enter Game ID to join:")
|
| 128 |
|
| 129 |
+
if game_id:
|
| 130 |
+
with open(GAME_DATA_FILE, "r") as file:
|
| 131 |
+
all_games = json.load(file)
|
| 132 |
+
|
| 133 |
+
if game_id in all_games:
|
| 134 |
+
game_data = all_games[game_id]
|
| 135 |
+
st.session_state['game_data'] = game_data
|
| 136 |
+
st.success(f"Joined game with ID: {game_id}")
|
| 137 |
+
start_game(game_data) # Start the game with the joined data
|
| 138 |
+
else:
|
| 139 |
+
st.error("Invalid Game ID. Please check and try again.")
|
| 140 |
+
|
| 141 |
+
# Function to handle the game logic (dynamic number of questions based on user choice)
|
| 142 |
+
def start_game(game_data):
|
| 143 |
+
topic = game_data['topic']
|
| 144 |
+
questions = game_data['questions']
|
| 145 |
+
|
| 146 |
# Get player's name
|
| 147 |
username = st.text_input("Enter your name:")
|
| 148 |
+
|
| 149 |
if username:
|
| 150 |
# Store answers in session state
|
| 151 |
if 'answers' not in st.session_state:
|
| 152 |
st.session_state['answers'] = [""] * len(questions) # Initialize answers list
|
| 153 |
|
| 154 |
score = 0
|
| 155 |
+
# Ensure options are generated and stored in session state for each question
|
| 156 |
+
if 'options' not in st.session_state:
|
| 157 |
+
st.session_state['options'] = {}
|
| 158 |
+
|
| 159 |
+
# Show radio buttons for each question
|
| 160 |
for i, (question, correct_answer) in enumerate(questions):
|
| 161 |
+
# If options are not yet generated for this question, do so
|
| 162 |
+
if i not in st.session_state['options']:
|
| 163 |
+
# Create a list of possible options (correct answer + 3 random incorrect answers)
|
| 164 |
+
incorrect_answers = random.sample([q[1] for q in questions_db[topic] if q[1] != correct_answer], 3)
|
| 165 |
+
options = [correct_answer] + incorrect_answers
|
| 166 |
+
random.shuffle(options) # Shuffle to mix correct and incorrect answers
|
| 167 |
+
st.session_state['options'][i] = options # Store options in session state
|
| 168 |
+
|
| 169 |
+
# Display the question and options as radio buttons
|
| 170 |
+
options = st.session_state['options'][i] # Get options from session state
|
| 171 |
+
answer = st.radio(f"Question {i+1}: {question}", options, key=f"q{i}")
|
| 172 |
+
st.session_state['answers'][i] = answer
|
| 173 |
|
| 174 |
# Submit button after all answers are entered
|
| 175 |
submit_button = st.button("Submit Answers")
|
|
|
|
| 203 |
# Main function to handle Streamlit app
|
| 204 |
def main():
|
| 205 |
st.title('Kahoot-like Game')
|
|
|
|
|
|
|
| 206 |
|
| 207 |
+
mode = st.sidebar.selectbox("Select Mode", ["Create Game", "Join Game", "Dashboard"])
|
| 208 |
+
|
| 209 |
+
if mode == "Create Game":
|
| 210 |
+
create_game()
|
| 211 |
+
|
| 212 |
+
elif mode == "Join Game":
|
| 213 |
+
join_game()
|
| 214 |
|
| 215 |
elif mode == "Dashboard":
|
| 216 |
leaderboard_df = load_leaderboard()
|