Rathapoom commited on
Commit
c5ef80b
·
verified ·
1 Parent(s): 1ec9a5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -83
app.py CHANGED
@@ -1493,22 +1493,9 @@ def show_story():
1493
  """, unsafe_allow_html=True)
1494
  else:
1495
  # Regular AI Response with ending mode indicator
1496
- ending_badge = ""
1497
  if st.session_state.get('ending_mode'):
1498
- remaining = st.session_state.sentences_to_end
1499
- ending_badge = f"""
1500
- <div style="
1501
- display: inline-block;
1502
- background-color: #fff3e0;
1503
- padding: 3px 8px;
1504
- border-radius: 12px;
1505
- font-size: 0.8em;
1506
- color: #f57c00;
1507
- margin-left: 10px;
1508
- ">
1509
- 🎭 Ending: {remaining} sentences left
1510
- </div>
1511
- """
1512
 
1513
  st.markdown(f"""
1514
  <div style="
@@ -1518,35 +1505,24 @@ def show_story():
1518
  margin: 8px 0;
1519
  border-left: 4px solid #4caf50;
1520
  ">
1521
- <div style="color: #2e7d32; display: flex; align-items: center;">
1522
- <span>🤖 AI: {entry['content']}</span>
1523
- {ending_badge}
 
 
1524
  </div>
1525
  </div>
1526
  """, unsafe_allow_html=True)
1527
 
1528
  elif entry['role'] == 'You':
1529
- # User Entry with ending mode indicator
1530
  status_icon = "✅" if entry.get('is_correct') else "✍️"
1531
  bg_color = "#e8f5e9" if entry.get('is_correct') else "#fff"
1532
  border_color = "#4caf50" if entry.get('is_correct') else "#1e88e5"
1533
 
1534
- ending_badge = ""
1535
  if st.session_state.get('ending_mode'):
1536
- remaining = st.session_state.sentences_to_end
1537
- ending_badge = f"""
1538
- <div style="
1539
- display: inline-block;
1540
- background-color: #fff3e0;
1541
- padding: 3px 8px;
1542
- border-radius: 12px;
1543
- font-size: 0.8em;
1544
- color: #f57c00;
1545
- margin-left: 10px;
1546
- ">
1547
- 🎭 Ending: {remaining} sentences left
1548
- </div>
1549
- """
1550
 
1551
  st.markdown(f"""
1552
  <div style="
@@ -1556,9 +1532,11 @@ def show_story():
1556
  margin: 8px 0;
1557
  border-left: 4px solid {border_color};
1558
  ">
1559
- <div style="color: #333; display: flex; align-items: center;">
1560
- <span>👤 You: {status_icon} {entry['content']}</span>
1561
- {ending_badge}
 
 
1562
  </div>
1563
  {f'<div style="font-size: 0.9em; color: #666; margin-top: 5px;">{entry.get("feedback", "")}</div>' if entry.get('feedback') else ''}
1564
  </div>
@@ -1636,41 +1614,25 @@ def handle_ending_mode(text: str):
1636
  try:
1637
  remaining = st.session_state.sentences_to_end
1638
 
1639
- # Show remaining sentences warning
1640
- st.warning(f"""
1641
- 🎭 Ending Mode Active:
1642
- - Remaining sentences: {remaining}
1643
- - Ending Type: {st.session_state.ending_type}
1644
-
1645
- Try to work towards completing your story!
1646
- """)
1647
-
1648
- # Get specialized ending feedback
1649
- feedback_data = provide_feedback(text, st.session_state.level)
1650
- st.session_state.feedback = feedback_data
1651
- is_correct = not feedback_data.get('has_errors', False)
1652
-
1653
  # Add user's sentence
1654
  st.session_state.story.append({
1655
  "role": "You",
1656
  "content": text,
1657
  "is_corrected": False,
1658
- "is_correct": is_correct,
1659
  "timestamp": datetime.now().isoformat()
1660
  })
1661
 
1662
- # Update stats and achievements
1663
- words = set(text.lower().split())
1664
- st.session_state.stats['vocabulary_used'].update(words)
1665
- update_points(is_correct)
1666
- update_achievements()
1667
 
1668
- # Generate AI's ending continuation
1669
- if remaining > 1: # Still have sentences remaining
1670
  ai_response = generate_ending_continuation(
1671
  text,
1672
  ending_type=st.session_state.ending_type,
1673
- remaining_sentences=remaining - 1
1674
  )
1675
 
1676
  st.session_state.story.append({
@@ -1679,10 +1641,11 @@ def handle_ending_mode(text: str):
1679
  "timestamp": datetime.now().isoformat()
1680
  })
1681
 
