Update app.py
Browse files
app.py
CHANGED
@@ -449,6 +449,7 @@ def clear_input():
|
|
449 |
|
450 |
# Callback function for submit button
|
451 |
def submit_story():
|
|
|
452 |
if st.session_state.text_input.strip():
|
453 |
user_text = st.session_state.text_input.strip()
|
454 |
|
@@ -457,99 +458,45 @@ def submit_story():
|
|
457 |
st.error("กรุณาเลือกธีมเรื่องราวก่อนเริ่มเขียน")
|
458 |
return
|
459 |
|
460 |
-
# เพิ่มประโยคของผู้ใช้
|
461 |
-
st.session_state.story.append({
|
462 |
-
"role": "You",
|
463 |
-
"content": user_text,
|
464 |
-
"is_corrected": False
|
465 |
-
})
|
466 |
-
|
467 |
-
# เพิ่มคำศัพท์ที่ใช้
|
468 |
-
words = set(user_text.lower().split())
|
469 |
-
st.session_state.stats['vocabulary_used'].update(words)
|
470 |
-
|
471 |
-
# ตรวจสอบความถูกต้อง
|
472 |
-
feedback_data = provide_feedback(user_text, st.session_state.level)
|
473 |
-
is_correct = not feedback_data.get('has_errors', False)
|
474 |
-
|
475 |
-
# อัพเดตคะแนน
|
476 |
-
update_points(is_correct)
|
477 |
-
|
478 |
-
# อัพเดต achievements
|
479 |
-
update_achievements()
|
480 |
-
|
481 |
-
# เพิ่มประโยคของผู้ใช้
|
482 |
-
story_index = len(st.session_state.story)
|
483 |
-
st.session_state.story.append({
|
484 |
-
"role": "You",
|
485 |
-
"content": user_text,
|
486 |
-
"is_corrected": False
|
487 |
-
})
|
488 |
-
|
489 |
try:
|
490 |
-
# รับ feedback
|
491 |
feedback_data = provide_feedback(user_text, st.session_state.level)
|
492 |
st.session_state.feedback = feedback_data
|
|
|
493 |
|
494 |
-
#
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
""", unsafe_allow_html=True)
|
502 |
|
503 |
-
#
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
509 |
|
510 |
except Exception as e:
|
511 |
st.error("เกิดข้อผิดพลาดในการวิเคราะห์ประโยค กรุณาลองใหม่อีกครั้ง")
|
512 |
-
# Log error for debugging
|
513 |
st.error(f"Debug - Error in submit_story: {str(e)}")
|
514 |
-
|
515 |
-
# Clear input
|
516 |
-
st.session_state.text_input = ""
|
517 |
|
518 |
-
('is_starter'):
|
519 |
-
# แสดงประโยคเริ่มต้นพิเศษ
|
520 |
-
st.markdown(f"""
|
521 |
-
<div style="background-color: #f0f7ff;
|
522 |
-
padding: 15px;
|
523 |
-
border-radius: 8px;
|
524 |
-
margin: 5px 0;
|
525 |
-
border-left: 4px solid #1e88e5;">
|
526 |
-
<p style="color: #1e88e5; margin-bottom: 5px;">🎬 เริ่มเ���ื่อง:</p>
|
527 |
-
<p style="color: #666;">{entry.get('thai_content', '')}</p>
|
528 |
-
<p>{entry['content']}</p>
|
529 |
-
</div>
|
530 |
-
""", unsafe_allow_html=True)
|
531 |
-
else:
|
532 |
-
st.markdown(f"""
|
533 |
-
<div style="padding: 10px; margin: 5px 0;">
|
534 |
-
<p>🤖 AI: {entry['content']}</p>
|
535 |
-
</div>
|
536 |
-
""", unsafe_allow_html=True)
|
537 |
-
elif entry['role'] == 'You':
|
538 |
-
status_icon = "✅ " if entry.get('is_correct') else "✍️ "
|
539 |
-
if entry.get('is_corrected'):
|
540 |
-
# แสดงข้อความที่ถูกแก้ไขแล้ว
|
541 |
-
st.markdown(f"""
|
542 |
-
<div style="padding: 10px; margin: 5px 0; background-color: #f0f9ff; border-radius: 5px;">
|
543 |
-
<p>👤 You: {status_icon}{entry['content']}</p>
|
544 |
-
</div>
|
545 |
-
""", unsafe_allow_html=True)
|
546 |
-
else:
|
547 |
-
# แสดงข้อความปกติ
|
548 |
-
st.markdown(f"""
|
549 |
-
<div style="padding: 10px; margin: 5px 0;">
|
550 |
-
<p>👤 You: {status_icon}{entry['content']}</p>
|
551 |
-
</div>
|
552 |
-
""", unsafe_allow_html=True)
|
553 |
|
554 |
# Theme Helper Functions
|
555 |
def get_available_themes(level: str) -> List[Dict]:
|
@@ -1580,23 +1527,15 @@ def update_points(is_correct_first_try: bool):
|
|
1580 |
)
|
1581 |
|
1582 |
def show_story():
|
|
|
1583 |
story_display = st.container()
|
1584 |
|
1585 |
with story_display:
|
1586 |
if not st.session_state.story:
|
1587 |
st.info("เลือกธีมเรื่องราวที่ต้องการเพื่อเริ่มต้นการผจญภัย!")
|
1588 |
else:
|
1589 |
-
#
|
1590 |
-
|
1591 |
-
for idx, entry in enumerate(st.session_state.story):
|
1592 |
-
if entry['role'] == 'You':
|
1593 |
-
latest_messages[idx] = entry
|
1594 |
-
else:
|
1595 |
-
# สำหรับข้อความของ AI ให้แสดงทุกข้อความ
|
1596 |
-
latest_messages[idx] = entry
|
1597 |
-
|
1598 |
-
# แสดงข้อความที่ไม่ซ้ำ
|
1599 |
-
for idx, entry in latest_messages.items():
|
1600 |
if entry['role'] == 'AI':
|
1601 |
if entry.get('is_starter'):
|
1602 |
# แสดงประโยคเริ่มต้นพิเศษ
|
@@ -1612,27 +1551,45 @@ def show_story():
|
|
1612 |
</div>
|
1613 |
""", unsafe_allow_html=True)
|
1614 |
else:
|
|
|
1615 |
st.markdown(f"""
|
1616 |
<div style="padding: 10px; margin: 5px 0;">
|
1617 |
<p>🤖 AI: {entry['content']}</p>
|
1618 |
</div>
|
1619 |
""", unsafe_allow_html=True)
|
1620 |
elif entry['role'] == 'You':
|
|
|
1621 |
status_icon = "✅ " if entry.get('is_correct') else "✍️ "
|
1622 |
-
if entry.get('is_corrected')
|
1623 |
-
|
1624 |
-
|
1625 |
-
|
1626 |
-
|
1627 |
-
|
1628 |
-
|
1629 |
-
|
1630 |
-
|
1631 |
-
|
1632 |
-
|
1633 |
-
|
1634 |
-
|
1635 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1636 |
|
1637 |
# แสดงผลคะแนนและความสำเร็จ
|
1638 |
def show_achievements():
|
|
|
449 |
|
450 |
# Callback function for submit button
|
451 |
def submit_story():
|
452 |
+
"""Handle story submission and processing."""
|
453 |
if st.session_state.text_input.strip():
|
454 |
user_text = st.session_state.text_input.strip()
|
455 |
|
|
|
458 |
st.error("กรุณาเลือกธีมเรื่องราวก่อนเริ่มเขียน")
|
459 |
return
|
460 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
461 |
try:
|
462 |
+
# รับ feedback และตรวจสอบความถูกต้อง
|
463 |
feedback_data = provide_feedback(user_text, st.session_state.level)
|
464 |
st.session_state.feedback = feedback_data
|
465 |
+
is_correct = not feedback_data.get('has_errors', False)
|
466 |
|
467 |
+
# เพิ่มประโยคของผู้ใช้ (เพิ่มครั้งเดียว)
|
468 |
+
st.session_state.story.append({
|
469 |
+
"role": "You",
|
470 |
+
"content": user_text,
|
471 |
+
"is_corrected": False,
|
472 |
+
"is_correct": is_correct
|
473 |
+
})
|
|
|
474 |
|
475 |
+
# เพิ่มคำศัพท์ที่ใช้
|
476 |
+
words = set(user_text.lower().split())
|
477 |
+
st.session_state.stats['vocabulary_used'].update(words)
|
478 |
+
|
479 |
+
# อัพเดตคะแนนและ achievements
|
480 |
+
update_points(is_correct)
|
481 |
+
update_achievements()
|
482 |
+
|
483 |
+
# Generate AI continuation
|
484 |
+
text_for_continuation = feedback_data['corrected'] if feedback_data['has_errors'] else user_text
|
485 |
+
ai_response = generate_story_continuation(text_for_continuation, st.session_state.level)
|
486 |
+
|
487 |
+
# เพิ่มการตอบกลับของ AI
|
488 |
+
st.session_state.story.append({
|
489 |
+
"role": "AI",
|
490 |
+
"content": ai_response
|
491 |
+
})
|
492 |
+
|
493 |
+
# Clear input
|
494 |
+
st.session_state.text_input = ""
|
495 |
|
496 |
except Exception as e:
|
497 |
st.error("เกิดข้อผิดพลาดในการวิเคราะห์ประโยค กรุณาลองใหม่อีกครั้ง")
|
|
|
498 |
st.error(f"Debug - Error in submit_story: {str(e)}")
|
|
|
|
|
|
|
499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
500 |
|
501 |
# Theme Helper Functions
|
502 |
def get_available_themes(level: str) -> List[Dict]:
|
|
|
1527 |
)
|
1528 |
|
1529 |
def show_story():
|
1530 |
+
"""Display the story with proper formatting."""
|
1531 |
story_display = st.container()
|
1532 |
|
1533 |
with story_display:
|
1534 |
if not st.session_state.story:
|
1535 |
st.info("เลือกธีมเรื่องราวที่ต้องการเพื่อเริ่มต้นการผจญภัย!")
|
1536 |
else:
|
1537 |
+
# แสดงข้อความทั้งหมดตามลำดับ
|
1538 |
+
for entry in st.session_state.story:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1539 |
if entry['role'] == 'AI':
|
1540 |
if entry.get('is_starter'):
|
1541 |
# แสดงประโยคเริ่มต้นพิเศษ
|
|
|
1551 |
</div>
|
1552 |
""", unsafe_allow_html=True)
|
1553 |
else:
|
1554 |
+
# แสดงข้อความ AI ปกติ
|
1555 |
st.markdown(f"""
|
1556 |
<div style="padding: 10px; margin: 5px 0;">
|
1557 |
<p>🤖 AI: {entry['content']}</p>
|
1558 |
</div>
|
1559 |
""", unsafe_allow_html=True)
|
1560 |
elif entry['role'] == 'You':
|
1561 |
+
# กำหนดสถานะและสไตล์การแสดงผล
|
1562 |
status_icon = "✅ " if entry.get('is_correct') else "✍️ "
|
1563 |
+
bg_color = "#f0f9ff" if entry.get('is_corrected') else "white"
|
1564 |
+
|
1565 |
+
st.markdown(f"""
|
1566 |
+
<div style="padding: 10px;
|
1567 |
+
margin: 5px 0;
|
1568 |
+
background-color: {bg_color};
|
1569 |
+
border-radius: 5px;">
|
1570 |
+
<p>👤 You: {status_icon}{entry['content']}</p>
|
1571 |
+
</div>
|
1572 |
+
""", unsafe_allow_html=True)
|
1573 |
+
|
1574 |
+
# Add CSS for consistent styling
|
1575 |
+
st.markdown("""
|
1576 |
+
<style>
|
1577 |
+
.story-message {
|
1578 |
+
padding: 10px;
|
1579 |
+
margin: 5px 0;
|
1580 |
+
border-radius: 5px;
|
1581 |
+
}
|
1582 |
+
.story-message.ai {
|
1583 |
+
background-color: #f0f7ff;
|
1584 |
+
}
|
1585 |
+
.story-message.user {
|
1586 |
+
background-color: #fff;
|
1587 |
+
}
|
1588 |
+
.story-message.corrected {
|
1589 |
+
background-color: #f0f9ff;
|
1590 |
+
}
|
1591 |
+
</style>
|
1592 |
+
""", unsafe_allow_html=True)
|
1593 |
|
1594 |
# แสดงผลคะแนนและความสำเร็จ
|
1595 |
def show_achievements():
|