Rathapoom commited on
Commit
499d5c7
·
verified ·
1 Parent(s): 8919ab6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -115
app.py CHANGED
@@ -1209,72 +1209,34 @@ def show_theme_selection():
1209
  </div>
1210
  """, unsafe_allow_html=True)
1211
 
1212
- # สร้าง 2 rows, แต่ละ row มี 4 themes
1213
- themes_per_row = 4
1214
  available_themes = get_available_themes(st.session_state.level)
 
1215
 
1216
- # แบ่งธีมเป็น rows
1217
- for i in range(0, len(available_themes), themes_per_row):
1218
- row_themes = available_themes[i:i + themes_per_row]
1219
- cols = st.columns(themes_per_row)
1220
-
1221
- # แสดงแต่ละธีมใน column
1222
- for col, theme in zip(cols, row_themes):
1223
- with col:
1224
- # สร้าง unique key โดยใช้ timestamp และ theme id
1225
- unique_key = f"theme_{theme['id']}_{datetime.now().timestamp()}"
1226
-
1227
- # ใช้ container เพื่อจัดการ layout
1228
- with st.container():
1229
- # สร้างปุ่มพร้อมสไตล์
1230
- button_html = f"""
1231
- <div style="
1232
- background: linear-gradient(135deg, {theme['background_color']}, white);
1233
- border: 2px solid {theme['accent_color']};
1234
- border-radius: 12px;
1235
- padding: 15px;
1236
- margin: 5px;
1237
- text-align: center;
1238
- min-height: 150px;">
1239
- <div style="font-size: 2em; margin-bottom: 8px;">
1240
- {theme['icon']}
1241
- </div>
1242
- <h4 style="color: {theme['accent_color']};
1243
- margin: 5px 0;
1244
- font-family: 'Sarabun', sans-serif;">
1245
- {theme['name_th']}
1246
- </h4>
1247
- <p style="font-size: 0.85em;
1248
- color: #666;
1249
- margin-top: 5px;">
1250
- {theme['description_th']}
1251
- </p>
1252
- </div>
1253
- """
1254
-
1255
- # ใช้ clickable div แทน button
1256
- if st.markdown(f"""
1257
- <div style="cursor: pointer;" onclick="document.getElementById('{unique_key}').click()">
1258
- {button_html}
1259
- </div>
1260
- """, unsafe_allow_html=True):
1261
- st.session_state.current_theme = theme['id']
1262
- starter = generate_dynamic_story_starter(theme['id'], st.session_state.level)
1263
- st.session_state.story = [{
1264
- "role": "AI",
1265
- "content": starter['en'],
1266
- "thai_content": starter['th'],
1267
- "is_starter": True
1268
- }]
1269
- st.rerun()
1270
-
1271
- # ซ่อนปุ่มจริงไว้
1272
- st.button("Select", key=unique_key, help=theme['description_th'],
1273
- on_click=lambda: None,
1274
- style="display: none;")
1275
-
1276
- # เพิ่มช่องว่างด้านล่าง
1277
- st.markdown("<br>", unsafe_allow_html=True)
1278
 
1279
 
1280
  # Add theme-specific CSS
@@ -2015,7 +1977,7 @@ with st.sidebar:
2015
  col1, col2 = st.columns([3, 1])
2016
 
2017
  with col1:
2018
- # แสดงส่วนบนของหน้า
2019
  st.markdown("""
2020
  <div class="thai-eng">
2021
  <div class="thai">📖 เรื่องราวของคุณ</div>
@@ -2023,15 +1985,12 @@ with col1:
2023
  </div>
2024
  """, unsafe_allow_html=True)
2025
 
2026
- # ตรวจสอบว่ามีธีมที่เลือกแล้วหรือไม่
2027
  if not st.session_state.current_theme:
2028
- # ถ้ายังไม่มีธีม แสดงส่วนเลือกธีม
2029
  show_theme_selection()
2030
  else:
2031
- # ถ้ามีธีมแล้ว แสดงปุ่ม Change Theme
2032
- if st.button("🔄 Change Theme", key="change_theme_button",
2033
- help="คลิกเพื่อเปลี่ยนธีมเรื่องราว"):
2034
- # รีเซ็ตค่าเมื่อต้องการเปลี่ยนธีม
2035
  st.session_state.current_theme = None
2036
  st.session_state.theme_story_starter = None
2037
  st.session_state.story = []
@@ -2040,8 +1999,7 @@ with col1:
2040
  # แสดงเนื้อเรื่อง
2041
  show_story()
2042
 
2043
- # User Input Box (แสดงเฉพาะเมื่อมีธีมแล้ว)
2044
- if st.session_state.current_theme:
2045
  st.markdown("""
