Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,7 @@ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
12 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
13 |
from datetime import datetime
|
14 |
import random
|
|
|
15 |
|
16 |
# === 2. CONFIGURATIONS ===
|
17 |
# Theme Configuration
|
@@ -706,6 +707,31 @@ def update_session_stats():
|
|
706 |
return False
|
707 |
|
708 |
# === 4. UTILITY FUNCTIONS ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
709 |
def generate_story_continuation(user_input: str, level: str) -> str:
|
710 |
"""Generate AI story continuation using ChatGPT"""
|
711 |
|
@@ -940,22 +966,26 @@ def update_achievements():
|
|
940 |
"🌟 นักเขียนไร้ที่ติ" not in current_achievements):
|
941 |
current_achievements.append("🌟 นักเขียนไร้ที่ติ")
|
942 |
st.success("🎉 ได้รับความสำเร็จใหม่: นักเขียนไร้ที่ติ!")
|
|
|
943 |
|
944 |
if (len(st.session_state.stats['vocabulary_used']) >= 50 and
|
945 |
"📚 ราชาคำศัพท์" not in current_achievements):
|
946 |
current_achievements.append("📚 ราชาคำศัพท์")
|
947 |
st.success("🎉 ได้รับความสำเร็จใหม่: ราชาคำศัพท์!")
|
|
|
948 |
|
949 |
if (len(st.session_state.story) >= 10 and
|
950 |
"📖 นักแต่งนิทาน" not in current_achievements):
|
951 |
current_achievements.append("📖 นักแต่งนิทาน")
|
952 |
st.success("🎉 ได้รับความสำเร็จใหม่: นักแต่งนิทาน!")
|
|
|
953 |
|
954 |
if (st.session_state.stats['total_sentences'] >= 10 and
|
955 |
st.session_state.stats['accuracy_rate'] >= 80 and
|
956 |
"👑 ราชาความแม่นยำ" not in current_achievements):
|
957 |
current_achievements.append("👑 ราชาความแม่นยำ")
|
958 |
st.success("🎉 ได้รับความสำเร็จใหม่: ราชาความแม่นยำ!")
|
|
|
959 |
|
960 |
# บันทึกความสำเร็จ
|
961 |
st.session_state.achievements = current_achievements
|
@@ -2312,45 +2342,52 @@ def generate_ending_continuation(text: str, ending_type: str, remaining_sentence
|
|
2312 |
|
2313 |
def complete_story():
|
2314 |
"""Handle story completion and celebration"""
|
2315 |
-
|
2316 |
-
|
2317 |
-
|
2318 |
-
|
2319 |
-
|
2320 |
-
|
2321 |
-
|
2322 |
-
|
2323 |
-
|
2324 |
-
|
2325 |
-
|
2326 |
-
|
2327 |
-
|
2328 |
-
|
2329 |
-
|
2330 |
-
|
2331 |
-
|
2332 |
-
<
|
2333 |
-
|
2334 |
-
|
2335 |
-
|
2336 |
-
|
2337 |
-
|
2338 |
-
|
2339 |
-
<
|
2340 |
-
|
|
|
|
|
|
|
|
|
2341 |
</div>
|
2342 |
-
|
2343 |
-
|
2344 |
-
|
2345 |
-
|
2346 |
-
|
2347 |
-
|
2348 |
-
|
2349 |
-
|
2350 |
-
|
2351 |
-
|
2352 |
-
|
2353 |
-
|
|
|
|
|
|
|
2354 |
|
2355 |
def show_feedback_section():
|
2356 |
"""Display writing feedback section"""
|
@@ -2700,26 +2737,20 @@ def show_story_input():
|
|
2700 |
|
2701 |
# Submit and Clear buttons
|
2702 |
col1, col2, col3 = st.columns([3, 1, 1])
|
2703 |
-
|
2704 |
with col1:
|
2705 |
-
|
2706 |
-
|
2707 |
-
use_container_width=True,
|
2708 |
-
disabled=not text_input.strip() # Disable if empty
|
2709 |
-
)
|
2710 |
-
|
2711 |
-
if submit_button:
|
2712 |
-
if text_input.strip():
|
2713 |
-
try:
|
2714 |
-
# Save current submission
|
2715 |
-
st.session_state.last_submission = text_input.strip()
|
2716 |
-
with st.spinner("กำลังวิเคราะห์ประโยค..."):
|
2717 |
-
handle_story_submission(text_input.strip())
|
2718 |
-
except Exception as e:
|
2719 |
-
logging.error(f"Error submitting story: {str(e)}")
|
2720 |
-
st.error("เกิดข้อผิดพลาดในการส่งคำตอบ กรุณาลองใหม่อีกครั้ง")
|
2721 |
-
else:
|
2722 |
st.warning("กรุณาเขียนข้อความก่อนส่ง")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2723 |
|
2724 |
with col2:
|
2725 |
# Clear button with unique key
|
@@ -3443,6 +3474,9 @@ def main():
|
|
3443 |
# Initialize states
|
3444 |
init_session_state()
|
3445 |
init_theme_state()
|
|
|
|
|
|
|
3446 |
|
3447 |
# Initialize ending system state if not exists
|
3448 |
if 'ending_mode' not in st.session_state:
|
@@ -3470,7 +3504,8 @@ def main():
|
|
3470 |
# Sidebar
|
3471 |
with st.sidebar:
|
3472 |
show_sidebar()
|
3473 |
-
|
|
|
3474 |
# Session Status Check
|
3475 |
check_session_status()
|
3476 |
|
|
|
12 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
13 |
from datetime import datetime
|
14 |
import random
|
15 |
+
from sounds import AudioManager, get_sound_commands
|
16 |
|
17 |
# === 2. CONFIGURATIONS ===
|
18 |
# Theme Configuration
|
|
|
707 |
return False
|
708 |
|
709 |
# === 4. UTILITY FUNCTIONS ===
|
710 |
+
|
711 |
+
def initialize_audio():
|
712 |
+
"""Initialize audio system"""
|
713 |
+
if 'audio_manager' not in st.session_state:
|
714 |
+
audio_manager = AudioManager()
|
715 |
+
st.session_state.audio_manager = audio_manager
|
716 |
+
st.session_state.sound_commands = get_sound_commands()
|
717 |
+
|
718 |
+
# Add audio elements to the page
|
719 |
+
st.markdown(audio_manager.get_audio_html(), unsafe_allow_html=True)
|
720 |
+
|
721 |
+
def show_audio_controls():
|
722 |
+
"""Show audio control in sidebar"""
|
723 |
+
st.sidebar.markdown("### 🔊 ตั้งค่าเสียง")
|
724 |
+
|
725 |
+
# Background music toggle
|
726 |
+
if st.sidebar.checkbox("เปิดเพลงประกอบ", value=True, key='bgm_enabled'):
|
727 |
+
st.markdown(f"<script>{st.session_state.sound_commands['bgm_play']}</script>", unsafe_allow_html=True)
|
728 |
+
else:
|
729 |
+
st.markdown(f"<script>{st.session_state.sound_commands['bgm_pause']}</script>", unsafe_allow_html=True)
|
730 |
+
|
731 |
+
# Volume control
|
732 |
+
volume = st.sidebar.slider("ระดับเสียง", 0, 100, 50, key='volume') / 100
|
733 |
+
st.markdown(f"<script>setVolume({volume});</script>", unsafe_allow_html=True)
|
734 |
+
|
735 |
def generate_story_continuation(user_input: str, level: str) -> str:
|
736 |
"""Generate AI story continuation using ChatGPT"""
|
737 |
|
|
|
966 |
"🌟 นักเขียนไร้ที่ติ" not in current_achievements):
|
967 |
current_achievements.append("🌟 นักเขียนไร้ที่ติ")
|
968 |
st.success("🎉 ได้รับความสำเร็จใหม่: นักเขียนไร้ที่ติ!")
|
969 |
+
st.markdown(f"<script>{st.session_state.sound_commands['achievement']}</script>", unsafe_allow_html=True)
|
970 |
|
971 |
if (len(st.session_state.stats['vocabulary_used']) >= 50 and
|
972 |
"📚 ราชาคำศัพท์" not in current_achievements):
|
973 |
current_achievements.append("📚 ราชาคำศัพท์")
|
974 |
st.success("🎉 ได้รับความสำเร็จใหม่: ราชาคำศัพท์!")
|
975 |
+
st.markdown(f"<script>{st.session_state.sound_commands['achievement']}</script>", unsafe_allow_html=True)
|
976 |
|
977 |
if (len(st.session_state.story) >= 10 and
|
978 |
"📖 นักแต่งนิทาน" not in current_achievements):
|
979 |
current_achievements.append("📖 นักแต่งนิทาน")
|
980 |
st.success("🎉 ได้รับความสำเร็จใหม่: นักแต่งนิทาน!")
|
981 |
+
st.markdown(f"<script>{st.session_state.sound_commands['achievement']}</script>", unsafe_allow_html=True)
|
982 |
|
983 |
if (st.session_state.stats['total_sentences'] >= 10 and
|
984 |
st.session_state.stats['accuracy_rate'] >= 80 and
|
985 |
"👑 ราชาความแม่นยำ" not in current_achievements):
|
986 |
current_achievements.append("👑 ราชาความแม่นยำ")
|
987 |
st.success("🎉 ได้รับความสำเร็จใหม่: ราชาความแม่นยำ!")
|
988 |
+
st.markdown(f"<script>{st.session_state.sound_commands['achievement']}</script>", unsafe_allow_html=True)
|
989 |
|
990 |
# บันทึกความสำเร็จ
|
991 |
st.session_state.achievements = current_achievements
|
|
|
2342 |
|
2343 |
def complete_story():
|
2344 |
"""Handle story completion and celebration"""
|
2345 |
+
try:
|
2346 |
+
# เล่นเสียงจบเรื่อง
|
2347 |
+
st.markdown(f"<script>{st.session_state.sound_commands['complete']}</script>", unsafe_allow_html=True)
|
2348 |
+
st.balloons()
|
2349 |
+
|
2350 |
+
# Generate Story Summary
|
2351 |
+
story_summary = generate_story_summary(st.session_state.story)
|
2352 |
+
|
2353 |
+
# แสดงหน้าจบเรื่อง
|
2354 |
+
st.markdown(f"""
|
2355 |
+
<div style="
|
2356 |
+
background-color: #e8f5e9;
|
2357 |
+
padding: 20px;
|
2358 |
+
border-radius: 10px;
|
2359 |
+
text-align: center;
|
2360 |
+
margin: 20px 0;
|
2361 |
+
">
|
2362 |
+
<h2>🎉 ยินดีด้วย! คุณเขียนเรื่องราวจบสมบูรณ์แล้ว</h2>
|
2363 |
+
|
2364 |
+
<div style="margin: 20px 0;">
|
2365 |
+
<h3>📝 สรุปเรื่องราว</h3>
|
2366 |
+
<p>{story_summary}</p>
|
2367 |
+
</div>
|
2368 |
+
|
2369 |
+
<div style="margin: 20px 0;">
|
2370 |
+
<h3>🏆 ความสำเร็จ</h3>
|
2371 |
+
<p>จำนวนประโยค: {len(st.session_state.story)}</p>
|
2372 |
+
<p>คำศัพท์ที่ใช้: {len(st.session_state.stats['vocabulary_used'])}</p>
|
2373 |
+
<p>ความแม่นยำ: {st.session_state.stats['accuracy_rate']:.1f}%</p>
|
2374 |
+
</div>
|
2375 |
</div>
|
2376 |
+
""", unsafe_allow_html=True)
|
2377 |
+
|
2378 |
+
# แสดงตัวเลือกหลังจบเรื่อง
|
2379 |
+
col1, col2 = st.columns(2)
|
2380 |
+
with col1:
|
2381 |
+
if st.button("💾 บันทึกเรื่องราว", use_container_width=True):
|
2382 |
+
save_completed_story()
|
2383 |
+
with col2:
|
2384 |
+
if st.button("🔄 เริ่มเรื่องใหม่", use_container_width=True):
|
2385 |
+
reset_story()
|
2386 |
+
st.rerun()
|
2387 |
+
|
2388 |
+
except Exception as e:
|
2389 |
+
logging.error(f"Error in complete_story: {str(e)}")
|
2390 |
+
st.error("เกิดข้อผิดพลาดในการแสดงผลการจบเรื่อง")
|
2391 |
|
2392 |
def show_feedback_section():
|
2393 |
"""Display writing feedback section"""
|
|
|
2737 |
|
2738 |
# Submit and Clear buttons
|
2739 |
col1, col2, col3 = st.columns([3, 1, 1])
|
2740 |
+
|
2741 |
with col1:
|
2742 |
+
if st.button("📝 ส่งคำตอบ | Submit", use_container_width=True):
|
2743 |
+
if not text_input.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2744 |
st.warning("กรุณาเขียนข้อความก่อนส่ง")
|
2745 |
+
return
|
2746 |
+
try:
|
2747 |
+
# เล่นเสียงตอนกดปุ่ม Submit
|
2748 |
+
st.markdown(f"<script>{st.session_state.sound_commands['submit']}</script>", unsafe_allow_html=True)
|
2749 |
+
with st.spinner("กำลังวิเคราะห์ประโยค..."):
|
2750 |
+
handle_story_submission(text_input.strip())
|
2751 |
+
except Exception as e:
|
2752 |
+
logging.error(f"Error submitting story: {str(e)}")
|
2753 |
+
st.error("เกิดข้อผิดพลาดในการส่งคำตอบ กรุณาลองใหม่อีกครั้ง")
|
2754 |
|
2755 |
with col2:
|
2756 |
# Clear button with unique key
|
|
|
3474 |
# Initialize states
|
3475 |
init_session_state()
|
3476 |
init_theme_state()
|
3477 |
+
|
3478 |
+
# Initialize audio system
|
3479 |
+
initialize_audio()
|
3480 |
|
3481 |
# Initialize ending system state if not exists
|
3482 |
if 'ending_mode' not in st.session_state:
|
|
|
3504 |
# Sidebar
|
3505 |
with st.sidebar:
|
3506 |
show_sidebar()
|
3507 |
+
show_audio_controls()
|
3508 |
+
|
3509 |
# Session Status Check
|
3510 |
check_session_status()
|
3511 |
|