Rathapoom commited on
Commit
b7dcb66
·
verified ·
1 Parent(s): 906cfa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -104,8 +104,6 @@ def init_session_state():
104
  st.session_state.badges = []
105
  if 'should_reset' not in st.session_state:
106
  st.session_state.should_reset = False
107
- if 'user_input' not in st.session_state:
108
- st.session_state.user_input = ""
109
 
110
  init_session_state()
111
 
@@ -113,6 +111,25 @@ init_session_state()
113
  def clear_input():
114
  st.session_state.user_input = ""
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def show_welcome_section():
117
  st.markdown("""
118
  <div class="welcome-header">
@@ -463,35 +480,18 @@ with col1:
463
  </div>
464
  """, unsafe_allow_html=True)
465
 
466
- user_input = st.text_area(
 
467
  "เขียนต่อจากเรื่องราว | Continue the story:",
468
  height=100,
469
- key="user_input", # ใช้ key เดียวกับ session state
470
- value=st.session_state.user_input # ผูก value กับ session state
471
  )
472
 
473
- if st.button("ส่งคำตอบ | Submit"):
474
- if user_input.strip():
475
- # Add user's input to story
476
- st.session_state.story.append({"role": "You", "content": user_input})
477
-
478
- # Update achievements
479
- update_achievements(user_input)
480
-
481
- # Generate feedback
482
- st.session_state.feedback = provide_feedback(user_input, st.session_state.level)
483
-
484
- # Generate AI continuation
485
- ai_response = generate_story_continuation(user_input, st.session_state.level)
486
- st.session_state.story.append({"role": "AI", "content": ai_response})
487
-
488
- # Clear the input
489
- clear_input()
490
-
491
- # Rerun the app
492
- st.rerun()
493
- else:
494
- st.warning("กรุณาเขียนเนื้อเรื่องก่อนกดส่ง | Please write something to continue the story.")
495
 
496
  with col2:
497
  # Feedback Display
 
104
  st.session_state.badges = []
105
  if 'should_reset' not in st.session_state:
106
  st.session_state.should_reset = False
 
 
107
 
108
  init_session_state()
109
 
 
111
  def clear_input():
112
  st.session_state.user_input = ""
113
 
114
+ # Callback function for submit button
115
+ def submit_story():
116
+ if st.session_state.text_input.strip():
117
+ # Add user's input to story
118
+ st.session_state.story.append({"role": "You", "content": st.session_state.text_input})
119
+
120
+ # Update achievements
121
+ update_achievements(st.session_state.text_input)
122
+
123
+ # Generate feedback
124
+ st.session_state.feedback = provide_feedback(st.session_state.text_input, st.session_state.level)
125
+
126
+ # Generate AI continuation
127
+ ai_response = generate_story_continuation(st.session_state.text_input, st.session_state.level)
128
+ st.session_state.story.append({"role": "AI", "content": ai_response})
129
+
130
+ # Clear the input by setting it to empty
131
+ st.session_state.text_input = ""
132
+
133
  def show_welcome_section():
134
  st.markdown("""
135
  <div class="welcome-header">
 
480
  </div>
481
  """, unsafe_allow_html=True)
482
 
483
+ # Text input with callback
484
+ st.text_area(
485
  "เขียนต่อจากเรื่องราว | Continue the story:",
486
  height=100,
487
+ key="text_input"
 
488
  )
489
 
490
+ # Submit button with callback
491
+ st.button(
492
+ "ส่งคำตอบ | Submit",
493
+ on_click=submit_story
494
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495
 
496
  with col2:
497
  # Feedback Display