ankanghosh commited on
Commit
58224c2
·
verified ·
1 Parent(s): 069e614

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -17,6 +17,10 @@ if 'submit_clicked' not in st.session_state:
17
  st.session_state.submit_clicked = False
18
  if 'init_time' not in st.session_state:
19
  st.session_state.init_time = None
 
 
 
 
20
 
21
  # THEN: Import your modules
22
  from rag_engine import process_query, load_model
@@ -61,13 +65,6 @@ div[data-baseweb="input"] > div {
61
  <div class="main-title">Indian Spiritual Texts Q&A</div>
62
  """, unsafe_allow_html=True)
63
 
64
- # Function to process query (only on explicit submission)
65
- def process_input():
66
- if st.session_state.query_input: # Only process if input is not empty
67
- st.session_state.last_query = st.session_state.query_input
68
- st.session_state.query_input = "" # Clear the input field
69
- st.session_state.submit_clicked = True
70
-
71
  # Handle initialization and success message timing
72
  if not st.session_state.initialized:
73
  # Show loading message
@@ -89,7 +86,7 @@ if not st.session_state.initialized:
89
 
90
  # Force rerun to start the timer for removing the message
91
  time.sleep(0.1) # Small delay to ensure message appears
92
- st.experimental_rerun()
93
 
94
  except Exception as e:
95
  init_message.error(f"Error initializing: {str(e)}")
@@ -100,22 +97,26 @@ elif st.session_state.init_time is not None:
100
  init_message.empty()
101
  st.session_state.init_time = None # Reset timer after clearing
102
 
103
- # Query input with callback ONLY for Enter key
 
 
 
 
 
 
 
104
  query = st.text_input(
105
  "Ask your question:",
106
  key="query_input",
107
- on_change=None # Don't trigger on any change, only on enter
108
  )
109
 
110
- # Only process enter key press here
111
- if query and query != st.session_state.get('previous_query', ''):
112
- st.session_state.previous_query = query
113
- st.session_state.last_query = query
114
  st.session_state.query_input = ""
115
- st.session_state.submit_clicked = True
116
- # Force refresh to update UI immediately
117
- st.experimental_rerun()
118
-
119
  # Display the current question if there is one
120
  if st.session_state.last_query:
121
  st.markdown("### Current Question:")
@@ -128,18 +129,17 @@ with col1:
128
  with col2:
129
  word_limit = st.slider("Word limit:", 50, 500, 200)
130
 
131
- # Process button - separate from the input field's behavior
132
  if st.button("Get Answer"):
133
- if query: # If there's text in the input field
134
- st.session_state.last_query = query
135
- st.session_state.query_input = ""
136
  st.session_state.submit_clicked = True
137
- # Force refresh
138
- st.experimental_rerun()
139
 
140
  # Only process the query if explicitly submitted
141
  if st.session_state.submit_clicked and st.session_state.last_query:
142
- # Reset the submit flag
143
  st.session_state.submit_clicked = False
144
 
145
  # Process the query
 
17
  st.session_state.submit_clicked = False
18
  if 'init_time' not in st.session_state:
19
  st.session_state.init_time = None
20
+ if 'should_clear_input' not in st.session_state:
21
+ st.session_state.should_clear_input = False
22
+ if 'current_input' not in st.session_state:
23
+ st.session_state.current_input = ""
24
 
25
  # THEN: Import your modules
26
  from rag_engine import process_query, load_model
 
65
  <div class="main-title">Indian Spiritual Texts Q&A</div>
66
  """, unsafe_allow_html=True)
67
 
 
 
 
 
 
 
 
68
  # Handle initialization and success message timing
69
  if not st.session_state.initialized:
70
  # Show loading message
 
86
 
87
  # Force rerun to start the timer for removing the message
88
  time.sleep(0.1) # Small delay to ensure message appears
89
+ st.rerun()
90
 
91
  except Exception as e:
92
  init_message.error(f"Error initializing: {str(e)}")
 
97
  init_message.empty()
98
  st.session_state.init_time = None # Reset timer after clearing
99
 
100
+ # Function to handle form submission (Enter key or button)
101
+ def handle_submit():
102
+ if st.session_state.query_input: # Only process if input is not empty
103
+ st.session_state.last_query = st.session_state.query_input
104
+ st.session_state.should_clear_input = True
105
+ st.session_state.submit_clicked = True
106
+
107
+ # Query input with callback for Enter key
108
  query = st.text_input(
109
  "Ask your question:",
110
  key="query_input",
111
+ on_change=handle_submit if not st.session_state.should_clear_input else None
112
  )
113
 
114
+ # Clear input if needed (after processing)
115
+ if st.session_state.should_clear_input:
116
+ # We can't modify the widget value directly, so we'll rerun with a blank value
117
+ st.session_state.should_clear_input = False
118
  st.session_state.query_input = ""
119
+
 
 
 
120
  # Display the current question if there is one
121
  if st.session_state.last_query:
122
  st.markdown("### Current Question:")
 
129
  with col2:
130
  word_limit = st.slider("Word limit:", 50, 500, 200)
131
 
132
+ # Process button
133
  if st.button("Get Answer"):
134
+ if st.session_state.query_input: # If there's text in the input field
135
+ st.session_state.last_query = st.session_state.query_input
136
+ st.session_state.should_clear_input = True
137
  st.session_state.submit_clicked = True
138
+ # We'll clear the input on the next run
 
139
 
140
  # Only process the query if explicitly submitted
141
  if st.session_state.submit_clicked and st.session_state.last_query:
142
+ # Reset the submit flag first
143
  st.session_state.submit_clicked = False
144
 
145
  # Process the query