Rathapoom commited on
Commit
6292a8a
·
verified ·
1 Parent(s): 7cacc8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -31
app.py CHANGED
@@ -132,7 +132,6 @@ st.markdown("""
132
 
133
  # Initialize session state variables
134
  def init_session_state():
135
- """Initialize all session state variables"""
136
  if 'story' not in st.session_state:
137
  st.session_state.story = []
138
  if 'feedback' not in st.session_state:
@@ -147,7 +146,6 @@ def init_session_state():
147
  st.session_state.should_reset = False
148
  if 'user_input' not in st.session_state:
149
  st.session_state.user_input = ""
150
- # เพิ่ม points และ stats
151
  if 'points' not in st.session_state:
152
  st.session_state.points = {
153
  'total': 0,
@@ -163,12 +161,8 @@ def init_session_state():
163
  'accuracy_rate': 0.0,
164
  'vocabulary_used': set()
165
  }
166
- # เพิ่ม achievements
167
  if 'achievements' not in st.session_state:
168
  st.session_state.achievements = []
169
- # เพิ่มชื่อผู้เล่น (optional)
170
- if 'player_name' not in st.session_state:
171
- st.session_state.player_name = "Young Writer"
172
 
173
  init_session_state()
174
 
@@ -180,7 +174,7 @@ def clear_input():
180
  def submit_story():
181
  if st.session_state.text_input.strip():
182
  user_text = st.session_state.text_input
183
-
184
  # เพิ่มคำศัพท์ที่ใช้
185
  words = set(user_text.lower().split())
186
  st.session_state.stats['vocabulary_used'].update(words)
@@ -192,6 +186,9 @@ def submit_story():
192
  # อัพเดตคะแนน
193
  update_points(is_correct)
194
 
 
 
 
195
  # เพิ่มประโยคของผู้ใช้
196
  story_index = len(st.session_state.story)
197
  st.session_state.story.append({
@@ -540,8 +537,9 @@ def update_points(is_correct_first_try: bool):
540
 
541
  # แสดงผลคะแนนและความสำเร็จ
542
  def show_achievements():
 
543
  with st.container():
544
- # แสดงคะแนนรวม
545
  st.markdown(f"""
546
  <div style='background-color: #f0f8ff; padding: 15px; border-radius: 10px; margin-bottom: 15px;'>
547
  <h3 style='color: #1e88e5; margin: 0;'>🌟 คะแนนรวม: {st.session_state.points['total']}</h3>
@@ -550,40 +548,86 @@ def show_achievements():
550
  </div>
551
  """, unsafe_allow_html=True)
552
 
553
- # แสดงสถิติ
554
- st.markdown("### 📊 สถิติการเขียน")
 
 
 
 
 
555
  col1, col2 = st.columns(2)
556
  with col1:
557
- st.metric("ประโยคที่เขียนทั้งหมด", st.session_state.stats['total_sentences'])
558
- st.metric("ถูกต้องตั้งแต่แรก", st.session_state.stats['correct_first_try'])
 
 
 
 
 
 
 
 
559
  with col2:
560
- st.metric("ความแม่นยำ", f"{st.session_state.stats['accuracy_rate']:.1f}%")
561
- st.metric("คำศัพท์ที่ใช้", len(st.session_state.stats['vocabulary_used']))
 
 
 
 
 
 
 
 
562
 
563
- # แสดง Achievements ที่มี
564
- st.markdown("### 🏆 ความสำเร็จ")
 
 
 
 
 
565
  if st.session_state.achievements:
566
  for achievement in st.session_state.achievements:
567
- st.success(f"🏆 {achievement}")
568
  else:
569
  st.info("ยังไม่มีความสำเร็จ - เขียนต่อไปเพื่อปลดล็อกรางวัล!")
570
 
571
- def update_achievements(text: str):
572
- """Update user achievements based on their writing."""
573
- words = set(text.lower().split())
574
- st.session_state.unique_words.update(words)
575
- st.session_state.total_words += len(words)
 
 
 
 
 
 
 
 
 
 
 
 
576
 
577
- achievements = {
578
- '50 Words': len(st.session_state.unique_words) >= 50,
579
- '100 Words': len(st.session_state.unique_words) >= 100,
580
- 'Story Master': len(st.session_state.story) >= 10,
581
- }
 
 
 
 
 
 
 
582
 
583
- for badge, condition in achievements.items():
584
- if condition and badge not in st.session_state.badges:
585
- st.session_state.badges.append(badge)
586
- st.success(f"🏆 Achievement Unlocked: {badge}!")
 
587
 
588
  def create_story_pdf():
589
  """สร้าง PDF จากเรื่องราวที่เขียน"""
 
132
 
133
  # Initialize session state variables
134
  def init_session_state():
 
135
  if 'story' not in st.session_state:
136
  st.session_state.story = []
137
  if 'feedback' not in st.session_state:
 
146
  st.session_state.should_reset = False
147
  if 'user_input' not in st.session_state:
148
  st.session_state.user_input = ""
 
149
  if 'points' not in st.session_state:
150
  st.session_state.points = {
151
  'total': 0,
 
161
  'accuracy_rate': 0.0,
162
  'vocabulary_used': set()
163
  }
 
164
  if 'achievements' not in st.session_state:
165
  st.session_state.achievements = []
 
 
 
166
 
167
  init_session_state()
168
 
 
174
  def submit_story():
175
  if st.session_state.text_input.strip():
176
  user_text = st.session_state.text_input
177
+
178
  # เพิ่มคำศัพท์ที่ใช้
179
  words = set(user_text.lower().split())
180
  st.session_state.stats['vocabulary_used'].update(words)
 
186
  # อัพเดตคะแนน
187
  update_points(is_correct)
188
 
189
+ # อัพเดต achievements
190
+ update_achievements()
191
+
192
  # เพิ่มประโยคของผู้ใช้
193
  story_index = len(st.session_state.story)
194
  st.session_state.story.append({
 
537
 
538
  # แสดงผลคะแนนและความสำเร็จ
539
  def show_achievements():
540
+ """แสดงความสำเร็จและสถิติ"""
541
  with st.container():
542
+ # 1. แสดงคะแนนรวมและ Streak
543
  st.markdown(f"""
544
  <div style='background-color: #f0f8ff; padding: 15px; border-radius: 10px; margin-bottom: 15px;'>
545
  <h3 style='color: #1e88e5; margin: 0;'>🌟 คะแนนรวม: {st.session_state.points['total']}</h3>
 
548
  </div>
549
  """, unsafe_allow_html=True)
550
 
551
+ # 2. แสดงสถิติการเขียน
552
+ st.markdown("""
553
+ <div style='margin-bottom: 10px;'>
554
+ <h3>📊 สถิติการเขียน</h3>
555
+ </div>
556
+ """, unsafe_allow_html=True)
557
+
558
  col1, col2 = st.columns(2)
559
  with col1:
560
+ st.metric(
561
+ "ประโยคที่เขียนทั้งหมด",
562
+ st.session_state.stats['total_sentences'],
563
+ help="จำนวนประโยคทั้งหมดที่คุณได้เขียน"
564
+ )
565
+ st.metric(
566
+ "ถูกต้องตั้งแต่แรก",
567
+ st.session_state.stats['correct_first_try'],
568
+ help="จำนวนประโยคที่ถูกต้องโดยไม่ต้องแก้ไข"
569
+ )
570
  with col2:
571
+ st.metric(
572
+ "ความแม่นยำ",
573
+ f"{st.session_state.stats['accuracy_rate']:.1f}%",
574
+ help="เปอร์เซ็นต์ของประโยคที่ถูกต้องตั้งแต่แรก"
575
+ )
576
+ st.metric(
577
+ "คำศัพท์ที่ใช้",
578
+ len(st.session_state.stats['vocabulary_used']),
579
+ help="จำนวนคำศัพท์ที่ไม่ซ้ำกันที่คุณได้ใช้"
580
+ )
581
 
582
+ # 3. แสดงความสำเร็จ
583
+ st.markdown("""
584
+ <div style='margin: 20px 0 10px 0;'>
585
+ <h3>🏆 ความสำเร็จ</h3>
586
+ </div>
587
+ """, unsafe_allow_html=True)
588
+
589
  if st.session_state.achievements:
590
  for achievement in st.session_state.achievements:
591
+ st.success(achievement)
592
  else:
593
  st.info("ยังไม่มีความสำเร็จ - เขียนต่อไปเพื่อปลดล็อกรางวัล!")
594
 
595
+ # 4. แสดงความสำเร็จที่ยังไม่ได้รับ (ตัวเลือก)
596
+ if not st.session_state.achievements: # ถ้ายังไม่มีความสำเร็จ
597
+ st.markdown("""
598
+ <div style='margin-top: 15px; font-size: 0.9em; color: #666;'>
599
+ เป้าหมายที่จะได้รับความสำเร็จ:
600
+ </div>
601
+ """, unsafe_allow_html=True)
602
+ st.markdown("""
603
+ - 🌟 นักเขียนไร้ที่ติ: เขียนถูกต้อง 5 ประโยคติดต่อกัน
604
+ - 📚 ราชาคำศัพท์: ใช้คำศัพท์ไม่ซ้ำกัน 50 คำ
605
+ - 📖 นักแต่งนิทาน: เขียนเรื่องยาว 10 ประโยค
606
+ - 👑 ราชาความแม่นยำ: มีอัตราความถูกต้อง 80% ขึ้นไป
607
+ """)
608
+
609
+ def update_achievements():
610
+ """ตรวจสอบและอัพเดตความสำเร็จ"""
611
+ current_achievements = st.session_state.achievements
612
 
613
+ # เช็คเงื่อนไขต่างๆ
614
+ if st.session_state.points['streak'] >= 5 and "🌟 นักเขียนไร้ที่ติ" not in current_achievements:
615
+ current_achievements.append("🌟 นักเขียนไร้ที่ติ")
616
+ st.success("🎉 ได้รับความสำเร็จใหม่: นักเขียนไร้ที่ติ!")
617
+
618
+ if len(st.session_state.stats['vocabulary_used']) >= 50 and "📚 ราชาคำศัพท์" not in current_achievements:
619
+ current_achievements.append("📚 ราชาคำศัพท์")
620
+ st.success("🎉 ได้รับความสำเร็จใหม่: ราชาคำศัพท์!")
621
+
622
+ if len(st.session_state.story) >= 10 and "📖 นักแต่งนิทาน" not in current_achievements:
623
+ current_achievements.append("📖 นักแต่งนิทาน")
624
+ st.success("🎉 ได้รับความสำเร็จใหม่: นักแต่งนิทาน!")
625
 
626
+ if (st.session_state.stats['total_sentences'] >= 10 and
627
+ st.session_state.stats['accuracy_rate'] >= 80 and
628
+ "👑 ราชาความแม่นยำ" not in current_achievements):
629
+ current_achievements.append("👑 ราชาความแม่นยำ")
630
+ st.success("🎉 ได้รับความสำเร็จใหม่: ราชาความแม่นยำ!")
631
 
632
  def create_story_pdf():
633
  """สร้าง PDF จากเรื่องราวที่เขียน"""