Update app.py
Browse files
app.py
CHANGED
@@ -126,16 +126,31 @@ def submit_story():
|
|
126 |
"is_corrected": False
|
127 |
})
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
# Clear input
|
141 |
st.session_state.text_input = ""
|
@@ -356,65 +371,77 @@ def get_creative_prompt() -> Dict[str, str]:
|
|
356 |
"thai": "แล้วอะไรจะเกิดขึ้นต่อ?"
|
357 |
}
|
358 |
|
359 |
-
def provide_feedback(text: str, level: str) -> str:
|
360 |
-
"""Provide
|
361 |
try:
|
362 |
response = client.chat.completions.create(
|
363 |
model="gpt-4o-mini",
|
364 |
messages=[
|
365 |
{"role": "system", "content": f"""You are a Thai English teacher helping {level} students.
|
366 |
-
|
367 |
-
1. Points out ONE main grammar or vocabulary point
|
368 |
-
2. Explains the correct usage in simple Thai
|
369 |
-
3. Shows example sentences
|
370 |
-
4. Gives encouragement
|
371 |
-
|
372 |
-
Format example:
|
373 |
-
"🎯 คำว่า 'go' เมื่อพูดถึงเหตุการณ์ในอดีต ต้องเปลี่ยนเป็น 'went' นะคะ
|
374 |
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
],
|
383 |
max_tokens=200,
|
384 |
-
temperature=0.
|
385 |
)
|
386 |
-
|
387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
except Exception as e:
|
389 |
-
st.error(f"Error
|
|
|
|
|
|
|
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:
|
398 |
-
st.markdown("""
|
399 |
-
<div class="thai-eng">
|
400 |
-
<div class="thai">📝 คำแนะนำจากคุณครู</div>
|
401 |
-
<div class="eng">Teacher's Feedback</div>
|
402 |
-
</div>
|
403 |
-
""", unsafe_allow_html=True)
|
404 |
-
|
405 |
-
feedback_container = st.container()
|
406 |
-
with feedback_container:
|
407 |
-
st.markdown(f"""
|
408 |
-
<div style='background-color: #f0f2f6;
|
409 |
-
padding: 10px;
|
410 |
-
border-radius: 8px;
|
411 |
-
border-left: 4px solid #4CAF50;
|
412 |
-
margin: 5px 0;
|
413 |
-
font-family: "Sarabun", sans-serif;'>
|
414 |
-
{st.session_state.feedback}
|
415 |
-
</div>
|
416 |
-
""", unsafe_allow_html=True)
|
417 |
-
|
418 |
def update_achievements(text: str):
|
419 |
"""Update user achievements based on their writing."""
|
420 |
words = set(text.lower().split())
|
|
|
126 |
"is_corrected": False
|
127 |
})
|
128 |
|
129 |
+
try:
|
130 |
+
# รับ feedback และประโยคที่ถูกต้อง
|
131 |
+
feedback_data = provide_feedback(user_text, st.session_state.level)
|
132 |
+
st.session_state.feedback = feedback_data
|
133 |
+
|
134 |
+
# ถ้ามีข้อผิดพลาด แสดงคำแนะนำ
|
135 |
+
if feedback_data['has_errors']:
|
136 |
+
st.markdown(f"""
|
137 |
+
<div style='background-color: #fff3e0; padding: 10px; border-radius: 5px; margin: 10px 0;'>
|
138 |
+
<p style='color: #ff6d00;'>🎯 คำแนะนำ:</p>
|
139 |
+
<p>{feedback_data['feedback']}</p>
|
140 |
+
</div>
|
141 |
+
""", unsafe_allow_html=True)
|
142 |
+
|
143 |
+
# Generate AI continuation using corrected text if there were errors
|
144 |
+
ai_response = generate_story_continuation(
|
145 |
+
feedback_data['corrected'] if feedback_data['has_errors'] else user_text,
|
146 |
+
st.session_state.level
|
147 |
+
)
|
148 |
+
st.session_state.story.append({"role": "AI", "content": ai_response})
|
149 |
+
|
150 |
+
except Exception as e:
|
151 |
+
st.error("เกิดข้อผิดพลาดในการวิเคราะห์ประโยค กรุณาลองใหม่อีกครั้ง")
|
152 |
+
# Log error for debugging
|
153 |
+
st.error(f"Debug - Error in submit_story: {str(e)}")
|
154 |
|
155 |
# Clear input
|
156 |
st.session_state.text_input = ""
|
|
|
371 |
"thai": "แล้วอะไรจะเกิดขึ้นต่อ?"
|
372 |
}
|
373 |
|
374 |
+
def provide_feedback(text: str, level: str) -> Dict[str, str]:
|
375 |
+
"""Provide feedback and corrected sentence."""
|
376 |
try:
|
377 |
response = client.chat.completions.create(
|
378 |
model="gpt-4o-mini",
|
379 |
messages=[
|
380 |
{"role": "system", "content": f"""You are a Thai English teacher helping {level} students.
|
381 |
+
Your task is to review the student's sentence and provide feedback.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
|
383 |
+
Return your response in this EXACT format only (must be valid JSON):
|
384 |
+
{{
|
385 |
+
"feedback": "(ข้อเสนอแนะภาษาไทย)",
|
386 |
+
"corrected": "(ประโยคภาษาอังกฤษที่ถูกต้อง)",
|
387 |
+
"has_errors": true/false
|
388 |
+
}}
|
389 |
+
|
390 |
+
Example 1 - with error:
|
391 |
+
{{
|
392 |
+
"feedback": "คำว่า 'go' เมื่อพูดถึงเหตุการณ์ในอดีต ต้องเปลี่ยนเป็น 'went'",
|
393 |
+
"corrected": "I went to school yesterday.",
|
394 |
+
"has_errors": true
|
395 |
+
}}
|
396 |
+
|
397 |
+
Example 2 - no error:
|
398 |
+
{{
|
399 |
+
"feedback": "เขียนได้ถูกต้องแล้วค่ะ ประโยคสื่อความหมายได้ดี",
|
400 |
+
"corrected": "The cat is sleeping.",
|
401 |
+
"has_errors": false
|
402 |
+
}}"""},
|
403 |
+
{"role": "user", "content": f"Review this sentence and provide feedback in the specified JSON format: {text}"}
|
404 |
],
|
405 |
max_tokens=200,
|
406 |
+
temperature=0.3
|
407 |
)
|
408 |
+
|
409 |
+
# Get the response text
|
410 |
+
response_text = response.choices[0].message.content.strip()
|
411 |
+
|
412 |
+
try:
|
413 |
+
# Try to parse the JSON response
|
414 |
+
feedback_data = json.loads(response_text)
|
415 |
+
|
416 |
+
# Validate the required fields
|
417 |
+
required_fields = ['feedback', 'corrected', 'has_errors']
|
418 |
+
if not all(field in feedback_data for field in required_fields):
|
419 |
+
raise ValueError("Missing required fields in response")
|
420 |
+
|
421 |
+
return feedback_data
|
422 |
+
|
423 |
+
except json.JSONDecodeError as json_err:
|
424 |
+
# If JSON parsing fails, try to extract information manually
|
425 |
+
st.error(f"Debug - Response received: {response_text}")
|
426 |
+
|
427 |
+
# Return a safe fallback response
|
428 |
+
return {
|
429 |
+
"feedback": "⚠️ คำแนะนำ: ระบบไม่สามารถวิเคราะห์ประโยคได้อย่างถูกต้อง กรุณาลองใหม่อีกครั้ง",
|
430 |
+
"corrected": text,
|
431 |
+
"has_errors": False
|
432 |
+
}
|
433 |
+
|
434 |
except Exception as e:
|
435 |
+
st.error(f"Debug - Error type: {type(e).__name__}")
|
436 |
+
st.error(f"Debug - Error message: {str(e)}")
|
437 |
+
|
438 |
+
# Return a safe fallback response
|
439 |
return {
|
440 |
+
"feedback": "⚠️ ขออภัย ระบบไม่สามารถวิเคราะห์ประโยคได้ในขณะนี้",
|
441 |
"corrected": text,
|
442 |
"has_errors": False
|
443 |
}
|
444 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
445 |
def update_achievements(text: str):
|
446 |
"""Update user achievements based on their writing."""
|
447 |
words = set(text.lower().split())
|