Rathapoom commited on
Commit
2cbd7c8
·
verified ·
1 Parent(s): 69fee89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -35
app.py CHANGED
@@ -237,13 +237,25 @@ def main():
237
  st.write("### 📖 Our Story So Far:")
238
  for entry in st.session_state.story:
239
  css_class = "ai-text" if entry['author'] == 'AI' else "player-text"
 
 
240
  st.markdown(f'<div class="story-text {css_class}">{entry["text"]}</div>',
241
- unsafe_allow_html=True)
242
- if 'prompt' in entry:
243
- st.markdown(f'<div class="prompt-text">🤔 {entry["prompt"]}</div>',
244
- unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
245
  st.markdown('</div>', unsafe_allow_html=True)
246
-
247
  # Box 2: Writing Area
248
  if st.session_state.current_turn == 'Player':
249
  with st.container():
@@ -255,6 +267,8 @@ def main():
255
  process_player_input(user_input)
256
  st.rerun()
257
  st.markdown('</div>', unsafe_allow_html=True)
 
 
258
 
259
  # Box 3: Help Buttons
260
  with st.container():
@@ -288,44 +302,18 @@ def main():
288
  st.markdown('</div>', unsafe_allow_html=True)
289
 
290
  def process_player_input(user_input):
291
- """Process the player's input, generate feedback, and update the story"""
292
  try:
293
  # Get story context for AI
294
  story_context = " ".join([entry['text'] for entry in st.session_state.story])
295
 
296
- # Get writing feedback from GPT
297
- feedback_response = openai.ChatCompletion.create(
298
- model="gpt-4o-mini", # หรือ model ที่ถูกต้องตามที่คุณใช้
299
- messages=[
300
- {"role": "system", "content": f"""You are an English teacher helping a {st.session_state.current_level} level student.
301
- Analyze their sentence and provide feedback in JSON format with:
302
- 1. grammar_check: List of grammar issues found (if any)
303
- 2. spelling_check: List of spelling issues found (if any)
304
- 3. improvement_suggestions: Specific suggestions for improvement
305
- 4. positive_feedback: What they did well
306
- Be encouraging but thorough in your feedback."""},
307
- {"role": "user", "content": f"Student's sentence: {user_input}"}
308
- ],
309
- temperature=0.7
310
- )
311
- feedback = json.loads(feedback_response.choices[0].message.content)
312
-
313
- # Add player's sentence to story with feedback
314
  st.session_state.story.append({
315
  'author': 'Player',
316
  'text': user_input,
317
- 'feedback': feedback,
318
  'timestamp': datetime.now().isoformat()
319
  })
320
 
321
- # Update vocabulary used
322
- words = set(user_input.lower().split())
323
- st.session_state.vocabulary_used.update(words)
324
-
325
- # Check for achievements
326
- check_achievements(user_input, feedback)
327
-
328
- # Get AI's continuation
329
  ai_response = get_ai_story_continuation(
330
  story_context + "\n" + user_input,
331
  st.session_state.current_level
@@ -341,8 +329,28 @@ def process_player_input(user_input):
341
  'timestamp': datetime.now().isoformat()
342
  })
343
 
344
- # Switch turn back to player
345
- st.session_state.current_turn = 'Player'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
 
347
  # Show feedback to player
348
  display_feedback(feedback)
 
237
  st.write("### 📖 Our Story So Far:")
238
  for entry in st.session_state.story:
239
  css_class = "ai-text" if entry['author'] == 'AI' else "player-text"
240
+
241
+ # แสดงข้อความหลัก
242
  st.markdown(f'<div class="story-text {css_class}">{entry["text"]}</div>',
243
+ unsafe_allow_html=True)
244
+
245
+ # ถ้าเป็น AI response ให้แสดงคำแปลและ prompt
246
+ if entry['author'] == 'AI':
247
+ if 'translation' in entry:
248
+ st.markdown(f'<div class="prompt-text">🇹🇭 {entry["translation"]}</div>',
249
+ unsafe_allow_html=True)
250
+ if 'prompt' in entry:
251
+ st.markdown(f'<div class="prompt-text">🤔 {entry["prompt"]}</div>',
252
+ unsafe_allow_html=True)
253
+ if 'vocabulary' in entry:
254
+ vocab_text = ", ".join(entry["vocabulary"])
255
+ st.markdown(f'<div class="prompt-text">📚 Suggested words: {vocab_text}</div>',
256
+ unsafe_allow_html=True)
257
  st.markdown('</div>', unsafe_allow_html=True)
258
+
259
  # Box 2: Writing Area
260
  if st.session_state.current_turn == 'Player':
261
  with st.container():
 
267
  process_player_input(user_input)
268
  st.rerun()
269
  st.markdown('</div>', unsafe_allow_html=True)
270
+ else:
271
+ st.info("🤖 AI is thinking...") # แสดงสถานะเมื่อเป็น turn ของ AI
272
 
273
  # Box 3: Help Buttons
274
  with st.container():
 
302
  st.markdown('</div>', unsafe_allow_html=True)
303
 
304
  def process_player_input(user_input):
 
305
  try:
306
  # Get story context for AI
307
  story_context = " ".join([entry['text'] for entry in st.session_state.story])
308
 
309
+ # Add player's sentence first
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
  st.session_state.story.append({
311
  'author': 'Player',
312
  'text': user_input,
 
313
  'timestamp': datetime.now().isoformat()
314
  })
315
 
316
+ # Get AI's continuation immediately after player's input
 
 
 
 
 
 
 
317
  ai_response = get_ai_story_continuation(
318
  story_context + "\n" + user_input,
319
  st.session_state.current_level
 
329
  'timestamp': datetime.now().isoformat()
330
  })
331
 
332
+ # Get feedback for player's writing
333
+ feedback_response = openai.ChatCompletion.create(
334
+ model="gpt-4o-mini",
335
+ messages=[
336
+ {"role": "system", "content": f"""You are an English teacher helping a {st.session_state.current_level} level student.
337
+ Analyze their sentence and provide feedback in JSON format with:
338
+ 1. grammar_check: List of grammar issues found (if any)
339
+ 2. spelling_check: List of spelling issues found (if any)
340
+ 3. improvement_suggestions: Specific suggestions for improvement
341
+ 4. positive_feedback: What they did well
342
+ Be encouraging but thorough in your feedback."""},
343
+ {"role": "user", "content": f"Student's sentence: {user_input}"}
344
+ ]
345
+ )
346
+ feedback = json.loads(feedback_response.choices[0].message.content)
347
+
348
+ # Update vocabulary used
349
+ words = set(user_input.lower().split())
350
+ st.session_state.vocabulary_used.update(words)
351
+
352
+ # Check for achievements
353
+ check_achievements(user_input, feedback)
354
 
355
  # Show feedback to player
356
  display_feedback(feedback)