Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,38 @@ import datetime
|
|
4 |
from openai import OpenAI
|
5 |
from typing import Dict, List, Set
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Set up Streamlit page configuration
|
8 |
st.set_page_config(
|
9 |
page_title="JoyStory - Interactive Story Adventure",
|
@@ -98,6 +130,21 @@ def init_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:
|
102 |
st.session_state.unique_words = set()
|
103 |
if 'total_words' not in st.session_state:
|
@@ -117,6 +164,17 @@ def clear_input():
|
|
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)
|
@@ -440,6 +498,60 @@ def provide_feedback(text: str, level: str) -> Dict[str, str]:
|
|
440 |
"has_errors": False
|
441 |
}
|
442 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
443 |
def update_achievements(text: str):
|
444 |
"""Update user achievements based on their writing."""
|
445 |
words = set(text.lower().split())
|
@@ -524,9 +636,9 @@ with col1:
|
|
524 |
else:
|
525 |
for idx, entry in enumerate(st.session_state.story):
|
526 |
if entry['role'] == 'You':
|
527 |
-
#
|
528 |
-
|
529 |
-
st.write(f"👤 You: {
|
530 |
elif entry['role'] == 'AI':
|
531 |
st.write("🤖 AI:", entry['content'])
|
532 |
|
@@ -631,6 +743,9 @@ with col2:
|
|
631 |
<div class="eng">💭 {prompt['eng']}</div>
|
632 |
</div>
|
633 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
634 |
|
635 |
# Achievements Box
|
636 |
st.markdown("""
|
|
|
4 |
from openai import OpenAI
|
5 |
from typing import Dict, List, Set
|
6 |
|
7 |
+
# ระบบ Achievements
|
8 |
+
achievements_list = {
|
9 |
+
'perfect_writer': {
|
10 |
+
'name': '🌟 นักเขียนไร้ที่ติ',
|
11 |
+
'description': 'เขียนถูกต้อง 5 ประโยคติดต่อกัน',
|
12 |
+
'condition': lambda: st.session_state.points['streak'] >= 5
|
13 |
+
},
|
14 |
+
'vocabulary_master': {
|
15 |
+
'name': '📚 ราชาคำศัพท์',
|
16 |
+
'description': 'ใช้คำศัพท์ไม่ซ้ำกัน 50 คำ',
|
17 |
+
'condition': lambda: len(st.session_state.stats['vocabulary_used']) >= 50
|
18 |
+
},
|
19 |
+
'quick_learner': {
|
20 |
+
'name': '🚀 นักเรียนจอมขยัน',
|
21 |
+
'description': 'แก้ไขประโยคให้ถูกต้องภายใน 3 วินาที',
|
22 |
+
'condition': lambda: True # ต้องเพิ่มการจับเวลา
|
23 |
+
},
|
24 |
+
'story_master': {
|
25 |
+
'name': '📖 นักแต่งนิทาน',
|
26 |
+
'description': 'เขียนเรื่องยาว 10 ประโยค',
|
27 |
+
'condition': lambda: len(st.session_state.story) >= 10
|
28 |
+
},
|
29 |
+
'accuracy_king': {
|
30 |
+
'name': '👑 ราชาความแม่นยำ',
|
31 |
+
'description': 'มีอัตราความถูกต้อง 80% ขึ้นไป (อย่างน้อย 10 ประโยค)',
|
32 |
+
'condition': lambda: (
|
33 |
+
st.session_state.stats['total_sentences'] >= 10 and
|
34 |
+
st.session_state.stats['accuracy_rate'] >= 80
|
35 |
+
)
|
36 |
+
}
|
37 |
+
}
|
38 |
+
|
39 |
# Set up Streamlit page configuration
|
40 |
st.set_page_config(
|
41 |
page_title="JoyStory - Interactive Story Adventure",
|
|
|
130 |
st.session_state.corrections = {} # เก็บประวัติการแก้ไข
|
131 |
if 'level' not in st.session_state:
|
132 |
st.session_state.level = 'Beginner'
|
133 |
+
if 'points' not in st.session_state:
|
134 |
+
st.session_state.points = {
|
135 |
+
'total': 0,
|
136 |
+
'perfect_sentences': 0,
|
137 |
+
'corrections_made': 0,
|
138 |
+
'streak': 0,
|
139 |
+
'max_streak': 0
|
140 |
+
}
|
141 |
+
if 'stats' not in st.session_state:
|
142 |
+
st.session_state.stats = {
|
143 |
+
'total_sentences': 0,
|
144 |
+
'correct_first_try': 0,
|
145 |
+
'accuracy_rate': 0.0,
|
146 |
+
'vocabulary_used': set()
|
147 |
+
}
|
148 |
if 'unique_words' not in st.session_state:
|
149 |
st.session_state.unique_words = set()
|
150 |
if 'total_words' not in st.session_state:
|
|
|
164 |
def submit_story():
|
165 |
if st.session_state.text_input.strip():
|
166 |
user_text = st.session_state.text_input
|
167 |
+
|
168 |
+
# เพิ่มคำศัพท์ที่ใช้
|
169 |
+
words = set(user_text.lower().split())
|
170 |
+
st.session_state.stats['vocabulary_used'].update(words)
|
171 |
+
|
172 |
+
# ตรวจสอบความถูกต้อง
|
173 |
+
feedback_data = provide_feedback(user_text, st.session_state.level)
|
174 |
+
is_correct = not feedback_data.get('has_errors', False)
|
175 |
+
|
176 |
+
# อัพเดตคะแนน
|
177 |
+
update_points(is_correct)
|
178 |
|
179 |
# เพิ่มประโยคของผู้ใช้
|
180 |
story_index = len(st.session_state.story)
|
|
|
498 |
"has_errors": False
|
499 |
}
|
500 |
|
501 |
+
def update_points(is_correct_first_try: bool):
|
502 |
+
"""อัพเดตคะแนนตามผลการเขียน"""
|
503 |
+
base_points = 10
|
504 |
+
if is_correct_first_try:
|
505 |
+
points = base_points * 2
|
506 |
+
st.session_state.points['perfect_sentences'] += 1
|
507 |
+
st.session_state.points['streak'] += 1
|
508 |
+
if st.session_state.points['streak'] > st.session_state.points['max_streak']:
|
509 |
+
st.session_state.points['max_streak'] = st.session_state.points['streak']
|
510 |
+
else:
|
511 |
+
points = base_points // 2
|
512 |
+
st.session_state.points['corrections_made'] += 1
|
513 |
+
st.session_state.points['streak'] = 0
|
514 |
+
|
515 |
+
st.session_state.points['total'] += points
|
516 |
+
|
517 |
+
st.session_state.stats['total_sentences'] += 1
|
518 |
+
if is_correct_first_try:
|
519 |
+
st.session_state.stats['correct_first_try'] += 1
|
520 |
+
st.session_state.stats['accuracy_rate'] = (
|
521 |
+
st.session_state.stats['correct_first_try'] /
|
522 |
+
st.session_state.stats['total_sentences'] * 100
|
523 |
+
)
|
524 |
+
|
525 |
+
# แสดงผลคะแนนและความสำเร็จ
|
526 |
+
def show_achievements():
|
527 |
+
with st.container():
|
528 |
+
# แสดงคะแนนรวม
|
529 |
+
st.markdown(f"""
|
530 |
+
<div style='background-color: #f0f8ff; padding: 15px; border-radius: 10px; margin-bottom: 15px;'>
|
531 |
+
<h3 style='color: #1e88e5; margin: 0;'>🌟 คะแนนรวม: {st.session_state.points['total']}</h3>
|
532 |
+
<p style='margin: 5px 0;'>Streak ปัจจุบัน: {st.session_state.points['streak']} ประโยค</p>
|
533 |
+
<p style='margin: 5px 0;'>Streak สูงสุด: {st.session_state.points['max_streak']} ประโยค</p>
|
534 |
+
</div>
|
535 |
+
""", unsafe_allow_html=True)
|
536 |
+
|
537 |
+
# แสดงสถิติ
|
538 |
+
st.markdown("""### 📊 สถิติการเขียน""")
|
539 |
+
col1, col2 = st.columns(2)
|
540 |
+
with col1:
|
541 |
+
st.metric("ประโยคที่เขียนทั้งหมด", st.session_state.stats['total_sentences'])
|
542 |
+
st.metric("ถูกต้องตั้งแต่แรก", st.session_state.stats['correct_first_try'])
|
543 |
+
with col2:
|
544 |
+
st.metric("ความแม่นยำ", f"{st.session_state.stats['accuracy_rate']:.1f}%")
|
545 |
+
st.metric("คำศัพท์ที่ใช้", len(st.session_state.stats['vocabulary_used']))
|
546 |
+
|
547 |
+
# แสดง Achievements
|
548 |
+
st.markdown("### 🏆 ความสำเร็จ")
|
549 |
+
for achievement_id, achievement in achievements_list.items():
|
550 |
+
if achievement['condition']():
|
551 |
+
st.success(f"{achievement['name']}: {achievement['description']}")
|
552 |
+
else:
|
553 |
+
st.info(f"🔒 {achievement['description']} (ยังไม่ปลดล็อก)")
|
554 |
+
|
555 |
def update_achievements(text: str):
|
556 |
"""Update user achievements based on their writing."""
|
557 |
words = set(text.lower().split())
|
|
|
636 |
else:
|
637 |
for idx, entry in enumerate(st.session_state.story):
|
638 |
if entry['role'] == 'You':
|
639 |
+
# เพิ่มไอคอนแสดงสถานะความถูกต้อง
|
640 |
+
status_icon = "✅ " if entry.get('is_correct') else "✍️ "
|
641 |
+
st.write(f"👤 You: {status_icon}{entry['content']}")
|
642 |
elif entry['role'] == 'AI':
|
643 |
st.write("🤖 AI:", entry['content'])
|
644 |
|
|
|
743 |
<div class="eng">💭 {prompt['eng']}</div>
|
744 |
</div>
|
745 |
""", unsafe_allow_html=True)
|
746 |
+
|
747 |
+
st.markdown("---") # เส้นคั่น
|
748 |
+
show_achievements()
|
749 |
|
750 |
# Achievements Box
|
751 |
st.markdown("""
|