2046
  <div class="thai-eng">
2047
  <div class="thai">✏️ ถึงตาคุณแล้ว</div>
@@ -2049,20 +2007,15 @@ with col1:
2049
  </div>
2050
  """, unsafe_allow_html=True)
2051
 
2052
- # Text input with callback
2053
  st.text_area(
2054
  "เขียนต่อจากเรื่องราว | Continue the story:",
2055
  height=100,
2056
  key="text_input",
2057
- label_visibility="collapsed"
2058
  )
2059
 
2060
- # Submit button with callback
2061
- st.button(
2062
- "ส่งคำตอบ | Submit",
2063
- on_click=submit_story
2064
- )
2065
-
2066
  with col2:
2067
  # 1. Feedback Section (Most Important)
2068
  if st.session_state.feedback:
@@ -2125,7 +2078,7 @@ with col2:
2125
 
2126
  # 2. Writing Tools (Expandable)
2127
  with st.expander("✨ เครื่องมือช่วยเขียน | Writing Tools"):
2128
- if st.button("🎯 ขอคำใบ้ | Get Creative Prompt"):
2129
  prompt = get_creative_prompt()
2130
  st.markdown(f"""
2131
  <div class="thai-eng">
@@ -2134,10 +2087,9 @@ with col2:
2134
  </div>
2135
  """, unsafe_allow_html=True)
2136
 
2137
- # Vocabulary ย้ายมาอยู่ในนี้
2138
- if st.button("📚 คำศัพท์แนะนำ | Vocabulary Tips"):
2139
  vocab_suggestions = get_vocabulary_suggestions()
2140
- st.markdown("#### 📚 คำศัพท์น่ารู้ | Useful Words")
2141
  for word in vocab_suggestions:
2142
  st.markdown(f"• {word}")
2143
 
@@ -2148,44 +2100,18 @@ with col2:
2148
  # 4. Save Options (At the bottom)
2149
  st.markdown("### 💾 บันทึกเรื่องราว")
2150
  if st.session_state.story:
 
2151
  col1, col2 = st.columns(2)
2152
-
2153
  with col1:
2154
- # บันทึกเป็น PDF
2155
  pdf = create_story_pdf()
2156
- st.download_button(
2157
- label="📑 PDF",
2158
- data=pdf,
2159
- file_name=f"my_story_{datetime.now().strftime('%Y%m%d')}.pdf",
2160
- mime="application/pdf"
2161
- )
2162
-
2163
  with col2:
2164
- # บันทึกความก้าวหน้า (แก้ไขส่วนนี้)
2165
  story_data = {
2166
  'level': st.session_state.level,
2167
- 'date': datetime.now().isoformat(),
2168
  'story': st.session_state.story,
2169
- 'achievements': st.session_state.achievements,
2170
- 'points': st.session_state.points,
2171
- 'stats': {
2172
- 'total_sentences': st.session_state.stats['total_sentences'],
2173
- 'correct_first_try': st.session_state.stats['correct_first_try'],
2174
- 'accuracy_rate': st.session_state.stats['accuracy_rate'],
2175
- 'vocabulary_used': list(st.session_state.stats['vocabulary_used']) # แปลง set เป็น list
2176
- }
2177
  }
2178
-
2179
- # แปลงข้อมูลทั้งหมดที่อาจมี set
2180
- serializable_data = convert_sets_to_lists(story_data)
2181
-
2182
- st.download_button(
2183
- label="💾 Save",
2184
- data=json.dumps(serializable_data, indent=2),
2185
- file_name=f"story_progress_{datetime.now().strftime('%Y%m%d')}.json",
2186
- mime="application/json"
2187
- )
2188
-
2189
 
2190
  # Add new CSS for improved layout
2191
  st.markdown("""
 
1209
  </div>
