ankanghosh commited on
Commit
069e614
·
verified ·
1 Parent(s): 6aa479a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -43
app.py CHANGED
@@ -15,10 +15,8 @@ if 'last_query' not in st.session_state:
15
  st.session_state.last_query = ""
16
  if 'submit_clicked' not in st.session_state:
17
  st.session_state.submit_clicked = False
18
- if 'success_shown' not in st.session_state:
19
- st.session_state.success_shown = False
20
- if 'init_timestamp' not in st.session_state:
21
- st.session_state.init_timestamp = None
22
 
23
  # THEN: Import your modules
24
  from rag_engine import process_query, load_model
@@ -27,7 +25,7 @@ from utils import setup_all_auth
27
  # Create a placeholder for our initialization message
28
  init_message = st.empty()
29
 
30
- # Custom styling without any JavaScript (we'll use pure HTML)
31
  st.markdown("""
32
  <style>
33
  .main-title {
@@ -63,6 +61,13 @@ div[data-baseweb="input"] > div {
63
  <div class="main-title">Indian Spiritual Texts Q&A</div>
64
  """, unsafe_allow_html=True)
65
 
 
 
 
 
 
 
 
66
  # Handle initialization and success message timing
67
  if not st.session_state.initialized:
68
  # Show loading message
@@ -72,39 +77,45 @@ if not st.session_state.initialized:
72
  # Setup authentication
73
  setup_all_auth()
74
 
75
- # Load the model (this will store it in st.session_state)
76
  load_model()
77
 
78
- # Mark as initialized and set timestamp
79
  st.session_state.initialized = True
80
- st.session_state.success_shown = True
81
- st.session_state.init_timestamp = time.time()
82
 
83
  # Show success message
84
  init_message.success("System initialized successfully!")
 
 
 
 
 
85
  except Exception as e:
86
  init_message.error(f"Error initializing: {str(e)}")
87
- else:
88
- # Check if we need to hide the success message
89
- if st.session_state.success_shown and st.session_state.init_timestamp is not None:
90
- time_elapsed = time.time() - st.session_state.init_timestamp
91
- if time_elapsed >= 2.0:
92
- init_message.empty()
93
- st.session_state.success_shown = False
94
-
95
- # Function to process when enter is pressed or button is clicked
96
- def process_input():
97
- st.session_state.last_query = st.session_state.query_input
98
- st.session_state.query_input = ""
99
- st.session_state.submit_clicked = True
100
 
101
- # Query input with callback for Enter key
102
  query = st.text_input(
103
  "Ask your question:",
104
  key="query_input",
105
- on_change=process_input
106
  )
107
 
 
 
 
 
 
 
 
 
 
108
  # Display the current question if there is one
109
  if st.session_state.last_query:
110
  st.markdown("### Current Question:")
@@ -117,25 +128,31 @@ with col1:
117
  with col2:
118
  word_limit = st.slider("Word limit:", 50, 500, 200)
119
 
120
- # Process button
121
- if st.button("Get Answer") or st.session_state.submit_clicked:
122
- if st.session_state.last_query:
123
- # Reset the submit flag
124
- st.session_state.submit_clicked = False
125
-
126
- # Single processing spinner for the entire operation
127
- with st.spinner("Processing your question..."):
128
- try:
129
- result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
130
- st.subheader("Answer:")
131
- st.write(result["answer_with_rag"])
132
- st.subheader("Sources:")
133
- for citation in result["citations"].split("\n"):
134
- st.write(citation)
135
- except Exception as e:
136
- st.error(f"Error processing query: {str(e)}")
137
- else:
138
- st.warning("Please enter a question first.")
 
 
 
 
 
 
139
 
140
  # Add helpful information
141
  st.markdown("---")
 
15
  st.session_state.last_query = ""
16
  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
 
25
  # Create a placeholder for our initialization message
26
  init_message = st.empty()
27
 
28
+ # Custom styling (pure CSS)
29
  st.markdown("""
30
  <style>
31
  .main-title {
 
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
 
77
  # Setup authentication
78
  setup_all_auth()
79
 
80
+ # Load the model
81
  load_model()
82
 
83
+ # Mark as initialized and record time
84
  st.session_state.initialized = True
85
+ st.session_state.init_time = time.time()
 
86
 
87
  # Show success message
88
  init_message.success("System initialized successfully!")
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)}")
96
+ # Check if we need to hide the success message (after initialization)
97
+ elif st.session_state.init_time is not None:
98
+ elapsed_time = time.time() - st.session_state.init_time
99
+ if elapsed_time >= 2.0:
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
  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
146
+ with st.spinner("Processing your question..."):
147
+ try:
148
+ result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
149
+ st.subheader("Answer:")
150
+ st.write(result["answer_with_rag"])
151
+ st.subheader("Sources:")
152
+ for citation in result["citations"].split("\n"):
153
+ st.write(citation)
154
+ except Exception as e:
155
+ st.error(f"Error processing query: {str(e)}")
156
 
157
  # Add helpful information
158
  st.markdown("---")