Update app.py
Browse files
app.py
CHANGED
@@ -2651,8 +2651,20 @@ def show_sidebar():
|
|
2651 |
else:
|
2652 |
st.sidebar.info("ยังไม่มีเรื่องราวที่จะรีเซ็ต")
|
2653 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2654 |
def show_story_input():
|
2655 |
-
"""Display story input section with clear
|
2656 |
st.markdown("""
|
2657 |
<div class="thai-eng">
|
2658 |
<div class="thai">✏️ ถึงตาคุณแล้ว</div>
|
@@ -2660,55 +2672,66 @@ def show_story_input():
|
|
2660 |
</div>
|
2661 |
""", unsafe_allow_html=True)
|
2662 |
|
2663 |
-
# Initialize
|
2664 |
if 'clear_input' not in st.session_state:
|
2665 |
st.session_state.clear_input = False
|
2666 |
-
|
2667 |
-
|
2668 |
-
|
2669 |
-
|
2670 |
-
# If clear_input flag is True, reset it
|
2671 |
-
if st.session_state.clear_input:
|
2672 |
-
st.session_state.clear_input = False
|
2673 |
|
2674 |
# Show remaining sentences if in ending mode
|
2675 |
if st.session_state.get('ending_mode'):
|
2676 |
remaining = st.session_state.sentences_to_end
|
2677 |
st.info(f"🎭 โหมดจบเรื่อง - เหลืออีก {remaining} ประโยค")
|
2678 |
|
2679 |
-
# Input area
|
2680 |
text_input = st.text_area(
|
2681 |
"เขียนต่อจากเรื่องราว | Continue the story:",
|
2682 |
-
value=st.session_state.
|
2683 |
height=100,
|
2684 |
-
key="
|
2685 |
help="พิมพ์ประโยคภาษาอังกฤษเพื่อต่อเรื่อง",
|
2686 |
label_visibility="collapsed"
|
2687 |
)
|
2688 |
|
2689 |
-
#
|
2690 |
st.session_state.text_input = text_input
|
2691 |
|
2692 |
-
# Submit and Clear buttons
|
2693 |
col1, col2, col3 = st.columns([3, 1, 1])
|
2694 |
|
2695 |
with col1:
|
2696 |
-
|
2697 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2698 |
st.warning("กรุณาเขียนข้อความก่อนส่ง")
|
2699 |
-
return
|
2700 |
-
try:
|
2701 |
-
with st.spinner("กำลังวิเคราะห์ประโยค..."):
|
2702 |
-
handle_story_submission(text_input.strip())
|
2703 |
-
except Exception as e:
|
2704 |
-
logging.error(f"Error submitting story: {str(e)}")
|
2705 |
-
st.error("เกิดข้อผิดพลาดในการส่งคำตอบ กรุณาลองใหม่อีกครั้ง")
|
2706 |
|
2707 |
with col2:
|
2708 |
-
#
|
2709 |
-
if st.button(
|
2710 |
-
|
2711 |
-
st.session_state.
|
|
|
|
|
|
|
|
|
|
|
|
|
2712 |
st.rerun()
|
2713 |
|
2714 |
with col3:
|
@@ -2720,8 +2743,12 @@ def show_story_input():
|
|
2720 |
</div>
|
2721 |
""", unsafe_allow_html=True)
|
2722 |
|
|
|
|
|
|
|
|
|
2723 |
def handle_story_submission(text: str):
|
2724 |
-
"""Handle story submission
|
2725 |
if not st.session_state.story:
|
2726 |
st.error("กรุณาเลือกธีมเรื่องราวก่อนเริ่มเขียน")
|
2727 |
return
|
@@ -2729,6 +2756,8 @@ def handle_story_submission(text: str):
|
|
2729 |
# Check if in ending mode
|
2730 |
if st.session_state.get('ending_mode'):
|
2731 |
handle_ending_mode(text)
|
|
|
|
|
2732 |
return
|
2733 |
|
2734 |
# Regular processing
|
@@ -2795,14 +2824,23 @@ def handle_story_submission(text: str):
|
|
2795 |
# Update session stats
|
2796 |
update_session_stats()
|
2797 |
|
2798 |
-
#
|
2799 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2800 |
|
2801 |
# Rerun to update UI
|
2802 |
st.rerun()
|
2803 |
|
2804 |
except Exception as e:
|
2805 |
logging.error(f"Error in story submission: {str(e)}")
|
|
|
|
|
2806 |
raise
|
2807 |
|
2808 |
|
|
|
2651 |
else:
|
2652 |
st.sidebar.info("ยังไม่มีเรื่องราวที่จะรีเซ็ต")
|
2653 |
|
2654 |
+
def clear_input_state():
|
2655 |
+
"""Clear all input-related session states"""
|
2656 |
+
if 'text_input' in st.session_state:
|
2657 |
+
st.session_state.text_input = ""
|
2658 |
+
if 'story_input_area' in st.session_state:
|
2659 |
+
st.session_state.story_input_area = ""
|
2660 |
+
if 'last_submission' in st.session_state:
|
2661 |
+
del st.session_state.last_submission
|
2662 |
+
st.session_state.clear_input = True
|
2663 |
+
# Increment clear count to force new input field
|
2664 |
+
st.session_state.clear_count = st.session_state.get('clear_count', 0) + 1
|
2665 |
+
|
2666 |
def show_story_input():
|
2667 |
+
"""Display story input section with improved clear functionality"""
|
2668 |
st.markdown("""
|
2669 |
<div class="thai-eng">
|
2670 |
<div class="thai">✏️ ถึงตาคุณแล้ว</div>
|
|
|
2672 |
</div>
|
2673 |
""", unsafe_allow_html=True)
|
2674 |
|
2675 |
+
# Initialize states if not exists
|
2676 |
if 'clear_input' not in st.session_state:
|
2677 |
st.session_state.clear_input = False
|
2678 |
+
if 'text_input' not in st.session_state:
|
2679 |
+
st.session_state.text_input = ""
|
2680 |
+
if 'last_submission' not in st.session_state:
|
2681 |
+
st.session_state.last_submission = None
|
|
|
|
|
|
|
2682 |
|
2683 |
# Show remaining sentences if in ending mode
|
2684 |
if st.session_state.get('ending_mode'):
|
2685 |
remaining = st.session_state.sentences_to_end
|
2686 |
st.info(f"🎭 โหมดจบเรื่อง - เหลืออีก {remaining} ประโยค")
|
2687 |
|
2688 |
+
# Input area with key based on clear state
|
2689 |
text_input = st.text_area(
|
2690 |
"เขียนต่อจากเรื่องราว | Continue the story:",
|
2691 |
+
value=st.session_state.text_input,
|
2692 |
height=100,
|
2693 |
+
key=f"story_input_area_{st.session_state.get('clear_count', 0)}",
|
2694 |
help="พิมพ์ประโยคภาษาอังกฤษเพื่อต่อเรื่อง",
|
2695 |
label_visibility="collapsed"
|
2696 |
)
|
2697 |
|
2698 |
+
# Keep track of current input
|
2699 |
st.session_state.text_input = text_input
|
2700 |
|
2701 |
+
# Submit and Clear buttons
|
2702 |
col1, col2, col3 = st.columns([3, 1, 1])
|
2703 |
|
2704 |
with col1:
|
2705 |
+
submit_button = st.button(
|
2706 |
+
"📝 ส่งคำตอบ | Submit",
|
2707 |
+
use_container_width=True,
|
2708 |
+
disabled=not text_input.strip() # Disable if empty
|
2709 |
+
)
|
2710 |
+
|
2711 |
+
if submit_button:
|
2712 |
+
if text_input.strip():
|
2713 |
+
try:
|
2714 |
+
# Save current submission
|
2715 |
+
st.session_state.last_submission = text_input.strip()
|
2716 |
+
with st.spinner("กำลังวิเคราะห์ประโยค..."):
|
2717 |
+
handle_story_submission(text_input.strip())
|
2718 |
+
except Exception as e:
|
2719 |
+
logging.error(f"Error submitting story: {str(e)}")
|
2720 |
+
st.error("เกิดข้อผิดพลาดในการส่งคำตอบ กรุณาลองใหม่อีกครั้ง")
|
2721 |
+
else:
|
2722 |
st.warning("กรุณาเขียนข้อความก่อนส่ง")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2723 |
|
2724 |
with col2:
|
2725 |
+
# Clear button with unique key
|
2726 |
+
if st.button(
|
2727 |
+
"🗑️ ล้างข้อความ | Clear",
|
2728 |
+
key=f"clear_button_{st.session_state.get('clear_count', 0)}",
|
2729 |
+
use_container_width=True,
|
2730 |
+
disabled=not text_input.strip() # Disable if already empty
|
2731 |
+
):
|
2732 |
+
# Increment clear count to force new key generation
|
2733 |
+
st.session_state.clear_count = st.session_state.get('clear_count', 0) + 1
|
2734 |
+
clear_input_state()
|
2735 |
st.rerun()
|
2736 |
|
2737 |
with col3:
|
|
|
2743 |
</div>
|
2744 |
""", unsafe_allow_html=True)
|
2745 |
|
2746 |
+
# Warning for long text
|
2747 |
+
if char_count > 200:
|
2748 |
+
st.warning("⚠️ ข้อความยาวเกิน 200 ตัวอักษร")
|
2749 |
+
|
2750 |
def handle_story_submission(text: str):
|
2751 |
+
"""Handle story submission with improved state management"""
|
2752 |
if not st.session_state.story:
|
2753 |
st.error("กรุณาเลือกธีมเรื่องราวก่อนเริ่มเขียน")
|
2754 |
return
|
|
|
2756 |
# Check if in ending mode
|
2757 |
if st.session_state.get('ending_mode'):
|
2758 |
handle_ending_mode(text)
|
2759 |
+
# Clear input after handling ending mode
|
2760 |
+
clear_input_state()
|
2761 |
return
|
2762 |
|
2763 |
# Regular processing
|
|
|
2824 |
# Update session stats
|
2825 |
update_session_stats()
|
2826 |
|
2827 |
+
# Clear input state completely before rerun
|
2828 |
+
clear_input_state()
|
2829 |
+
|
2830 |
+
# Make sure we don't have any lingering text
|
2831 |
+
if 'story_input_area' in st.session_state:
|
2832 |
+
st.session_state.story_input_area = ""
|
2833 |
+
|
2834 |
+
# Increment clear count to force new input field
|
2835 |
+
st.session_state.clear_count = st.session_state.get('clear_count', 0) + 1
|
2836 |
|
2837 |
# Rerun to update UI
|
2838 |
st.rerun()
|
2839 |
|
2840 |
except Exception as e:
|
2841 |
logging.error(f"Error in story submission: {str(e)}")
|
2842 |
+
# Clear input even on error to prevent stuck state
|
2843 |
+
clear_input_state()
|
2844 |
raise
|
2845 |
|
2846 |
|