Update app.py
Browse files
app.py
CHANGED
@@ -1809,14 +1809,41 @@ def handle_story_submission(text: str):
|
|
1809 |
update_achievements()
|
1810 |
|
1811 |
# Generate AI continuation
|
1812 |
-
|
1813 |
-
|
1814 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1815 |
st.session_state.story.append({
|
1816 |
"role": "AI",
|
1817 |
"content": ai_response,
|
1818 |
"timestamp": datetime.now().isoformat()
|
1819 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1820 |
|
1821 |
# Update session stats
|
1822 |
update_session_stats()
|
@@ -1831,6 +1858,31 @@ def handle_story_submission(text: str):
|
|
1831 |
logging.error(f"Error in story submission: {str(e)}")
|
1832 |
raise
|
1833 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1834 |
def show_main_interface():
|
1835 |
"""Display main story interface"""
|
1836 |
col1, col2 = st.columns([3, 1])
|
|
|
1809 |
update_achievements()
|
1810 |
|
1811 |
# Generate AI continuation
|
1812 |
+
try:
|
1813 |
+
logging.info("Attempting to generate AI continuation...")
|
1814 |
+
# ใช้ข้อความที่ถูกต้องสำหรับการต่อเรื่อง
|
1815 |
+
text_for_continuation = feedback_data['corrected'] if feedback_data.get('has_errors') else text
|
1816 |
+
|
1817 |
+
ai_response = generate_story_continuation(text_for_continuation, st.session_state.level)
|
1818 |
+
|
1819 |
+
# Log the AI response
|
1820 |
+
logging.info(f"AI Response generated: {ai_response}")
|
1821 |
+
|
1822 |
+
if ai_response and ai_response.strip():
|
1823 |
st.session_state.story.append({
|
1824 |
"role": "AI",
|
1825 |
"content": ai_response,
|
1826 |
"timestamp": datetime.now().isoformat()
|
1827 |
})
|
1828 |
+
logging.info("AI response added to story successfully")
|
1829 |
+
else:
|
1830 |
+
logging.error("AI generated empty response")
|
1831 |
+
# กรณีที่ AI ไม่สร้างประโยค ให้สร้างประโยคง่ายๆ ตาม theme
|
1832 |
+
fallback_response = generate_fallback_response(st.session_state.current_theme, st.session_state.level)
|
1833 |
+
st.session_state.story.append({
|
1834 |
+
"role": "AI",
|
1835 |
+
"content": fallback_response,
|
1836 |
+
"timestamp": datetime.now().isoformat()
|
1837 |
+
})
|
1838 |
+
except Exception as e:
|
1839 |
+
logging.error(f"Error generating AI continuation: {str(e)}")
|
1840 |
+
# สร้าง fallback response เมื่อเกิดข้อผิดพลาด
|
1841 |
+
fallback_response = generate_fallback_response(st.session_state.current_theme, st.session_state.level)
|
1842 |
+
st.session_state.story.append({
|
1843 |
+
"role": "AI",
|
1844 |
+
"content": fallback_response,
|
1845 |
+
"timestamp": datetime.now().isoformat()
|
1846 |
+
})
|
1847 |
|
1848 |
# Update session stats
|
1849 |
update_session_stats()
|
|
|
1858 |
logging.error(f"Error in story submission: {str(e)}")
|
1859 |
raise
|
1860 |
|
1861 |
+
def generate_fallback_response(theme_id: str, level: str) -> str:
|
1862 |
+
"""Generate a simple fallback response when AI continuation fails"""
|
1863 |
+
try:
|
1864 |
+
theme = story_themes.get(theme_id, {})
|
1865 |
+
if theme:
|
1866 |
+
# ใช้คำศัพท์จาก theme และ level ที่เลือก
|
1867 |
+
vocab = theme.get('vocabulary', {}).get(level, [])
|
1868 |
+
if vocab:
|
1869 |
+
word = random.choice(vocab)
|
1870 |
+
|
1871 |
+
# สร้างประโยคตาม level
|
1872 |
+
if level == 'Beginner':
|
1873 |
+
return f"The story continues with {word}..."
|
1874 |
+
elif level == 'Intermediate':
|
1875 |
+
return f"Something interesting happened with the {word}."
|
1876 |
+
else: # Advanced
|
1877 |
+
return f"The mystery deepens as we discover more about the {word}."
|
1878 |
+
|
1879 |
+
# Default fallback if no theme-specific response can be generated
|
1880 |
+
return "The story continues..."
|
1881 |
+
|
1882 |
+
except Exception as e:
|
1883 |
+
logging.error(f"Error generating fallback response: {str(e)}")
|
1884 |
+
return "What happens next?"
|
1885 |
+
|
1886 |
def show_main_interface():
|
1887 |
"""Display main story interface"""
|
1888 |
col1, col2 = st.columns([3, 1])
|