Update app.py
Browse files
app.py
CHANGED
@@ -549,6 +549,8 @@ def init_session_state():
|
|
549 |
st.session_state.clear_input = False
|
550 |
if 'text_input' not in st.session_state:
|
551 |
st.session_state.text_input = ""
|
|
|
|
|
552 |
|
553 |
def init_theme_state():
|
554 |
"""Initialize theme-specific state variables"""
|
@@ -1882,42 +1884,35 @@ def show_story_input():
|
|
1882 |
<div class="eng">Your Turn</div>
|
1883 |
</div>
|
1884 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
1885 |
|
1886 |
-
#
|
1887 |
-
if 'clear_input' not in st.session_state:
|
1888 |
-
st.session_state.clear_input = False
|
1889 |
-
|
1890 |
-
# Default value for text input
|
1891 |
-
default_value = "" if st.session_state.clear_input else st.session_state.get('text_input', "")
|
1892 |
-
|
1893 |
-
# If clear_input flag is True, reset it
|
1894 |
-
if st.session_state.clear_input:
|
1895 |
-
st.session_state.clear_input = False
|
1896 |
-
|
1897 |
-
# Input area
|
1898 |
text_input = st.text_area(
|
1899 |
"เขียนต่อจากเรื่องราว | Continue the story:",
|
1900 |
-
value=
|
1901 |
height=100,
|
1902 |
key="story_input_area",
|
1903 |
help="พิมพ์ประโยคภาษาอังกฤษเพื่อต่อเรื่อง",
|
1904 |
label_visibility="collapsed"
|
1905 |
)
|
1906 |
-
|
1907 |
-
# Save current input to session state
|
1908 |
-
st.session_state.text_input = text_input
|
1909 |
-
|
1910 |
# Submit button with character count
|
1911 |
col1, col2 = st.columns([3, 1])
|
1912 |
with col1:
|
1913 |
-
if st.button("📝 ส่งคำตอบ | Submit", use_container_width=True):
|
1914 |
if not text_input.strip():
|
1915 |
st.warning("กรุณาเขียนข้อความก่อนส่ง")
|
1916 |
return
|
1917 |
|
1918 |
try:
|
1919 |
with st.spinner("กำลังวิเคราะห์ประโยค..."):
|
1920 |
-
|
|
|
|
|
|
|
1921 |
except Exception as e:
|
1922 |
logging.error(f"Error submitting story: {str(e)}")
|
1923 |
st.error("เกิดข้อผิดพลาดในการส่งคำตอบ กรุณาลองใหม่อีกครั้ง")
|
@@ -1961,26 +1956,18 @@ def handle_story_submission(text: str):
|
|
1961 |
|
1962 |
# Generate AI continuation
|
1963 |
try:
|
1964 |
-
|
1965 |
-
# ใช้ข้อความที่ถูกต้องสำหรับการต่อเรื่อง
|
1966 |
-
text_for_continuation = feedback_data['corrected'] if feedback_data.get('has_errors') else text
|
1967 |
-
|
1968 |
-
ai_response = generate_story_continuation(text_for_continuation, st.session_state.level)
|
1969 |
-
|
1970 |
-
# Log the AI response
|
1971 |
-
logging.info(f"AI Response generated: {ai_response}")
|
1972 |
-
|
1973 |
if ai_response and ai_response.strip():
|
1974 |
st.session_state.story.append({
|
1975 |
"role": "AI",
|
1976 |
"content": ai_response,
|
1977 |
"timestamp": datetime.now().isoformat()
|
1978 |
})
|
1979 |
-
logging.info("AI response added to story successfully")
|
1980 |
else:
|
1981 |
-
|
1982 |
-
|
1983 |
-
|
|
|
1984 |
st.session_state.story.append({
|
1985 |
"role": "AI",
|
1986 |
"content": fallback_response,
|
@@ -1988,20 +1975,22 @@ def handle_story_submission(text: str):
|
|
1988 |
})
|
1989 |
except Exception as e:
|
1990 |
logging.error(f"Error generating AI continuation: {str(e)}")
|
1991 |
-
|
1992 |
-
|
|
|
|
|
1993 |
st.session_state.story.append({
|
1994 |
"role": "AI",
|
1995 |
"content": fallback_response,
|
1996 |
"timestamp": datetime.now().isoformat()
|
1997 |
})
|
1998 |
|
|
|
|
|
|
|
1999 |
# Update session stats
|
2000 |
update_session_stats()
|
2001 |
|
2002 |
-
# Set flag to clear input on next rerun
|
2003 |
-
st.session_state.clear_input = True
|
2004 |
-
|
2005 |
# Rerun to update UI
|
2006 |
st.rerun()
|
2007 |
|
|
|
549 |
st.session_state.clear_input = False
|
550 |
if 'text_input' not in st.session_state:
|
551 |
st.session_state.text_input = ""
|
552 |
+
if 'story_input_area' not in st.session_state:
|
553 |
+
st.session_state.story_input_area = ""
|
554 |
|
555 |
def init_theme_state():
|
556 |
"""Initialize theme-specific state variables"""
|
|
|
1884 |
<div class="eng">Your Turn</div>
|
1885 |
</div>
|
1886 |
""", unsafe_allow_html=True)
|
1887 |
+
|
1888 |
+
# ใช้ callback function เพื่อเคลียร์ input
|
1889 |
+
def clear_text():
|
1890 |
+
st.session_state.story_input_area = ""
|
1891 |
|
1892 |
+
# Input area with default empty value and callback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1893 |
text_input = st.text_area(
|
1894 |
"เขียนต่อจากเรื่องราว | Continue the story:",
|
1895 |
+
value="",
|
1896 |
height=100,
|
1897 |
key="story_input_area",
|
1898 |
help="พิมพ์ประโยคภาษาอังกฤษเพื่อต่อเรื่อง",
|
1899 |
label_visibility="collapsed"
|
1900 |
)
|
1901 |
+
|
|
|
|
|
|
|
1902 |
# Submit button with character count
|
1903 |
col1, col2 = st.columns([3, 1])
|
1904 |
with col1:
|
1905 |
+
if st.button("📝 ส่งคำตอบ | Submit", use_container_width=True, on_click=clear_text):
|
1906 |
if not text_input.strip():
|
1907 |
st.warning("กรุณาเขียนข้อความก่อนส่ง")
|
1908 |
return
|
1909 |
|
1910 |
try:
|
1911 |
with st.spinner("กำลังวิเคราะห์ประโยค..."):
|
1912 |
+
if st.session_state.get('ending_mode'):
|
1913 |
+
handle_ending_mode(text_input.strip())
|
1914 |
+
else:
|
1915 |
+
handle_story_submission(text_input.strip())
|
1916 |
except Exception as e:
|
1917 |
logging.error(f"Error submitting story: {str(e)}")
|
1918 |
st.error("เกิดข้อผิดพลาดในการส่งคำตอบ กรุณาลองใหม่อีกครั้ง")
|
|
|
1956 |
|
1957 |
# Generate AI continuation
|
1958 |
try:
|
1959 |
+
ai_response = generate_story_continuation(text, st.session_state.level)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1960 |
if ai_response and ai_response.strip():
|
1961 |
st.session_state.story.append({
|
1962 |
"role": "AI",
|
1963 |
"content": ai_response,
|
1964 |
"timestamp": datetime.now().isoformat()
|
1965 |
})
|
|
|
1966 |
else:
|
1967 |
+
fallback_response = generate_fallback_response(
|
1968 |
+
st.session_state.current_theme,
|
1969 |
+
st.session_state.level
|
1970 |
+
)
|
1971 |
st.session_state.story.append({
|
1972 |
"role": "AI",
|
1973 |
"content": fallback_response,
|
|
|
1975 |
})
|
1976 |
except Exception as e:
|
1977 |
logging.error(f"Error generating AI continuation: {str(e)}")
|
1978 |
+
fallback_response = generate_fallback_response(
|
1979 |
+
st.session_state.current_theme,
|
1980 |
+
st.session_state.level
|
1981 |
+
)
|
1982 |
st.session_state.story.append({
|
1983 |
"role": "AI",
|
1984 |
"content": fallback_response,
|
1985 |
"timestamp": datetime.now().isoformat()
|
1986 |
})
|
1987 |
|
1988 |
+
# Clear input after successful submission
|
1989 |
+
st.session_state.story_input_area = ""
|
1990 |
+
|
1991 |
# Update session stats
|
1992 |
update_session_stats()
|
1993 |
|
|
|
|
|
|
|
1994 |
# Rerun to update UI
|
1995 |
st.rerun()
|
1996 |
|