absiitr commited on
Commit
984f2ea
·
verified ·
1 Parent(s): 37b4016

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -16
app.py CHANGED
@@ -137,6 +137,10 @@ if "uploaded_file_name" not in st.session_state:
137
  if "uploader_key" not in st.session_state:
138
  st.session_state.uploader_key = 0
139
 
 
 
 
 
140
  # ---------------- FUNCTIONS ----------------
141
  def clear_chat_history():
142
  st.session_state.chat = []
@@ -279,23 +283,25 @@ if uploaded and uploaded.name != st.session_state.uploaded_file_name:
279
 
280
  st.rerun()
281
 
282
- # Chat Input
283
  disabled_input = st.session_state.uploaded_file_name is None or client is None
284
- question = st.text_input(
285
- "Ask a question about the loaded PDF:",
286
- key="question_input",
287
- disabled=disabled_input,
288
- label_visibility="collapsed",
289
- placeholder="Type your question here..."
290
- )
291
 
292
- col1, col2 = st.columns([6, 1])
293
- with col1:
294
- pass # Empty column for spacing
295
- with col2:
296
- send_clicked = st.button("Send", disabled=disabled_input, use_container_width=True)
 
 
 
 
 
 
 
 
297
 
298
- if send_clicked and question:
 
299
  # Add user query to chat history
300
  st.session_state.chat.append(("user", question))
301
 
@@ -309,8 +315,7 @@ if send_clicked and question:
309
  else:
310
  st.session_state.chat.append(("bot", f"🔴 **Error:** {error}"))
311
 
312
- # Clear the input field
313
- st.session_state.question_input = ""
314
  st.rerun()
315
 
316
  # Display Chat History in scrollable container
 
137
  if "uploader_key" not in st.session_state:
138
  st.session_state.uploader_key = 0
139
 
140
+ # Initialize question key to track input state
141
+ if "question_submitted" not in st.session_state:
142
+ st.session_state.question_submitted = ""
143
+
144
  # ---------------- FUNCTIONS ----------------
145
  def clear_chat_history():
146
  st.session_state.chat = []
 
283
 
284
  st.rerun()
285
 
286
+ # Chat Input with form for better control
287
  disabled_input = st.session_state.uploaded_file_name is None or client is None
 
 
 
 
 
 
 
288
 
289
+ # Use a form to handle input submission and clearing
290
+ with st.form(key="chat_form", clear_on_submit=True):
291
+ col1, col2 = st.columns([6, 1])
292
+ with col1:
293
+ question = st.text_input(
294
+ "Ask a question about the loaded PDF:",
295
+ key="question_input",
296
+ disabled=disabled_input,
297
+ label_visibility="collapsed",
298
+ placeholder="Type your question here..."
299
+ )
300
+ with col2:
301
+ submit_button = st.form_submit_button("Send", disabled=disabled_input, use_container_width=True)
302
 
303
+ # Handle form submission
304
+ if submit_button and question:
305
  # Add user query to chat history
306
  st.session_state.chat.append(("user", question))
307
 
 
315
  else:
316
  st.session_state.chat.append(("bot", f"🔴 **Error:** {error}"))
317
 
318
+ # The form already clears the input (clear_on_submit=True)
 
319
  st.rerun()
320
 
321
  # Display Chat History in scrollable container