deepak1307 commited on
Commit
75d53b9
·
verified ·
1 Parent(s): ebb1f8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -117
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
- background-color: white;
31
- padding: 10px;
32
- margin: 5px;
33
- border-radius: 5px;
34
- border: 1px solid #d1d5db;
35
- cursor: pointer;
36
- text-align: center;
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
- .center-content {
56
- display: flex;
57
- justify-content: center;
 
 
 
 
 
 
 
 
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 'items_placed' not in st.session_state:
127
- st.session_state.items_placed = 0
128
 
129
  if 'game_complete' not in st.session_state:
130
  st.session_state.game_complete = False
131
 
132
- if 'current_items' not in st.session_state:
133
- st.session_state.current_items = []
134
 
135
- if 'item_positions' not in st.session_state:
136
- st.session_state.item_positions = {}
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.current_items = []
155
- st.session_state.item_positions = {}
156
  setup_game()
157
 
158
  # Function to change category set
159
  def change_category_set():
160
- st.session_state.score = 0
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=5):
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, 4)
187
 
188
- st.session_state.current_items = items
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, dropped_category):
201
  correct_category = st.session_state.item_categories.get(item)
202
- return correct_category == dropped_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>Drag each AI term to its correct category. Right answers stay, wrong answers return to their original position.</p></div>", unsafe_allow_html=True)
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.current_items:
231
  setup_game()
232
 
233
- # Game area
234
- categories = get_current_categories()
235
- category_cols = st.columns(len(categories))
 
 
 
236
 
237
- # Display category boxes
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 items to be sorted
246
- st.markdown("<div style='height: 50px;'></div>", unsafe_allow_html=True)
247
- st.markdown("<div class='center-content'><div style='width: 80%; padding: 20px; background-color: #e5e7eb; border-radius: 10px;'>", unsafe_allow_html=True)
 
248
 
249
- # Create unique item IDs
250
- item_ids = {item: f"item-{i}" for i, item in enumerate(st.session_state.current_items)}
 
251
 
252
- for item in st.session_state.current_items:
253
- if item not in st.session_state.correct_placements:
254
- item_id = item_ids[item]
255
- st.markdown(f"<div class='item' id='{item_id}'>{item}</div>", unsafe_allow_html=True)
256
 
257
- # Make the item draggable
258
- pos = st_draggable(item_id)
 
 
 
 
 
 
259
 
260
- # Handle item positioning and check for correct category
261
- if pos:
262
- st.session_state.item_positions[item] = pos
263
-
264
- # Check if the item is dropped into a category box
265
- for category in categories.keys():
266
- category_id = category.replace(' ', '-').lower()
267
- # Logic to detect if item is dropped in a category box
268
- # This is a simplified approximation - in a real app you'd use more robust detection
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
- st.markdown("</div></div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
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()