Rathapoom commited on
Commit
7091cd9
·
verified ·
1 Parent(s): 095901f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -15
app.py CHANGED
@@ -1279,31 +1279,101 @@ def show_level_selection():
1279
 
1280
  def show_theme_selection():
1281
  """Display theme selection interface"""
1282
- st.markdown("""
1283
- <div class="theme-header text-center">
1284
- <h2 style="color: #1e88e5; font-family: 'Sarabun', sans-serif;">
1285
- 🎨 เลือกธีมเรื่องราว | Choose Story Theme
1286
- </h2>
1287
- <p style="color: #666; max-width: 600px; margin: 0 auto;">
 
 
 
 
1288
  เลือกโลกแห่งจินตนาการที่คุณต้องการผจญภัย และเริ่มต้นเขียนเรื่องราวของคุณ
1289
  </p>
1290
  </div>
1291
- """, unsafe_allow_html=True)
 
1292
 
1293
- # Filter themes for current level
1294
  available_themes = [
1295
  theme for theme in story_themes.values()
1296
  if st.session_state.level in theme['level_range']
1297
  ]
1298
 
1299
- # Create rows of 4 themes each
1300
- for i in range(0, len(available_themes), 4):
 
 
 
 
1301
  cols = st.columns(4)
1302
- row_themes = available_themes[i:i+4]
1303
-
1304
- for col, theme in zip(cols, row_themes):
1305
- with col:
1306
- show_theme_card(theme) # เปลี่ยนจาก display_theme_card เป็น show_theme_card
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1307
 
1308
  def show_theme_card(theme: Dict): # เปลี่ยนจาก display_theme_card เป็น show_theme_card
1309
  """Display a single theme card with proper styling"""
 
1279
 
1280
  def show_theme_selection():
1281
  """Display theme selection interface"""
1282
+ # Header section
1283
+ header_html = '''
1284
+ <div style="text-align: center; margin-bottom: 2rem;">
1285
+ <div style="display: flex; justify-content: center; align-items: center; gap: 10px;">
1286
+ <img src="🎨" alt="palette" style="width: 2rem; height: 2rem;"/>
1287
+ <h2 style="color: #1e88e5; font-family: 'Sarabun', sans-serif; margin: 0;">
1288
+ เลือกธีมเรื่องราว | Choose Story Theme
1289
+ </h2>
1290
+ </div>
1291
+ <p style="color: #666; max-width: 600px; margin: 1rem auto 0;">
1292
  เลือกโลกแห่งจินตนาการที่คุณต้องการผจญภัย และเริ่มต้นเขียนเรื่องราวของคุณ
1293
  </p>
1294
  </div>
1295
+ '''
1296
+ st.markdown(header_html, unsafe_allow_html=True)
1297
 
1298
+ # Filter available themes
1299
  available_themes = [
1300
  theme for theme in story_themes.values()
1301
  if st.session_state.level in theme['level_range']
1302
  ]
1303
 
1304
+ # Create grid layout
1305
+ num_themes = len(available_themes)
1306
+ rows = (num_themes + 3) // 4 # Ceiling division to get number of rows needed
1307
+
1308
+ # Create rows with 4 themes each
1309
+ for row in range(rows):
1310
  cols = st.columns(4)
1311
+ for col_idx, col in enumerate(cols):
1312
+ theme_idx = row * 4 + col_idx
1313
+ if theme_idx < num_themes:
1314
+ theme = available_themes[theme_idx]
1315
+ with col:
1316
+ # Theme card container
1317
+ theme_card = f'''
1318
+ <div style="
1319
+ background-color: {theme['background_color']};
1320
+ border-radius: 15px;
1321
+ padding: 1.5rem;
1322
+ margin-bottom: 1rem;
1323
+ height: 100%;
1324
+ position: relative;
1325
+ transition: all 0.3s ease;
1326
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
1327
+ ">
1328
+ <div style="position: absolute; top: 1rem; right: 1rem;
1329
+ padding: 0.25rem 0.75rem; border-radius: 999px;
1330
+ font-size: 0.8rem; background: #E3F2FD;
1331
+ color: #1E88E5;">
1332
+ {st.session_state.level}
1333
+ </div>
1334
+ <div style="font-size: 2.5rem; margin-bottom: 1rem;">
1335
+ {theme['icon']}
1336
+ </div>
1337
+ <div style="font-size: 1.2rem; font-weight: 600;
1338
+ margin-bottom: 0.5rem; color: {theme['accent_color']};">
1339
+ {theme['name_th']}
1340
+ </div>
1341
+ <div style="font-size: 0.9rem; color: #666;
1342
+ line-height: 1.4; margin-bottom: 1rem;">
1343
+ {theme['description_th']}
1344
+ </div>
1345
+ </div>
1346
+ '''
1347
+ st.markdown(theme_card, unsafe_allow_html=True)
1348
+
1349
+ # Theme selection button
1350
+ if st.button(
1351
+ f"เลือกธีม {theme['name_th']}",
1352
+ key=f"theme_{theme['id']}_{row}_{col_idx}",
1353
+ use_container_width=True
1354
+ ):
1355
+ handle_theme_selection(theme)
1356
+
1357
+ def handle_theme_selection(theme: dict):
1358
+ """Handle theme selection and initialization"""
1359
+ try:
1360
+ with st.spinner("กำลังเตรียมเรื่องราว..."):
1361
+ st.session_state.current_theme = theme['id']
1362
+ starter = generate_dynamic_story_starter(
1363
+ theme['id'],
1364
+ st.session_state.level
1365
+ )
1366
+ st.session_state.story = [{
1367
+ "role": "AI",
1368
+ "content": starter['en'],
1369
+ "thai_content": starter['th'],
1370
+ "is_starter": True
1371
+ }]
1372
+ st.success(f"เลือกธีม {theme['name_th']} เรียบร้อยแล้ว!")
1373
+ st.rerun()
1374
+ except Exception as e:
1375
+ logging.error(f"Error selecting theme: {str(e)}")
1376
+ st.error("เกิดข้อผิดพลาดในการเลือกธีม กรุณาลองใหม่อีกครั้ง")
1377
 
1378
  def show_theme_card(theme: Dict): # เปลี่ยนจาก display_theme_card เป็น show_theme_card
1379
  """Display a single theme card with proper styling"""