Rathapoom commited on
Commit
511dc37
·
verified ·
1 Parent(s): d6aa5eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -26
app.py CHANGED
@@ -141,47 +141,88 @@ 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,
182
- temperature=0.7
 
 
 
 
 
 
183
  )
184
- return response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  except Exception as e:
186
  st.error(f"Error generating story continuation: {str(e)}")
187
  return "I'm having trouble continuing the story. Please try again."
 
141
  """Generate AI story continuation using ChatGPT with level-appropriate content."""
142
  level_context = {
143
  'Beginner': """
144
+ Role: You are a teaching assistant for Thai students in grades 1-3.
145
+ Rules:
146
+ - Use only 1-2 VERY simple sentences
147
+ - Use Present Simple Tense only
148
+ - Use basic vocabulary (family, school, daily activities)
149
+ - Each sentence should be 5-7 words maximum
150
+ - Focus on clear, basic responses
151
+ Example responses:
152
+ - "The cat sits under the tree."
153
+ - "The boy plays with his toy car."
154
+ - "They walk to school together."
155
  """,
156
  'Intermediate': """
157
+ Role: You are a teaching assistant for Thai students in grades 4-6.
158
+ Rules:
159
+ - Use exactly 2 sentences maximum
160
+ - Can use Present or Past Tense
161
+ - Keep each sentence under 12 words
162
+ - Use grade-appropriate vocabulary
163
+ - Add simple descriptions but stay concise
164
+ Example responses:
165
+ - "The brown cat jumped over the tall wooden fence. It landed softly in the garden."
166
+ - "Tom carefully opened his mysterious new book. The colorful pages showed amazing magical creatures."
167
  """,
168
  'Advanced': """
169
+ Role: You are a teaching assistant for Thai students in grades 7-9.
170
+ Rules:
171
+ - Use 2-3 sentences maximum (no more!)
172
+ - Various tenses are allowed
173
+ - No strict word limit per sentence, but keep overall response concise
174
+ - Use more sophisticated vocabulary and sentence structures
175
+ - Create engaging responses that encourage creative continuation
176
+ - Focus on quality and natural flow rather than sentence length
177
+ Example responses:
178
+ - "Sarah discovered an ancient-looking letter hidden beneath the creaky floorboards. The yellowed paper contained a mysterious message."
179
+ - "As the storm clouds gathered overhead, James remembered the old legend about the mountain. Lightning illuminated the winding path that led to the cave entrance."
180
  """
181
  }
182
 
183
  try:
184
+ story_context = '\n'.join([entry['content'] for entry in st.session_state.story[-3:]]) # Only use last 3 entries
185
 
186
  response = client.chat.completions.create(
187
  model="gpt-4o-mini",
188
  messages=[
189
+ {"role": "system", "content": f"""You are a storytelling assistant for Thai students.
190
  {level_context[level]}
191
+ CRUCIAL GUIDELINES:
192
+ - NEVER exceed the maximum number of sentences for the level
193
+ - Create openings for student's creativity
194
+ - Do not resolve plot points or conclude the story
195
+ - Avoid using 'suddenly' or 'then'
196
+ - Make each sentence meaningful but incomplete
197
+ - Leave room for the student to develop the story
198
+
199
+ Remember: This is interactive storytelling - let the student drive the story forward."""},
200
+ {"role": "user", "content": f"Story context (recent):\n{story_context}\nStudent's input:\n{user_input}\nProvide a brief continuation:"}
201
  ],
202
+ max_tokens={
203
+ 'Beginner': 30,
204
+ 'Intermediate': 40,
205
+ 'Advanced': 50
206
+ }[level],
207
+ temperature=0.7,
208
+ presence_penalty=0.6, # Discourage repetitive responses
209
+ frequency_penalty=0.6 # Encourage diversity in responses
210
  )
211
+
212
+ # Additional length check and cleanup
213
+ response_text = response.choices[0].message.content.strip()
214
+ sentences = [s.strip() for s in response_text.split('.') if s.strip()]
215
+
216
+ # Limit sentences based on level
217
+ max_sentences = {'Beginner': 2, 'Intermediate': 2, 'Advanced': 3}
218
+ if len(sentences) > max_sentences[level]:
219
+ sentences = sentences[:max_sentences[level]]
220
+
221
+ # Reconstruct response with proper punctuation
222
+ response_text = '. '.join(sentences) + '.'
223
+
224
+ return response_text
225
+
226
  except Exception as e:
227
  st.error(f"Error generating story continuation: {str(e)}")
228
  return "I'm having trouble continuing the story. Please try again."