Update app.py
Browse files
app.py
CHANGED
@@ -801,6 +801,117 @@ def generate_dynamic_story_starter(theme_id: str, level: str) -> Dict[str, str]:
|
|
801 |
'th': 'กาลครั้งหนึ่ง...'
|
802 |
}
|
803 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
804 |
def provide_feedback(text: str, level: str) -> Dict[str, str]:
|
805 |
"""Provide feedback on the user's writing with appropriate level"""
|
806 |
|
|
|
801 |
'th': 'กาลครั้งหนึ่ง...'
|
802 |
}
|
803 |
|
804 |
+
def update_points(is_correct_first_try: bool):
|
805 |
+
"""อัพเดตคะแนนตามผลการเขียน"""
|
806 |
+
try:
|
807 |
+
# คำนวณคะแนนพื้นฐาน
|
808 |
+
base_points = 10
|
809 |
+
|
810 |
+
if is_correct_first_try:
|
811 |
+
# ถูกต้องในครั้งแรก
|
812 |
+
points = base_points * 2
|
813 |
+
st.session_state.points['perfect_sentences'] += 1
|
814 |
+
st.session_state.points['streak'] += 1
|
815 |
+
|
816 |
+
# อัพเดต max streak
|
817 |
+
if st.session_state.points['streak'] > st.session_state.points['max_streak']:
|
818 |
+
st.session_state.points['max_streak'] = st.session_state.points['streak']
|
819 |
+
else:
|
820 |
+
# ต้องแก้ไข
|
821 |
+
points = base_points // 2
|
822 |
+
st.session_state.points['corrections_made'] += 1
|
823 |
+
st.session_state.points['streak'] = 0
|
824 |
+
|
825 |
+
# เพิ่มคะแนนรวม
|
826 |
+
st.session_state.points['total'] += points
|
827 |
+
|
828 |
+
# อัพเดตสถิติ
|
829 |
+
st.session_state.stats['total_sentences'] += 1
|
830 |
+
if is_correct_first_try:
|
831 |
+
st.session_state.stats['correct_first_try'] += 1
|
832 |
+
|
833 |
+
# คำนวณอัตราความถูกต้อง
|
834 |
+
st.session_state.stats['accuracy_rate'] = (
|
835 |
+
st.session_state.stats['correct_first_try'] /
|
836 |
+
st.session_state.stats['total_sentences'] * 100
|
837 |
+
)
|
838 |
+
|
839 |
+
logging.info(f"Points updated: +{points} points")
|
840 |
+
return True
|
841 |
+
|
842 |
+
except Exception as e:
|
843 |
+
logging.error(f"Error updating points: {str(e)}")
|
844 |
+
return False
|
845 |
+
|
846 |
+
def apply_correction(story_index: int, corrected_text: str):
|
847 |
+
"""แก้ไขประโยคในเรื่อง"""
|
848 |
+
try:
|
849 |
+
if not (0 <= story_index < len(st.session_state.story)):
|
850 |
+
raise ValueError("Invalid story index")
|
851 |
+
|
852 |
+
# เก็บข้อความเดิม
|
853 |
+
original_text = st.session_state.story[story_index]['content']
|
854 |
+
|
855 |
+
# อัพเดทข้อความ
|
856 |
+
st.session_state.story[story_index].update({
|
857 |
+
'content': corrected_text,
|
858 |
+
'is_corrected': True,
|
859 |
+
'is_correct': True,
|
860 |
+
'original_text': original_text,
|
861 |
+
'correction_timestamp': datetime.now().isoformat()
|
862 |
+
})
|
863 |
+
|
864 |
+
# อัพเดทสถิติ
|
865 |
+
st.session_state.stats['corrections_made'] += 1
|
866 |
+
|
867 |
+
# Log การแก้ไข
|
868 |
+
logging.info(f"Sentence corrected at index {story_index}")
|
869 |
+
|
870 |
+
# แสดงข้อความยืนยัน
|
871 |
+
st.success("✅ แก้ไขประโยคเรียบร้อยแล้ว!")
|
872 |
+
return True
|
873 |
+
|
874 |
+
except Exception as e:
|
875 |
+
logging.error(f"Error applying correction: {str(e)}")
|
876 |
+
st.error("เกิดข้อผิดพลาดในการแก้ไขประโยค กรุณาลองใหม่อีกครั้ง")
|
877 |
+
return False
|
878 |
+
|
879 |
+
def update_achievements():
|
880 |
+
"""ตรวจสอบและอัพเดตความสำเร็จ"""
|
881 |
+
try:
|
882 |
+
current_achievements = st.session_state.achievements
|
883 |
+
|
884 |
+
# ตรวจสอบเงื่อนไขต่างๆ
|
885 |
+
if (st.session_state.points['streak'] >= 5 and
|
886 |
+
"🌟 นักเขียนไร้ที่ติ" not in current_achievements):
|
887 |
+
current_achievements.append("🌟 นักเขียนไร้ที่ติ")
|
888 |
+
st.success("🎉 ได้รับความสำเร็จใหม่: นักเขียนไร้ที่ติ!")
|
889 |
+
|
890 |
+
if (len(st.session_state.stats['vocabulary_used']) >= 50 and
|
891 |
+
"📚 ราชาคำศัพท์" not in current_achievements):
|
892 |
+
current_achievements.append("📚 ราชาคำศัพท์")
|
893 |
+
st.success("🎉 ได้รับความสำเร็จใหม่: ราชาคำศัพท์!")
|
894 |
+
|
895 |
+
if (len(st.session_state.story) >= 10 and
|
896 |
+
"📖 นักแต่งนิทาน" not in current_achievements):
|
897 |
+
current_achievements.append("📖 นักแต่งนิทาน")
|
898 |
+
st.success("🎉 ได้รับความสำเร็จใหม่: นักแต่งนิทาน!")
|
899 |
+
|
900 |
+
if (st.session_state.stats['total_sentences'] >= 10 and
|
901 |
+
st.session_state.stats['accuracy_rate'] >= 80 and
|
902 |
+
"👑 ราชาความแม่นยำ" not in current_achievements):
|
903 |
+
current_achievements.append("👑 ราชาความแ��่นยำ")
|
904 |
+
st.success("🎉 ได้รับความสำเร็จใหม่: ราชาความแม่นยำ!")
|
905 |
+
|
906 |
+
# บันทึกความสำเร็จ
|
907 |
+
st.session_state.achievements = current_achievements
|
908 |
+
logging.info("Achievements updated successfully")
|
909 |
+
return True
|
910 |
+
|
911 |
+
except Exception as e:
|
912 |
+
logging.error(f"Error updating achievements: {str(e)}")
|
913 |
+
return False
|
914 |
+
|
915 |
def provide_feedback(text: str, level: str) -> Dict[str, str]:
|
916 |
"""Provide feedback on the user's writing with appropriate level"""
|
917 |
|