Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import random
|
| 4 |
-
from streamlit_draggable import st_draggable
|
| 5 |
-
import json
|
| 6 |
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="AI Category Sorting Game",
|
|
@@ -19,6 +17,7 @@ st.markdown("""
|
|
| 19 |
padding: 15px;
|
| 20 |
min-height: 300px;
|
| 21 |
border: 2px dashed #9ca3af;
|
|
|
|
| 22 |
}
|
| 23 |
.category-title {
|
| 24 |
text-align: center;
|
|
@@ -26,22 +25,15 @@ st.markdown("""
|
|
| 26 |
font-size: 1.2rem;
|
| 27 |
margin-bottom: 15px;
|
| 28 |
}
|
| 29 |
-
.item {
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
.correct {
|
| 39 |
-
background-color: #d1fae5;
|
| 40 |
-
border: 1px solid #10b981;
|
| 41 |
-
}
|
| 42 |
-
.incorrect {
|
| 43 |
-
background-color: #fee2e2;
|
| 44 |
-
border: 1px solid #ef4444;
|
| 45 |
}
|
| 46 |
.title-area {
|
| 47 |
text-align: center;
|
|
@@ -52,9 +44,17 @@ st.markdown("""
|
|
| 52 |
text-align: center;
|
| 53 |
margin-top: 20px;
|
| 54 |
}
|
| 55 |
-
.
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
</style>
|
| 60 |
""", unsafe_allow_html=True)
|
|
@@ -121,19 +121,19 @@ def init_session():
|
|
| 121 |
st.session_state.total_items = 0
|
| 122 |
|
| 123 |
if 'correct_placements' not in st.session_state:
|
| 124 |
-
st.session_state.correct_placements =
|
| 125 |
|
| 126 |
-
if '
|
| 127 |
-
st.session_state.
|
| 128 |
|
| 129 |
if 'game_complete' not in st.session_state:
|
| 130 |
st.session_state.game_complete = False
|
| 131 |
|
| 132 |
-
if '
|
| 133 |
-
st.session_state.
|
| 134 |
|
| 135 |
-
if '
|
| 136 |
-
st.session_state.
|
| 137 |
|
| 138 |
# Function to get current category data
|
| 139 |
def get_current_categories():
|
|
@@ -148,26 +148,18 @@ def get_current_categories():
|
|
| 148 |
def reset_game():
|
| 149 |
st.session_state.score = 0
|
| 150 |
st.session_state.total_items = 0
|
| 151 |
-
st.session_state.correct_placements =
|
| 152 |
-
st.session_state.items_placed = 0
|
| 153 |
st.session_state.game_complete = False
|
| 154 |
-
st.session_state.
|
| 155 |
-
st.session_state.
|
| 156 |
setup_game()
|
| 157 |
|
| 158 |
# Function to change category set
|
| 159 |
def change_category_set():
|
| 160 |
-
|
| 161 |
-
st.session_state.total_items = 0
|
| 162 |
-
st.session_state.correct_placements = {}
|
| 163 |
-
st.session_state.items_placed = 0
|
| 164 |
-
st.session_state.game_complete = False
|
| 165 |
-
st.session_state.current_items = []
|
| 166 |
-
st.session_state.item_positions = {}
|
| 167 |
-
setup_game()
|
| 168 |
|
| 169 |
# Function to get random items for the game
|
| 170 |
-
def get_random_items(categories, num_per_category=
|
| 171 |
all_items = []
|
| 172 |
item_categories = {}
|
| 173 |
|
|
@@ -183,29 +175,38 @@ def get_random_items(categories, num_per_category=5):
|
|
| 183 |
# Function to setup the game
|
| 184 |
def setup_game():
|
| 185 |
categories = get_current_categories()
|
| 186 |
-
items, item_categories = get_random_items(categories,
|
| 187 |
|
| 188 |
-
st.session_state.
|
| 189 |
st.session_state.item_categories = item_categories
|
| 190 |
st.session_state.total_items = len(items)
|
| 191 |
-
|
| 192 |
-
# Initialize positions for items
|
| 193 |
-
positions = {}
|
| 194 |
-
for idx, item in enumerate(items):
|
| 195 |
-
positions[item] = {"top": 0, "left": idx * 120}
|
| 196 |
-
|
| 197 |
-
st.session_state.item_positions = positions
|
| 198 |
|
| 199 |
# Function to check if an item is correctly placed
|
| 200 |
-
def check_placement(item,
|
| 201 |
correct_category = st.session_state.item_categories.get(item)
|
| 202 |
-
return correct_category ==
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
# Initialize session
|
| 205 |
init_session()
|
| 206 |
|
| 207 |
# Title and description
|
| 208 |
-
st.markdown("<div class='title-area'><h1>🎮 AI Category Sorting Game</h1><p>
|
| 209 |
|
| 210 |
# Game mode selection
|
| 211 |
col1, col2, col3 = st.columns([1, 2, 1])
|
|
@@ -227,55 +228,61 @@ with col2:
|
|
| 227 |
st.session_state.category_set = selected_category
|
| 228 |
|
| 229 |
# Setup game if not already setup
|
| 230 |
-
if not st.session_state.
|
| 231 |
setup_game()
|
| 232 |
|
| 233 |
-
#
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
for i, (category, items) in enumerate(categories.items()):
|
| 239 |
-
with category_cols[i]:
|
| 240 |
-
st.markdown(f"<div class='category-title'>{category}</div>", unsafe_allow_html=True)
|
| 241 |
-
category_placeholder = st.empty()
|
| 242 |
-
with category_placeholder.container():
|
| 243 |
-
st.markdown(f"<div class='category-box' id='{category.replace(' ', '-').lower()}'></div>", unsafe_allow_html=True)
|
| 244 |
|
| 245 |
-
# Display
|
| 246 |
-
st.
|
| 247 |
-
|
|
|
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
|
|
|
| 251 |
|
| 252 |
-
for item in
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
st.
|
| 256 |
|
| 257 |
-
#
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
if pos['top'] > 100 and pos['top'] < 400:
|
| 270 |
-
dropped_index = int(pos['left'] / (st.session_state.component_width / len(categories)))
|
| 271 |
-
if dropped_index < len(categories) and list(categories.keys())[dropped_index] == category:
|
| 272 |
-
is_correct = check_placement(item, category)
|
| 273 |
-
if is_correct:
|
| 274 |
-
st.session_state.correct_placements[item] = category
|
| 275 |
-
st.session_state.score += 1
|
| 276 |
-
# The UI would update on the next rerun
|
| 277 |
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
# Display score
|
| 281 |
st.markdown(f"""
|
|
@@ -289,27 +296,4 @@ col1, col2, col3 = st.columns([1, 1, 1])
|
|
| 289 |
with col2:
|
| 290 |
if st.button("Reset Game"):
|
| 291 |
reset_game()
|
| 292 |
-
|
| 293 |
-
# Check if game is complete
|
| 294 |
-
if st.session_state.score == st.session_state.total_items and st.session_state.total_items > 0:
|
| 295 |
-
if not st.session_state.game_complete:
|
| 296 |
-
st.session_state.game_complete = True
|
| 297 |
-
st.balloons()
|
| 298 |
-
|
| 299 |
-
st.success("🎉 Congratulations! You've correctly categorized all items!")
|
| 300 |
-
|
| 301 |
-
# Add JavaScript for drag and drop functionality
|
| 302 |
-
st.markdown("""
|
| 303 |
-
<script>
|
| 304 |
-
// This would be the client-side JavaScript to handle drag and drop
|
| 305 |
-
// In a real Streamlit app, this would be integrated with Streamlit components
|
| 306 |
-
// This is just a placeholder to illustrate the concept
|
| 307 |
-
</script>
|
| 308 |
-
""", unsafe_allow_html=True)
|
| 309 |
-
|
| 310 |
-
# Disclaimer about the app
|
| 311 |
-
st.markdown("""
|
| 312 |
-
---
|
| 313 |
-
**Note:** This app uses `streamlit_draggable`, a custom component. In a full implementation,
|
| 314 |
-
you would need to install this package or create a custom drag-and-drop component.
|
| 315 |
-
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import random
|
|
|
|
|
|
|
| 4 |
|
| 5 |
st.set_page_config(
|
| 6 |
page_title="AI Category Sorting Game",
|
|
|
|
| 17 |
padding: 15px;
|
| 18 |
min-height: 300px;
|
| 19 |
border: 2px dashed #9ca3af;
|
| 20 |
+
margin-bottom: 20px;
|
| 21 |
}
|
| 22 |
.category-title {
|
| 23 |
text-align: center;
|
|
|
|
| 25 |
font-size: 1.2rem;
|
| 26 |
margin-bottom: 15px;
|
| 27 |
}
|
| 28 |
+
.item-container {
|
| 29 |
+
display: flex;
|
| 30 |
+
flex-wrap: wrap;
|
| 31 |
+
justify-content: center;
|
| 32 |
+
gap: 10px;
|
| 33 |
+
margin-top: 20px;
|
| 34 |
+
padding: 15px;
|
| 35 |
+
background-color: #e5e7eb;
|
| 36 |
+
border-radius: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
.title-area {
|
| 39 |
text-align: center;
|
|
|
|
| 44 |
text-align: center;
|
| 45 |
margin-top: 20px;
|
| 46 |
}
|
| 47 |
+
.feedback-text {
|
| 48 |
+
text-align: center;
|
| 49 |
+
font-weight: bold;
|
| 50 |
+
margin: 15px 0;
|
| 51 |
+
height: 24px;
|
| 52 |
+
}
|
| 53 |
+
.correct {
|
| 54 |
+
color: #10b981;
|
| 55 |
+
}
|
| 56 |
+
.incorrect {
|
| 57 |
+
color: #ef4444;
|
| 58 |
}
|
| 59 |
</style>
|
| 60 |
""", unsafe_allow_html=True)
|
|
|
|
| 121 |
st.session_state.total_items = 0
|
| 122 |
|
| 123 |
if 'correct_placements' not in st.session_state:
|
| 124 |
+
st.session_state.correct_placements = set()
|
| 125 |
|
| 126 |
+
if 'available_items' not in st.session_state:
|
| 127 |
+
st.session_state.available_items = []
|
| 128 |
|
| 129 |
if 'game_complete' not in st.session_state:
|
| 130 |
st.session_state.game_complete = False
|
| 131 |
|
| 132 |
+
if 'feedback' not in st.session_state:
|
| 133 |
+
st.session_state.feedback = ""
|
| 134 |
|
| 135 |
+
if 'feedback_type' not in st.session_state:
|
| 136 |
+
st.session_state.feedback_type = ""
|
| 137 |
|
| 138 |
# Function to get current category data
|
| 139 |
def get_current_categories():
|
|
|
|
| 148 |
def reset_game():
|
| 149 |
st.session_state.score = 0
|
| 150 |
st.session_state.total_items = 0
|
| 151 |
+
st.session_state.correct_placements = set()
|
|
|
|
| 152 |
st.session_state.game_complete = False
|
| 153 |
+
st.session_state.feedback = ""
|
| 154 |
+
st.session_state.feedback_type = ""
|
| 155 |
setup_game()
|
| 156 |
|
| 157 |
# Function to change category set
|
| 158 |
def change_category_set():
|
| 159 |
+
reset_game()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Function to get random items for the game
|
| 162 |
+
def get_random_items(categories, num_per_category=3):
|
| 163 |
all_items = []
|
| 164 |
item_categories = {}
|
| 165 |
|
|
|
|
| 175 |
# Function to setup the game
|
| 176 |
def setup_game():
|
| 177 |
categories = get_current_categories()
|
| 178 |
+
items, item_categories = get_random_items(categories, 3)
|
| 179 |
|
| 180 |
+
st.session_state.available_items = items
|
| 181 |
st.session_state.item_categories = item_categories
|
| 182 |
st.session_state.total_items = len(items)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
# Function to check if an item is correctly placed
|
| 185 |
+
def check_placement(item, selected_category):
|
| 186 |
correct_category = st.session_state.item_categories.get(item)
|
| 187 |
+
return correct_category == selected_category
|
| 188 |
+
|
| 189 |
+
# Function to handle item placement
|
| 190 |
+
def place_item(item, category):
|
| 191 |
+
if check_placement(item, category):
|
| 192 |
+
st.session_state.correct_placements.add(item)
|
| 193 |
+
st.session_state.score += 1
|
| 194 |
+
st.session_state.feedback = f"Correct! {item} belongs to {category}"
|
| 195 |
+
st.session_state.feedback_type = "correct"
|
| 196 |
+
|
| 197 |
+
# Check if game is complete
|
| 198 |
+
if st.session_state.score == st.session_state.total_items:
|
| 199 |
+
st.session_state.game_complete = True
|
| 200 |
+
else:
|
| 201 |
+
correct_category = st.session_state.item_categories.get(item)
|
| 202 |
+
st.session_state.feedback = f"Incorrect. {item} belongs to {correct_category}, not {category}"
|
| 203 |
+
st.session_state.feedback_type = "incorrect"
|
| 204 |
|
| 205 |
# Initialize session
|
| 206 |
init_session()
|
| 207 |
|
| 208 |
# Title and description
|
| 209 |
+
st.markdown("<div class='title-area'><h1>🎮 AI Category Sorting Game</h1><p>Select the correct category for each AI term to sort them.</p></div>", unsafe_allow_html=True)
|
| 210 |
|
| 211 |
# Game mode selection
|
| 212 |
col1, col2, col3 = st.columns([1, 2, 1])
|
|
|
|
| 228 |
st.session_state.category_set = selected_category
|
| 229 |
|
| 230 |
# Setup game if not already setup
|
| 231 |
+
if not hasattr(st.session_state, 'available_items') or not st.session_state.available_items:
|
| 232 |
setup_game()
|
| 233 |
|
| 234 |
+
# Display feedback
|
| 235 |
+
feedback_class = ""
|
| 236 |
+
if st.session_state.feedback_type == "correct":
|
| 237 |
+
feedback_class = "correct"
|
| 238 |
+
elif st.session_state.feedback_type == "incorrect":
|
| 239 |
+
feedback_class = "incorrect"
|
| 240 |
|
| 241 |
+
st.markdown(f"<div class='feedback-text {feedback_class}'>{st.session_state.feedback}</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
+
# Display available items
|
| 244 |
+
st.subheader("AI Terms to Sort")
|
| 245 |
+
item_cols = st.columns(4)
|
| 246 |
+
available_items = [item for item in st.session_state.available_items if item not in st.session_state.correct_placements]
|
| 247 |
|
| 248 |
+
if not available_items and st.session_state.available_items:
|
| 249 |
+
st.success("🎉 Congratulations! You've correctly categorized all items!")
|
| 250 |
+
st.balloons()
|
| 251 |
|
| 252 |
+
for i, item in enumerate(available_items):
|
| 253 |
+
col_idx = i % 4
|
| 254 |
+
with item_cols[col_idx]:
|
| 255 |
+
st.write(f"**{item}**")
|
| 256 |
|
| 257 |
+
# Create dropdown for categories
|
| 258 |
+
categories = get_current_categories()
|
| 259 |
+
options = list(categories.keys())
|
| 260 |
+
category_choice = st.selectbox(
|
| 261 |
+
f"Select category for {item}",
|
| 262 |
+
options=["Select category..."] + options,
|
| 263 |
+
key=f"category_select_{item}"
|
| 264 |
+
)
|
| 265 |
|
| 266 |
+
if st.button(f"Submit", key=f"submit_{item}"):
|
| 267 |
+
if category_choice != "Select category...":
|
| 268 |
+
place_item(item, category_choice)
|
| 269 |
+
st.experimental_rerun()
|
| 270 |
+
|
| 271 |
+
# Display categories and their items
|
| 272 |
+
st.subheader("Categories")
|
| 273 |
+
categories = get_current_categories()
|
| 274 |
+
category_cols = st.columns(len(categories))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
+
for i, (category, _) in enumerate(categories.items()):
|
| 277 |
+
with category_cols[i]:
|
| 278 |
+
st.markdown(f"<div class='category-title'>{category}</div>", unsafe_allow_html=True)
|
| 279 |
+
with st.container():
|
| 280 |
+
st.markdown("<div class='category-box'>", unsafe_allow_html=True)
|
| 281 |
+
# Display items correctly placed in this category
|
| 282 |
+
for item in st.session_state.correct_placements:
|
| 283 |
+
if st.session_state.item_categories.get(item) == category:
|
| 284 |
+
st.info(item)
|
| 285 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 286 |
|
| 287 |
# Display score
|
| 288 |
st.markdown(f"""
|
|
|
|
| 296 |
with col2:
|
| 297 |
if st.button("Reset Game"):
|
| 298 |
reset_game()
|
| 299 |
+
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|