1210
  """, unsafe_allow_html=True)
1211
 
1212
+ # สร้าง 4 columns
 
1213
  available_themes = get_available_themes(st.session_state.level)
1214
+ cols = st.columns(4)
1215
 
1216
+ # แสดงธีมในแต่ละ column
1217
+ for idx, (col, theme) in enumerate(zip(cols, available_themes)):
1218
+ with col:
1219
+ if st.button(
1220
+ f"{theme['icon']} {theme['name_th']}",
1221
+ key=f"theme_select_{idx}", # ใช้ index เป็น key
1222
+ use_container_width=True
1223
+ ):
1224
+ st.session_state.current_theme = theme['id']
1225
+ starter = generate_dynamic_story_starter(theme['id'], st.session_state.level)
1226
+ st.session_state.story = [{
1227
+ "role": "AI",
1228
+ "content": starter['en'],
1229
+ "thai_content": starter['th'],
1230
+ "is_starter": True
1231
+ }]
1232
+ st.rerun()
1233
+
1234
+ # คำอธิบายใต้ปุ่ม
1235
+ st.markdown(f"""
1236
+ <div style="font-size: 0.85em; color: #666; padding: 5px;">
1237
+ {theme['description_th']}
1238
+ </div>
1239
+ """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1240
 
1241
 
1242
  # Add theme-specific CSS
 
1977
  col1, col2 = st.columns([3, 1])
1978
 
1979
  with col1:
1980
+ # Story Display Box
1981
  st.markdown("""
1982
  <div class="thai-eng">
1983
  <div class="thai">📖 เรื่องราวของคุณ</div>
 
1985
  </div>
1986
  """, unsafe_allow_html=True)
1987
 
1988
+ # Theme Selection หรือ Change Theme
1989
  if not st.session_state.current_theme:
 
1990
  show_theme_selection()
1991
  else:
1992
+ # Show current theme and option to change
1993
+ if st.button("🔄 Change Theme"):
 
 
1994
  st.session_state.current_theme = None
1995
  st.session_state.theme_story_starter = None
1996
  st.session_state.story = []
 
1999
  # แสดงเนื้อเรื่อง
2000
  show_story()
2001
 
2002
+ # Text input area
 
2003
  st.markdown("""
2004
  <div class="thai-eng">
2005
  <div class="thai">✏️ ถึงตาคุณแล้ว</div>
 
2007
  </div>
2008
  """, unsafe_allow_html=True)
2009
 
 
2010
  st.text_area(
2011
  "เขียนต่อจากเรื่องราว | Continue the story:",
2012
  height=100,
2013
  key="text_input",
2014
+ label_visibility="collapsed"
2015
  )
2016
 
2017
+ st.button("ส่งคำตอบ | Submit", on_click=submit_story)
2018
+
 
 
 
 
2019
  with col2:
2020
  # 1. Feedback Section (Most Important)
2021
  if st.session_state.feedback:
 
2078
 
2079
  # 2. Writing Tools (Expandable)
2080
  with st.expander("✨ เครื่องมือช่วยเขียน | Writing Tools"):
2081
+ if st.button("🎯 ขอคำใบ้"):
2082
  prompt = get_creative_prompt()
2083
  st.markdown(f"""
2084
  <div class="thai-eng">
 
2087
  </div>
2088
  """, unsafe_allow_html=True)
2089
 
2090
+ if st.button("📚 คำศัพท์แนะนำ"):
 
2091
  vocab_suggestions = get_vocabulary_suggestions()
2092
+ st.markdown("#### 📚 คำศัพท์น่ารู้")
2093
  for word in vocab_suggestions:
2094
  st.markdown(f"• {word}")
2095
 
 
2100
  # 4. Save Options (At the bottom)
2101
  st.markdown("### 💾 บันทึกเรื่องราว")
2102
  if st.session_state.story:
2103
+ st.markdown("### 💾 บันทึกเรื่องราว")
2104
  col1, col2 = st.columns(2)
 
2105
  with col1:
 
2106
  pdf = create_story_pdf()
2107
+ st.download_button("📑 PDF", data=pdf, file_name="story.pdf", mime="application/pdf")
 
 
 
 
 
 
2108
  with col2:
 
2109
  story_data = {
2110
  'level': st.session_state.level,
 
2111
  'story': st.session_state.story,
2112
+ 'achievements': st.session_state.achievements
 
 
 
 
 
 
 
2113
  }
2114
+ st.download_button("💾 Save", data=json.dumps(story_data), file_name="story.json", mime="application/json")
 
 
 
 
 
 
 
 
 
 
2115
 
2116
  # Add new CSS for improved layout
2117
  st.markdown("""