Rathapoom commited on
Commit
c4b97d9
·
verified ·
1 Parent(s): 3096664

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -40
app.py CHANGED
@@ -16,6 +16,10 @@ from datetime import datetime
16
  from typing import Dict, List, Optional, Tuple
17
  import random
18
 
 
 
 
 
19
  # Theme Configuration
20
  story_themes = {
21
  'fantasy': {
@@ -553,6 +557,8 @@ def init_session_state():
553
  st.session_state.theme_selection_id = datetime.now().strftime('%Y%m%d%H%M%S')
554
  if 'current_theme' not in st.session_state:
555
  st.session_state.current_theme = None
 
 
556
  if 'theme_story_starter' not in st.session_state:
557
  st.session_state.theme_story_starter = None
558
  if 'story' not in st.session_state:
@@ -1220,9 +1226,9 @@ def init_theme_state():
1220
 
1221
  # Theme Selection UI
1222
  def show_theme_selection():
1223
- # สร้าง session key ถ้ายังไม่มี
1224
- if 'theme_selection_id' not in st.session_state:
1225
- st.session_state.theme_selection_id = datetime.now().strftime('%Y%m%d%H%M%S')
1226
 
1227
  st.markdown("""
1228
  <div class="theme-header">
@@ -1230,46 +1236,45 @@ def show_theme_selection():
1230
  </div>
1231
  """, unsafe_allow_html=True)
1232
 
1233
- # สร้าง 4 columns
1234
  available_themes = get_available_themes(st.session_state.level)
1235
- cols = st.columns(4)
1236
 
1237
- # แสดงธีมในแต่ละ column
1238
- for col, theme in zip(cols, available_themes):
1239
- with col:
1240
- # ใช้ session ID และ theme ID สร้าง unique key
1241
- button_key = f"theme_{theme['id']}_{st.session_state.theme_selection_id}"
1242
-
1243
- if st.button(
1244
- f"{theme['icon']} {theme['name_th']}",
1245
- key=button_key,
1246
- use_container_width=True
1247
- ):
1248
- st.session_state.current_theme = theme['id']
1249
- starter = generate_dynamic_story_starter(theme['id'], st.session_state.level)
1250
- st.session_state.story = [{
1251
- "role": "AI",
1252
- "content": starter['en'],
1253
- "thai_content": starter['th'],
1254
- "is_starter": True
1255
- }]
1256
- st.rerun()
1257
-
1258
- # คำอธิบายใต้ปุ่ม
1259
- st.markdown(f"""
1260
- <div class="theme-card-description"
1261
- style="font-size: 0.85em;
1262
- color: #666;
1263
- padding: 5px;
1264
- background-color: {theme['background_color']};
1265
- border-radius: 0 0 8px 8px;
1266
- border: 1px solid {theme['accent_color']};
1267
- border-top: none;
1268
- margin-top: -5px;">
1269
- {theme['description_th']}
1270
- </div>
1271
- """, unsafe_allow_html=True)
1272
 
 
 
 
 
 
 
1273
 
1274
  # Add theme-specific CSS
1275
  def add_theme_css():
 
16
  from typing import Dict, List, Optional, Tuple
17
  import random
18
 
19
+ # Reset counter when page reloads
20
+ if 'theme_button_counter' in st.session_state:
21
+ st.session_state.theme_button_counter = 0
22
+
23
  # Theme Configuration
24
  story_themes = {
25
  'fantasy': {
 
557
  st.session_state.theme_selection_id = datetime.now().strftime('%Y%m%d%H%M%S')
558
  if 'current_theme' not in st.session_state:
559
  st.session_state.current_theme = None
560
+ if 'theme_button_counter' not in st.session_state:
561
+ st.session_state.theme_button_counter = 0
562
  if 'theme_story_starter' not in st.session_state:
563
  st.session_state.theme_story_starter = None
564
  if 'story' not in st.session_state:
 
1226
 
1227
  # Theme Selection UI
1228
  def show_theme_selection():
1229
+ # Initialize counter in session state if not exists
1230
+ if 'theme_button_counter' not in st.session_state:
1231
+ st.session_state.theme_button_counter = 0
1232
 
1233
  st.markdown("""
1234
  <div class="theme-header">
 
1236
  </div>
1237
  """, unsafe_allow_html=True)
1238
 
1239
+ # Create theme grid
1240
  available_themes = get_available_themes(st.session_state.level)
 
1241
 
1242
+ # Create rows with 4 themes each
1243
+ for i in range(0, len(available_themes), 4):
1244
+ row_themes = available_themes[i:i+4]
1245
+ cols = st.columns(4)
1246
+
1247
+ for col, theme in zip(cols, row_themes):
1248
+ with col:
1249
+ # Increment counter for each button
1250
+ st.session_state.theme_button_counter += 1
1251
+ current_count = st.session_state.theme_button_counter
1252
+
1253
+ # Create button with unique key using counter
1254
+ button_key = f"theme_button_{theme['id']}_{current_count}"
1255
+
1256
+ # Theme button
1257
+ if st.button(
1258
+ f"{theme['icon']} {theme['name_th']}",
1259
+ key=button_key,
1260
+ use_container_width=True,
1261
+ ):
1262
+ st.session_state.current_theme = theme['id']
1263
+ starter = generate_dynamic_story_starter(theme['id'], st.session_state.level)
1264
+ st.session_state.story = [{
1265
+ "role": "AI",
1266
+ "content": starter['en'],
1267
+ "thai_content": starter['th'],
1268
+ "is_starter": True
1269
+ }]
1270
+ st.rerun()
 
 
 
 
 
 
1271
 
1272
+ # Theme description
1273
+ st.markdown(f"""
1274
+ <div style="font-size: 0.85em; color: #666; padding: 5px;">
1275
+ {theme['description_th']}
1276
+ </div>
1277
+ """, unsafe_allow_html=True)
1278
 
1279
  # Add theme-specific CSS
1280
  def add_theme_css():