Rathapoom commited on
Commit
7cef3bc
·
verified ·
1 Parent(s): 0f35c09

Update app_work.py

Browse files
Files changed (1) hide show
  1. app_work.py +221 -2
app_work.py CHANGED
@@ -1,4 +1,4 @@
1
- # === 1. IMPORTS & SETUP ===
2
  import streamlit as st
3
  import json
4
  import datetime
@@ -1503,6 +1503,192 @@ def show_story():
1503
  </div>
1504
  """, unsafe_allow_html=True)
1505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1506
  def show_feedback_section():
1507
  """Display writing feedback section"""
1508
  if not st.session_state.feedback:
@@ -1996,12 +2182,19 @@ def show_main_interface():
1996
 
1997
  # === 6. MAIN APPLICATION LOGIC ===
1998
  def main():
1999
- """Main application entry point"""
2000
  try:
2001
  # Initialize states
2002
  init_session_state()
2003
  init_theme_state()
2004
 
 
 
 
 
 
 
 
 
2005
  # Add watermark
2006
  st.markdown("""
2007
  <div style='position: fixed; bottom: 10px; right: 10px; z-index: 1000;
@@ -2028,11 +2221,37 @@ def main():
2028
  if not st.session_state.current_theme:
2029
  show_theme_selection()
2030
  else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2031
  show_main_interface()
 
 
 
 
 
2032
 
2033
  # Handle reset if needed
2034
  if st.session_state.should_reset:
2035
  reset_story()
 
 
 
 
2036
 
2037
  # Auto-save progress periodically
2038
  if st.session_state.story:
 
1
+ # === 1. IMPORTS & SETUP ===
2
  import streamlit as st
3
  import json
4
  import datetime
 
1503
  </div>