1682
- # Decrease remaining sentences count
1683
- st.session_state.sentences_to_end = remaining - 2 # -2 because both user and AI added sentences
1684
 
1685
- else: # Last sentence - complete the story
 
1686
  final_response = generate_final_ending(
1687
  st.session_state.story,
1688
  st.session_state.ending_type
@@ -1699,10 +1662,7 @@ def handle_ending_mode(text: str):
1699
  st.session_state.story_completed = True
1700
  complete_story()
1701
 
1702
- # Update session stats
1703
- update_session_stats()
1704
-
1705
- # Clear input and rerun
1706
  st.session_state.story_input_area = ""
1707
  st.rerun()
1708
 
@@ -2423,27 +2383,80 @@ def main():
2423
  else:
2424
  # Show story progress if story exists
2425
  if st.session_state.story:
2426
- show_story_progress()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2427
 
2428
- # Show ending options if story is long enough and not in ending mode
2429
- if (len(st.session_state.story) >= 10 and
2430
- not st.session_state.ending_mode):
2431
- show_story_ending_options()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2432
 
2433
- # Show ending mode warning if active
2434
- if st.session_state.ending_mode:
2435
- st.warning(f"""
2436
- 🎭 กำลังอยู่ในโหมดจบเรื่อง - เหลือ {st.session_state.sentences_to_end} ประโยค
2437
- \nรูปแบบการจบ: {st.session_state.ending_type}
2438
- """)
 
 
 
 
 
2439
 
2440
- # Show main interface
2441
  show_main_interface()
2442
 
2443
- # Check if story is complete
2444
- if (st.session_state.ending_mode and
2445
- st.session_state.sentences_to_end <= 0):
2446
- complete_story()
 
 
 
 
 
 
 
 
 
 
 
 
 
2447
 
2448
  # Handle reset if needed
2449
  if st.session_state.should_reset:
 
1493
  """, unsafe_allow_html=True)
1494
  else:
1495
  # Regular AI Response with ending mode indicator
1496
+ ending_info = ""
1497
  if st.session_state.get('ending_mode'):
1498
+ ending_info = f"🎭 เหลืออีก {st.session_state.sentences_to_end} ประโยค"
 
 
 
 
 
 
 
 
 
 
 
 
 
1499
 
1500
  st.markdown(f"""
1501
  <div style="
 
1505
  margin: 8px 0;
1506
  border-left: 4px solid #4caf50;
1507
  ">
1508
+ <div style="color: #2e7d32;">
1509
+ <div style="display: flex; justify-content: space-between; align-items: center;">
1510
+ <span>🤖 AI: {entry['content']}</span>
1511
+ <span style="color: #f57c00; font-size: 0.9em;">{ending_info}</span>
1512
+ </div>
1513
  </div>
1514
  </div>
1515
  """, unsafe_allow_html=True)
1516
 
1517
  elif entry['role'] == 'You':
1518
+ # User Entry
1519
  status_icon = "✅" if entry.get('is_correct') else "✍️"
1520
  bg_color = "#e8f5e9" if entry.get('is_correct') else "#fff"
1521
  border_color = "#4caf50" if entry.get('is_correct') else "#1e88e5"
1522
 
1523
+ ending_info = ""
1524
  if st.session_state.get('ending_mode'):
1525
+ ending_info = f"🎭 เหลืออีก {st.session_state.sentences_to_end} ประโยค"
 
 
 
 
 
 
 
 
 
 
 
 
 
1526
 
1527
  st.markdown(f"""
1528
  <div style="
 
1532
  margin: 8px 0;
1533
  border-left: 4px solid {border_color};
1534
  ">
1535
+ <div style="color: #333;">
1536
+ <div style="display: flex; justify-content: space-between; align-items: center;">
1537
+ <span>👤 You: {status_icon} {entry['content']}</span>
1538
+ <span style="color: #f57c00; font-size: 0.9em;">{ending_info}</span>
1539
+ </div>
1540
  </div>
1541
  {f'<div style="font-size: 0.9em; color: #666; margin-top: 5px;">{entry.get("feedback", "")}</div>' if entry.get('feedback') else ''}
1542
  </div>
 
1614
  try:
1615
  remaining = st.session_state.sentences_to_end
1616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1617
  # Add user's sentence
1618
  st.session_state.story.append({
1619
  "role": "You",
1620
  "content": text,
1621
  "is_corrected": False,
1622
+ "is_correct": True, # We don't check correctness in ending mode
1623
  "timestamp": datetime.now().isoformat()
1624
  })
1625
 
1626
+ # Decrease remaining count
1627
+ remaining -= 1
1628
+ st.session_state.sentences_to_end = remaining
 
 
1629
 
1630
+ if remaining > 0:
1631
+ # Generate AI's ending continuation
1632
  ai_response = generate_ending_continuation(
1633
  text,
1634
  ending_type=st.session_state.ending_type,
1635
+ remaining_sentences=remaining
1636
  )
1637
 
1638
  st.session_state.story.append({
 
1641
  "timestamp": datetime.now().isoformat()
1642
  })
1643
 
1644
+ # Decrease for AI's sentence
1645
+ st.session_state.sentences_to_end = remaining - 1
1646
 
1647
+ else:
1648
+ # Generate final ending
1649
  final_response = generate_final_ending(
1650
  st.session_state.story,
1651
  st.session_state.ending_type
 
1662
  st.session_state.story_completed = True
1663
  complete_story()
1664
 
1665
+ # Clear input
 
 
 
1666
  st.session_state.story_input_area = ""
1667
  st.rerun()
1668
 
 
2383
  else:
2384
  # Show story progress if story exists
2385
  if st.session_state.story:
2386
+ # แสดงความคืบหน้าของเรื่อง
2387
+ total_sentences = len(st.session_state.story)
2388
+ st.markdown(f"""
2389
+ <div style="
2390
+ background-color: #f0f7ff;
2391
+ padding: 15px;
2392
+ border-radius: 10px;
2393
+ margin: 10px 0;
2394
+ ">
2395
+ <div style="margin-bottom: 10px;">
2396
+ 📊 ความยาวเรื่อง: {total_sentences} ประโยค
2397
+ </div>
2398
+ <div class="progress-bar">
2399
+ <div class="progress-bar-fill" style="width: {min(total_sentences * 10, 100)}%;"></div>
2400
+ </div>
2401
+ <div style="font-size: 0.9em; color: #666; margin-top: 5px;">
2402
+ เรื่องควรยาว 10-20 ประโยค เพื่อความสมบูรณ์
2403
+ </div>
2404
+ </div>
2405
+ """, unsafe_allow_html=True)
2406
 
2407
+ # แสดงตัวเลือกจบเรื่องเมื่อเรื่องยาวพอ
2408
+ if (total_sentences >= 10 and not st.session_state.get('ending_mode')):
2409
+ st.markdown("### 🎭 ต้องการจบเรื่องหรือไม่?")
2410
+ ending_type = st.radio(
2411
+ "เลือกวิธีจบเรื่อง:",
2412
+ options=[
2413
+ "Happy Ending - จบแบบมีความสุข",
2414
+ "Mysterious Ending - จบแบบทิ้งท้ายให้คิดต่อ",
2415
+ "Lesson Learned - จบแบบได้ข้อคิด",
2416
+ "Surprise Ending - จบแบบพลิกความคาดหมาย"
2417
+ ],
2418
+ index=0,
2419
+ key="ending_type_selector"
2420
+ )
2421
+
2422
+ if st.button("🎬 เริ่มจบเรื่อง", use_container_width=True):
2423
+ st.session_state.ending_mode = True
2424
+ st.session_state.ending_type = ending_type
2425
+ st.session_state.sentences_to_end = 5
2426
+ st.rerun()
2427
 
2428
+ # แสดงแจ้งเตือนโหมดจบเรื่อง
2429
+ if st.session_state.get('ending_mode'):
2430
+ remaining = st.session_state.sentences_to_end
2431
+ if remaining > 0:
2432
+ st.warning(f"""
2433
+ 🎭 กำลังอยู่ในโหมดจบเรื่อง
2434
+ - เหลืออีก {remaining} ประโยค
2435
+ - รูปแบบการจบ: {st.session_state.ending_type}
2436
+
2437
+ พยายามเขียนให้เรื่องจบอย่างสมบูรณ์!
2438
+ """)
2439
 
2440
+ # แสดงส่วนหลัก
2441
  show_main_interface()
2442
 
2443
+ # ตรวจสอบการจบเรื่อง
2444
+ if st.session_state.get('ending_mode'):
2445
+ if st.session_state.sentences_to_end <= 0:
2446
+ if not st.session_state.get('story_completed'):
2447
+ complete_story()
2448
+ st.balloons()
2449
+ elif st.session_state.sentences_to_end == 1:
2450
+ st.info("⚡ เหลือประโยคสุดท้ายแล้ว! เขียนให้จบอย่างสวยงาม")
2451
+
2452
+ # Handle reset if needed
2453
+ if st.session_state.get('should_reset'):
2454
+ reset_story()
2455
+ # Reset ending states
2456
+ st.session_state.ending_mode = False
2457
+ st.session_state.sentences_to_end = 5
2458
+ st.session_state.ending_type = None
2459
+ st.session_state.story_completed = False
2460
 
2461
  # Handle reset if needed
2462
  if st.session_state.should_reset: