Update app.py
Browse files
app.py
CHANGED
@@ -15,9 +15,48 @@ st.set_page_config(
|
|
15 |
# Initialize OpenAI client
|
16 |
client = OpenAI()
|
17 |
|
18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
st.markdown("""
|
20 |
<style>
|
|
|
|
|
21 |
.thai-eng {
|
22 |
font-size: 1.1em;
|
23 |
padding: 10px;
|
@@ -32,10 +71,19 @@ st.markdown("""
|
|
32 |
.eng {
|
33 |
color: #333;
|
34 |
}
|
35 |
-
.
|
36 |
background-color: #e3f2fd;
|
37 |
-
padding:
|
38 |
-
border-radius:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
}
|
40 |
</style>
|
41 |
""", unsafe_allow_html=True)
|
@@ -61,7 +109,7 @@ init_session_state()
|
|
61 |
|
62 |
def show_welcome_section():
|
63 |
st.markdown("""
|
64 |
-
<div class="
|
65 |
<div class="thai">
|
66 |
🌟 ยินดีต้อนรับสู่ JoyStory - แอพฝึกเขียนภาษาอังกฤษแสนสนุก!
|
67 |
<br>
|
@@ -73,29 +121,61 @@ def show_welcome_section():
|
|
73 |
</div>
|
74 |
""", unsafe_allow_html=True)
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
</div>
|
85 |
-
|
86 |
-
""", unsafe_allow_html=True)
|
87 |
|
88 |
def generate_story_continuation(user_input: str, level: str) -> str:
|
89 |
-
"""Generate AI story continuation using ChatGPT."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
try:
|
91 |
story_context = '\n'.join([entry['content'] for entry in st.session_state.story])
|
92 |
|
93 |
response = client.chat.completions.create(
|
94 |
model="gpt-4o-mini",
|
95 |
messages=[
|
96 |
-
{"role": "system", "content": f"""You are a creative storytelling assistant for {level}
|
97 |
-
|
98 |
-
|
|
|
99 |
{"role": "user", "content": f"Story so far:\n{story_context}\nUser's addition:\n{user_input}\nContinue the story with 1-2 short sentences:"}
|
100 |
],
|
101 |
max_tokens=50,
|
@@ -272,26 +352,35 @@ if st.session_state.should_reset:
|
|
272 |
reset_story()
|
273 |
|
274 |
# Main UI Layout
|
275 |
-
st.markdown("# 📖 JoyStory")
|
276 |
show_welcome_section()
|
|
|
277 |
|
278 |
# Sidebar for settings
|
279 |
with st.sidebar:
|
280 |
st.markdown("""
|
281 |
<div class="thai">
|
282 |
-
🎯
|
283 |
</div>
|
284 |
""", unsafe_allow_html=True)
|
285 |
|
286 |
level = st.radio(
|
287 |
-
"
|
288 |
-
(
|
289 |
-
|
|
|
290 |
)
|
291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
st.session_state.level = level
|
293 |
|
294 |
-
if st.button("Start New Story"):
|
295 |
st.session_state.should_reset = True
|
296 |
st.rerun()
|
297 |
|
|
|
15 |
# Initialize OpenAI client
|
16 |
client = OpenAI()
|
17 |
|
18 |
+
# Define level configurations
|
19 |
+
level_options = {
|
20 |
+
'Beginner': {
|
21 |
+
'thai_name': 'ระดับเริ่มต้น (ป.1-3)',
|
22 |
+
'age_range': '7-9 ปี',
|
23 |
+
'description': 'เหมาะสำหรับน้องๆ ที่เริ่มเรียนรู้การเขียนประโยคภาษาอังกฤษ',
|
24 |
+
'features': [
|
25 |
+
'ประโยคสั้นๆ ง่ายๆ',
|
26 |
+
'คำศัพท์พื้นฐานที่ใช้ในชีวิตประจำวัน',
|
27 |
+
'มีคำแนะนำภาษาไทยละเอียด',
|
28 |
+
'เน้นการใช้ Present Simple Tense'
|
29 |
+
]
|
30 |
+
},
|
31 |
+
'Intermediate': {
|
32 |
+
'thai_name': 'ระดับกลาง (ป.4-6)',
|
33 |
+
'age_range': '10-12 ปี',
|
34 |
+
'description': 'เหมาะสำหรับน้องๆ ที่สามารถเขียนประโยคพื้นฐานได้แล้ว',
|
35 |
+
'features': [
|
36 |
+
'ประโยคซับซ้อนขึ้น',
|
37 |
+
'เริ่มใช้ Past Tense ได้',
|
38 |
+
'คำศัพท์หลากหลายขึ้น',
|
39 |
+
'สามารถเขียนเรื่องราวต่อเนื่องได้'
|
40 |
+
]
|
41 |
+
},
|
42 |
+
'Advanced': {
|
43 |
+
'thai_name': 'ระดับก้าวหน้า (ม.1-3)',
|
44 |
+
'age_range': '13-15 ปี',
|
45 |
+
'description': 'เหมาะสำหรับน้องๆ ที่มีพื้นฐานภาษาอังกฤษดี',
|
46 |
+
'features': [
|
47 |
+
'เขียนเรื่องราวได้หลากหลายรูปแบบ',
|
48 |
+
'ใช้ Tense ต่างๆ ได้',
|
49 |
+
'คำศัพท์ระดับสูงขึ้น',
|
50 |
+
'สามารถแต่งเรื่องที่ซับซ้อนได้'
|
51 |
+
]
|
52 |
+
}
|
53 |
+
}
|
54 |
+
|
55 |
+
# Add custom CSS including new styles for level selection
|
56 |
st.markdown("""
|
57 |
<style>
|
58 |
+
@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@400;700&display=swap');
|
59 |
+
|
60 |
.thai-eng {
|
61 |
font-size: 1.1em;
|
62 |
padding: 10px;
|
|
|
71 |
.eng {
|
72 |
color: #333;
|
73 |
}
|
74 |
+
.level-info {
|
75 |
background-color: #e3f2fd;
|
76 |
+
padding: 10px;
|
77 |
+
border-radius: 8px;
|
78 |
+
margin: 10px 0;
|
79 |
+
font-size: 0.9em;
|
80 |
+
}
|
81 |
+
.parent-guide {
|
82 |
+
background-color: #fff3e0;
|
83 |
+
padding: 15px;
|
84 |
+
border-radius: 8px;
|
85 |
+
margin: 15px 0;
|
86 |
+
border-left: 4px solid #ff9800;
|
87 |
}
|
88 |
</style>
|
89 |
""", unsafe_allow_html=True)
|
|
|
109 |
|
110 |
def show_welcome_section():
|
111 |
st.markdown("""
|
112 |
+
<div class="welcome-header">
|
113 |
<div class="thai">
|
114 |
🌟 ยินดีต้อนรับสู่ JoyStory - แอพฝึกเขียนภาษาอังกฤษแสนสนุก!
|
115 |
<br>
|
|
|
121 |
</div>
|
122 |
""", unsafe_allow_html=True)
|
123 |
|
124 |
+
def show_parent_guide():
|
125 |
+
with st.expander("📖 คำแนะนำสำหรับผู้ปกครอง | Parent's Guide"):
|
126 |
+
st.markdown("""
|
127 |
+
<div class="parent-guide">
|
128 |
+
<h4>คำแนะนำในการใช้งาน</h4>
|
129 |
+
<ul>
|
130 |
+
<li>แนะนำให้นั่งเขียนเรื่องราวร่วมกับน้องๆ</li>
|
131 |
+
<li>ช่วยอธิบายคำแนะนำและคำศัพท์ที่น้องๆ ไม่เข้าใจ</li>
|
132 |
+
<li>ให้กำลังใจและชื่นชมเมื่อน้องๆ เขียนได้ดี</li>
|
133 |
+
<li>ใช้เวลาในการเขียนแต่ละครั้งไม่เกิน 20-30 นาที</li>
|
134 |
+
</ul>
|
135 |
+
<p>💡 เคล็ดลับ: ให้น้องๆ พูดเรื่องราวที่อยากเขียนเป็นภาษาไทยก่อน
|
136 |
+
แล้วค่อยๆ ช่วยกันแปลงเป็นภาษาอังกฤษ</p>
|
137 |
</div>
|
138 |
+
""", unsafe_allow_html=True)
|
|
|
139 |
|
140 |
def generate_story_continuation(user_input: str, level: str) -> str:
|
141 |
+
"""Generate AI story continuation using ChatGPT with level-appropriate content."""
|
142 |
+
level_context = {
|
143 |
+
'Beginner': """
|
144 |
+
Guidelines for Beginner level (Thai students grade 1-3):
|
145 |
+
- Use only Present Simple Tense
|
146 |
+
- Keep sentences very short and simple
|
147 |
+
- Use basic everyday vocabulary
|
148 |
+
- Focus on simple daily life topics
|
149 |
+
- Avoid complex words or phrases
|
150 |
+
""",
|
151 |
+
'Intermediate': """
|
152 |
+
Guidelines for Intermediate level (Thai students grade 4-6):
|
153 |
+
- Use Present and Past Tense
|
154 |
+
- Sentences can be slightly complex
|
155 |
+
- Use more varied vocabulary
|
156 |
+
- Include some descriptive words
|
157 |
+
- Stories can have more development
|
158 |
+
""",
|
159 |
+
'Advanced': """
|
160 |
+
Guidelines for Advanced level (Thai students grade 7-9):
|
161 |
+
- Use various tenses appropriately
|
162 |
+
- Complex sentences are acceptable
|
163 |
+
- Advanced vocabulary can be used
|
164 |
+
- Creative and detailed storytelling
|
165 |
+
- More sophisticated plot development
|
166 |
+
"""
|
167 |
+
}
|
168 |
+
|
169 |
try:
|
170 |
story_context = '\n'.join([entry['content'] for entry in st.session_state.story])
|
171 |
|
172 |
response = client.chat.completions.create(
|
173 |
model="gpt-4o-mini",
|
174 |
messages=[
|
175 |
+
{"role": "system", "content": f"""You are a creative storytelling assistant for {level} level Thai students.
|
176 |
+
{level_context[level]}
|
177 |
+
Keep the language appropriate for the level while being engaging.
|
178 |
+
Respond with only 1-2 short sentences to continue the story."""},
|
179 |
{"role": "user", "content": f"Story so far:\n{story_context}\nUser's addition:\n{user_input}\nContinue the story with 1-2 short sentences:"}
|
180 |
],
|
181 |
max_tokens=50,
|
|
|
352 |
reset_story()
|
353 |
|
354 |
# Main UI Layout
|
|
|
355 |
show_welcome_section()
|
356 |
+
show_parent_guide() # Add parent guide right after welcome section
|
357 |
|
358 |
# Sidebar for settings
|
359 |
with st.sidebar:
|
360 |
st.markdown("""
|
361 |
<div class="thai">
|
362 |
+
🎯 เลือกระดับการเรียนรู้
|
363 |
</div>
|
364 |
""", unsafe_allow_html=True)
|
365 |
|
366 |
level = st.radio(
|
367 |
+
"", # ไม่แสดงชื่อ label ภาษาอังกฤษ
|
368 |
+
options=list(level_options.keys()),
|
369 |
+
format_func=lambda x: level_options[x]['thai_name'],
|
370 |
+
help="เลือกระดับที่เหมาะสมกับความสามารถของน้องๆ"
|
371 |
)
|
372 |
|
373 |
+
# แสดงคำอธิบายระดับ
|
374 |
+
st.markdown(f"""
|
375 |
+
<div class="level-info thai">
|
376 |
+
🎓 {level_options[level]['description']}
|
377 |
+
<br>📚 เหมาะสำหรับอายุ: {level_options[level]['age_range']}
|
378 |
+
</div>
|
379 |
+
""", unsafe_allow_html=True)
|
380 |
+
|
381 |
st.session_state.level = level
|
382 |
|
383 |
+
if st.button("เริ่มเรื่องใหม่ | Start New Story"):
|
384 |
st.session_state.should_reset = True
|
385 |
st.rerun()
|
386 |
|