1504
  """, unsafe_allow_html=True)
1505
 
1506
+ def show_story_progress():
1507
+ """Display story progress metrics"""
1508
+ if st.session_state.story:
1509
+ total_sentences = len(st.session_state.story)
1510
+ st.markdown(f"""
1511
+ <div style="
1512
+ background-color: #f0f7ff;
1513
+ padding: 15px;
1514
+ border-radius: 10px;
1515
+ margin: 10px 0;
1516
+ ">
1517
+ <div style="margin-bottom: 10px;">
1518
+ 📊 ความยาวเรื่อง: {total_sentences} ประโยค
1519
+ </div>
1520
+ <div class="progress-bar">
1521
+ <div class="progress-bar-fill" style="width: {min(total_sentences * 10, 100)}%;"></div>
1522
+ </div>
1523
+ <div style="font-size: 0.9em; color: #666; margin-top: 5px;">
1524
+ เรื่องควรยาว 10-20 ประโยค เพื่อความสมบูรณ์
1525
+ </div>
1526
+ </div>
1527
+ """, unsafe_allow_html=True)
1528
+
1529
+ def show_story_ending_options():
1530
+ """Display story ending options and guidance"""
1531
+ if len(st.session_state.story) >= 10: # แสดงตัวเลือกเมื่อเรื่องยาวพอ
1532
+ st.markdown("### 🎭 ต้องการจบเรื่องหรือไม่?")
1533
+
1534
+ # แสดงตัวเลือกวิธีจบเรื่อง
1535
+ ending_type = st.radio(
1536
+ "เลือกวิธีจบเรื่อง:",
1537
+ options=[
1538
+ "Happy Ending - จบแบบมีความสุข",
1539
+ "Mysterious Ending - จบแบบทิ้งท้ายให้คิดต่อ",
1540
+ "Lesson Learned - จบแบบได้ข้อคิด",
1541
+ "Surprise Ending - จบแบบพลิกความคาดหมาย"
1542
+ ],
1543
+ index=0,
1544
+ help="เลือกรูปแบบการจบเรื่องที่คุณต้องการ"
1545
+ )
1546
+
1547
+ if st.button("🎬 เริ่มจบเรื่อง", use_container_width=True):
1548
+ st.session_state.ending_mode = True
1549
+ st.session_state.ending_type = ending_type
1550
+ st.session_state.sentences_to_end = 5
1551
+ st.rerun()
1552
+
1553
+ def handle_ending_mode(text: str):
1554
+ """Handle story submission during ending mode"""
1555
+ remaining = st.session_state.sentences_to_end
1556
+
1557
+ # แสดงการแจ้งเตือนจำนวนประโยคที่เหลือ
1558
+ st.info(f"🎯 เหลืออีก {remaining} ประโยคในการจบเรื่อง")
1559
+
1560
+ # สร้าง prompt พิเศษสำหรับ AI ในโหมดจบเรื่อง
1561
+ ending_prompts = {
1562
+ "Happy Ending": "Work towards a positive and uplifting conclusion",
1563
+ "Mysterious Ending": "Create an intriguing open-ended conclusion",
1564
+ "Lesson Learned": "Incorporate a meaningful life lesson",
1565
+ "Surprise Ending": "Build up to an unexpected twist"
1566
+ }
1567
+
1568
+ # ปรับ AI response ให้พยายามจบเรื่อง
1569
+ modified_continuation = generate_ending_continuation(
1570
+ text,
1571
+ ending_type=st.session_state.ending_type,
1572
+ remaining_sentences=remaining
1573
+ )
1574
+
1575
+ # อัพเดทจำนวนประโยคที่เหลือ
1576
+ st.session_state.sentences_to_end -= 1
1577
+
1578
+ # ตรวจสอบว่าถึงจบเรื่องหรือยัง
1579
+ if st.session_state.sentences_to_end <= 0:
1580
+ complete_story()
1581
+
1582
+ def generate_ending_continuation(text: str, ending_type: str, remaining_sentences: int) -> str:
1583
+ """Generate AI continuation focusing on story conclusion"""
1584
+ try:
1585
+ ending_prompts = {
1586
+ "Happy Ending": """
1587
+ Role: Story concluder aiming for a happy ending
1588
+ Goal: Create a satisfying, positive conclusion
1589
+ Rules:
1590
+ - Build towards joy, success, or resolution
1591
+ - Use uplifting and positive language
1592
+ - Connect to previous story elements
1593
+ """,
1594
+ "Mysterious Ending": """
1595
+ Role: Mystery writer creating intrigue
1596
+ Goal: Leave readers thinking and wondering
1597
+ Rules:
1598
+ - Add subtle hints and clues
1599
+ - Create atmospheric descriptions
1600
+ - Leave some questions unanswered
1601
+ """,
1602
+ "Lesson Learned": """
1603
+ Role: Moral story concluder
1604
+ Goal: Incorporate meaningful life lessons
1605
+ Rules:
1606
+ - Connect actions to consequences
1607
+ - Show character growth
1608
+ - Express the moral naturally
1609
+ """,
1610
+ "Surprise Ending": """
1611
+ Role: Plot twist creator
1612
+ Goal: Deliver unexpected but satisfying conclusion
1613
+ Rules:
1614
+ - Plant subtle hints earlier
1615
+ - Subvert expectations logically
1616
+ - Maintain story coherence
1617
+ """
1618
+ }
1619
+
1620
+ response = client.chat.completions.create(
1621
+ model="gpt-4",
1622
+ messages=[
1623
+ {
1624
+ "role": "system",
1625
+ "content": f"""
1626
+ {ending_prompts[ending_type]}
1627
+
1628
+ Additional Rules:
1629
+ - You have {remaining_sentences} sentences to conclude
1630
+ - Each response should be max 2 sentences
1631
+ - Build towards the finale naturally
1632
+ - Connect to previous story elements
1633
+ """
1634
+ },
1635
+ {
1636
+ "role": "user",
1637
+ "content": f"Continue and work towards ending this story: {text}"
1638
+ }
1639
+ ],
1640
+ max_tokens=100,
1641
+ temperature=0.7
1642
+ )
1643
+
1644
+ return response.choices[0].message.content.strip()
1645
+
1646
+ except Exception as e:
1647
+ logging.error(f"Error generating ending: {str(e)}")
1648
+ return "The story moved towards its conclusion..."
1649
+
1650
+ def complete_story():
1651
+ """Handle story completion and celebration"""
1652
+ st.balloons() # แสดงเอฟเฟคฉลอง
1653
+
1654
+ # สร้าง Story Summary
1655
+ story_summary = generate_story_summary(st.session_state.story)
1656
+
1657
+ # แสดงหน้าจบเรื่อง
1658
+ st.markdown(f"""
1659
+ <div style="
1660
+ background-color: #e8f5e9;
1661
+ padding: 20px;
1662
+ border-radius: 10px;
1663
+ text-align: center;
1664
+ margin: 20px 0;
1665
+ ">
1666
+ <h2>🎉 ยินดีด้วย! คุณเขียนเรื่องราวจบสมบูรณ์แล้ว</h2>
1667
+
1668
+ <div style="margin: 20px 0;">
1669
+ <h3>📝 สรุปเรื่องราว</h3>
1670
+ <p>{story_summary}</p>
1671
+ </div>
1672
+
1673
+ <div style="margin: 20px 0;">
1674
+ <h3>🏆 ความสำเร็จ</h3>
1675
+ <p>จำนวนประโยค: {len(st.session_state.story)}</p>
1676
+ <p>คำศัพท์ที่ใช้: {len(st.session_state.stats['vocabulary_used'])}</p>
1677
+ <p>ความแม่นยำ: {st.session_state.stats['accuracy_rate']:.1f}%</p>
1678
+ </div>
1679
+ </div>
1680
+ """, unsafe_allow_html=True)
1681
+
1682
+ # แสดงตัวเลือกหลังจบเรื่อง
1683
+ col1, col2 = st.columns(2)
1684
+ with col1:
1685
+ if st.button("💾 บันทึกเรื่องราว", use_container_width=True):
1686
+ save_completed_story()
1687
+ with col2:
1688
+ if st.button("🔄 เริ่มเรื่องใหม่", use_container_width=True):
1689
+ reset_story()
1690
+ st.rerun()
1691
+
1692
  def show_feedback_section():
1693
  """Display writing feedback section"""
1694
  if not st.session_state.feedback:
 
2182
 
2183
  # === 6. MAIN APPLICATION LOGIC ===
2184
  def main():
 
2185
  try:
2186
  # Initialize states
2187
  init_session_state()
2188
  init_theme_state()
2189
 
2190
+ # Initialize ending system state if not exists
2191
+ if 'ending_mode' not in st.session_state:
2192
+ st.session_state.ending_mode = False
2193
+ if 'sentences_to_end' not in st.session_state:
2194
+ st.session_state.sentences_to_end = 5
2195
+ if 'ending_type' not in st.session_state:
2196
+ st.session_state.ending_type = None
2197
+
2198
  # Add watermark
2199
  st.markdown("""
