Update app.py
Browse files
app.py
CHANGED
@@ -94,6 +94,8 @@ def init_session_state():
|
|
94 |
st.session_state.story = []
|
95 |
if 'feedback' not in st.session_state:
|
96 |
st.session_state.feedback = None
|
|
|
|
|
97 |
if 'level' not in st.session_state:
|
98 |
st.session_state.level = 'Beginner'
|
99 |
if 'unique_words' not in st.session_state:
|
@@ -114,20 +116,28 @@ def clear_input():
|
|
114 |
# Callback function for submit button
|
115 |
def submit_story():
|
116 |
if st.session_state.text_input.strip():
|
117 |
-
|
118 |
-
st.session_state.story.append({"role": "You", "content": st.session_state.text_input})
|
119 |
|
120 |
-
#
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
-
#
|
124 |
-
|
|
|
125 |
|
126 |
# Generate AI continuation
|
127 |
-
ai_response = generate_story_continuation(
|
|
|
|
|
|
|
128 |
st.session_state.story.append({"role": "AI", "content": ai_response})
|
129 |
|
130 |
-
# Clear
|
131 |
st.session_state.text_input = ""
|
132 |
|
133 |
def show_welcome_section():
|
@@ -250,6 +260,21 @@ def generate_story_continuation(user_input: str, level: str) -> str:
|
|
250 |
st.error(f"Error generating story continuation: {str(e)}")
|
251 |
return "I'm having trouble continuing the story. Please try again."
|
252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
def get_vocabulary_suggestions() -> List[str]:
|
254 |
"""Get contextual vocabulary suggestions with Thai translations."""
|
255 |
try:
|
@@ -358,10 +383,15 @@ def provide_feedback(text: str, level: str) -> str:
|
|
358 |
max_tokens=200,
|
359 |
temperature=0.7
|
360 |
)
|
361 |
-
|
|
|
362 |
except Exception as e:
|
363 |
st.error(f"Error generating feedback: {str(e)}")
|
364 |
-
return
|
|
|
|
|
|
|
|
|
365 |
|
366 |
# Update the feedback display in the UI
|
367 |
if st.session_state.feedback:
|
@@ -462,13 +492,27 @@ with col1:
|
|
462 |
""", unsafe_allow_html=True)
|
463 |
|
464 |
story_display = st.container()
|
|
|
465 |
with story_display:
|
466 |
if not st.session_state.story:
|
467 |
st.info("เริ่มต้นผจญภัยด้วยการเขียนประโยคแรกกันเลย! | Start your adventure by writing the first sentence!")
|
468 |
else:
|
469 |
-
for entry in st.session_state.story:
|
470 |
if entry['role'] == 'You':
|
471 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
472 |
elif entry['role'] == 'AI':
|
473 |
st.write("🤖 AI:", entry['content'])
|
474 |
|
|
|
94 |
st.session_state.story = []
|
95 |
if 'feedback' not in st.session_state:
|
96 |
st.session_state.feedback = None
|
97 |
+
if 'corrections' not in st.session_state:
|
98 |
+
st.session_state.corrections = {} # เก็บประวัติการแก้ไข
|
99 |
if 'level' not in st.session_state:
|
100 |
st.session_state.level = 'Beginner'
|
101 |
if 'unique_words' not in st.session_state:
|
|
|
116 |
# Callback function for submit button
|
117 |
def submit_story():
|
118 |
if st.session_state.text_input.strip():
|
119 |
+
user_text = st.session_state.text_input
|
|
|
120 |
|
121 |
+
# เพิ่มประโยคของผู้ใช้
|
122 |
+
story_index = len(st.session_state.story)
|
123 |
+
st.session_state.story.append({
|
124 |
+
"role": "You",
|
125 |
+
"content": user_text,
|
126 |
+
"is_corrected": False
|
127 |
+
})
|
128 |
|
129 |
+
# รับ feedback และประโยคที่ถูกต้อง
|
130 |
+
feedback_data = provide_feedback(user_text, st.session_state.level)
|
131 |
+
st.session_state.feedback = feedback_data
|
132 |
|
133 |
# Generate AI continuation
|
134 |
+
ai_response = generate_story_continuation(
|
135 |
+
feedback_data['corrected'] if feedback_data['has_errors'] else user_text,
|
136 |
+
st.session_state.level
|
137 |
+
)
|
138 |
st.session_state.story.append({"role": "AI", "content": ai_response})
|
139 |
|
140 |
+
# Clear input
|
141 |
st.session_state.text_input = ""
|
142 |
|
143 |
def show_welcome_section():
|
|
|
260 |
st.error(f"Error generating story continuation: {str(e)}")
|
261 |
return "I'm having trouble continuing the story. Please try again."
|
262 |
|
263 |
+
# ฟังก์ชันสำหรับแก้ไขประโยค
|
264 |
+
def apply_correction(story_index: int, corrected_text: str):
|
265 |
+
"""Apply correction to a specific story entry."""
|
266 |
+
if 0 <= story_index < len(st.session_state.story):
|
267 |
+
original_text = st.session_state.story[story_index]['content']
|
268 |
+
# เก็บประวัติการแก้ไข
|
269 |
+
st.session_state.corrections[story_index] = {
|
270 |
+
'original': original_text,
|
271 |
+
'corrected': corrected_text,
|
272 |
+
'timestamp': datetime.datetime.now().isoformat()
|
273 |
+
}
|
274 |
+
# แก้ไขประโยคในเรื่อง
|
275 |
+
st.session_state.story[story_index]['content'] = corrected_text
|
276 |
+
st.session_state.story[story_index]['is_corrected'] = True
|
277 |
+
|
278 |
def get_vocabulary_suggestions() -> List[str]:
|
279 |
"""Get contextual vocabulary suggestions with Thai translations."""
|
280 |
try:
|
|
|
383 |
max_tokens=200,
|
384 |
temperature=0.7
|
385 |
)
|
386 |
+
feedback_data = json.loads(response.choices[0].message.content)
|
387 |
+
return feedback_data
|
388 |
except Exception as e:
|
389 |
st.error(f"Error generating feedback: {str(e)}")
|
390 |
+
return {
|
391 |
+
"feedback": "ขออภัย ไม่สามารถวิเคราะห์ประโยคได้",
|
392 |
+
"corrected": text,
|
393 |
+
"has_errors": False
|
394 |
+
}
|
395 |
|
396 |
# Update the feedback display in the UI
|
397 |
if st.session_state.feedback:
|
|
|
492 |
""", unsafe_allow_html=True)
|
493 |
|
494 |
story_display = st.container()
|
495 |
+
|
496 |
with story_display:
|
497 |
if not st.session_state.story:
|
498 |
st.info("เริ่มต้นผจญภัยด้วยการเขียนประโยคแรกกันเลย! | Start your adventure by writing the first sentence!")
|
499 |
else:
|
500 |
+
for idx, entry in enumerate(st.session_state.story):
|
501 |
if entry['role'] == 'You':
|
502 |
+
col_text, col_button = st.columns([4, 1])
|
503 |
+
with col_text:
|
504 |
+
prefix = "👤 You (แก้ไขแล้ว):" if entry.get('is_corrected') else "👤 You:"
|
505 |
+
st.write(f"{prefix} {entry['content']}")
|
506 |
+
|
507 |
+
# ถ้ามี feedback และมีข้อผิด แสดงปุ่��แก้ไข
|
508 |
+
if (idx in st.session_state.corrections and
|
509 |
+
not entry.get('is_corrected') and
|
510 |
+
st.session_state.feedback and
|
511 |
+
st.session_state.feedback.get('has_errors')):
|
512 |
+
with col_button:
|
513 |
+
if st.button(f"✍️ แก้ไขประโยค", key=f"correct_{idx}"):
|
514 |
+
apply_correction(idx, st.session_state.feedback['corrected'])
|
515 |
+
st.rerun()
|
516 |
elif entry['role'] == 'AI':
|
517 |
st.write("🤖 AI:", entry['content'])
|
518 |
|