2200
  <div style='position: fixed; bottom: 10px; right: 10px; z-index: 1000;
 
2221
  if not st.session_state.current_theme:
2222
  show_theme_selection()
2223
  else:
2224
+ # Show story progress if story exists
2225
+ if st.session_state.story:
2226
+ show_story_progress()
2227
+
2228
+ # Show ending options if story is long enough and not in ending mode
2229
+ if (len(st.session_state.story) >= 10 and
2230
+ not st.session_state.ending_mode):
2231
+ show_story_ending_options()
2232
+
2233
+ # Show ending mode warning if active
2234
+ if st.session_state.ending_mode:
2235
+ st.warning(f"""
2236
+ 🎭 กำลังอยู่ในโหมดจบเรื่อง - เห���ือ {st.session_state.sentences_to_end} ประโยค
2237
+ \nรูปแบบการจบ: {st.session_state.ending_type}
2238
+ """)
2239
+
2240
+ # Show main interface
2241
  show_main_interface()
2242
+
2243
+ # Check if story is complete
2244
+ if (st.session_state.ending_mode and
2245
+ st.session_state.sentences_to_end <= 0):
2246
+ complete_story()
2247
 
2248
  # Handle reset if needed
2249
  if st.session_state.should_reset:
2250
  reset_story()
2251
+ # Reset ending mode states
2252
+ st.session_state.ending_mode = False
2253
+ st.session_state.sentences_to_end = 5
2254
+ st.session_state.ending_type = None
2255
 
2256
  # Auto-save progress periodically
2257
  if st.session_state.story: