ankanghosh commited on
Commit
23daf07
·
verified ·
1 Parent(s): 3cf3e00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -31
app.py CHANGED
@@ -1,80 +1,112 @@
1
  import streamlit as st
2
  import time
3
 
4
- st.set_page_config(page_title="Indian Spiritual RAG")
 
5
 
6
- # Initialize session state variables
7
  if 'initialized' not in st.session_state:
8
  st.session_state.initialized = False
 
 
 
 
9
  if 'last_query' not in st.session_state:
10
  st.session_state.last_query = ""
11
  if 'submit_clicked' not in st.session_state:
12
  st.session_state.submit_clicked = False
13
- if 'query_input' not in st.session_state:
14
- st.session_state.query_input = ""
15
 
16
- # Import other modules
17
  from rag_engine import process_query, load_model
18
  from utils import setup_all_auth
19
 
20
- # Custom CSS for styling
 
 
 
21
  st.markdown("""
22
  <style>
 
23
  .main-title {
24
  font-size: 2.5rem;
25
  color: #FF5722;
26
  text-align: center;
27
  margin-bottom: 1rem;
28
  }
 
 
 
 
 
 
 
 
 
 
 
 
29
  .stButton>button {
30
- background-color: #4CAF50 !important;
31
  color: white !important;
32
  border-radius: 8px !important;
33
- font-weight: bold !important;
 
 
 
34
  }
35
- div[data-baseweb="input"] {
36
- border: 3px solid #4CAF50 !important;
37
- border-radius: 8px !important;
38
- transition: border-color 0.3s ease-in-out;
39
  }
40
- div[data-baseweb="input"]:focus-within {
41
- border: 3px solid #FF5722 !important;
 
 
 
42
  }
 
43
  </style>
44
- <div class="main-title">Indian Spiritual Texts Q&A</div>
45
  """, unsafe_allow_html=True)
46
 
47
- # Initialization logic
48
  if not st.session_state.initialized:
49
- init_message = st.empty()
50
  init_message.info("Hang in there! We are setting the system up for you. 😊")
 
51
  try:
52
  setup_all_auth()
53
  load_model()
 
54
  st.session_state.initialized = True
55
- time.sleep(0.5)
 
56
  init_message.success("System initialized successfully!")
57
- time.sleep(1)
58
- init_message.empty()
59
  except Exception as e:
60
  init_message.error(f"Error initializing: {str(e)}")
61
 
 
 
 
 
 
 
62
  # Function to handle form submission
63
  def handle_form_submit():
64
- st.session_state.last_query = st.session_state.query_input
65
- st.session_state.submit_clicked = True
66
- st.session_state.query_input = "" # ✅ Clears input field on submission
 
 
67
 
68
  # Form for user input
69
  with st.form(key="query_form"):
70
- query = st.text_input(
71
- "Ask your question:",
72
- key="query_input",
73
- value=st.session_state.query_input
74
- )
75
  submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
76
 
77
- # Display last question
78
  if st.session_state.last_query:
79
  st.markdown("### Current Question:")
80
  st.info(st.session_state.last_query)
@@ -86,9 +118,10 @@ with col1:
86
  with col2:
87
  word_limit = st.slider("Word limit:", 50, 500, 200)
88
 
89
- # Process query when submitted
90
  if st.session_state.submit_clicked and st.session_state.last_query:
91
  st.session_state.submit_clicked = False
 
92
  with st.spinner("Processing your question..."):
93
  try:
94
  result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
@@ -100,7 +133,7 @@ if st.session_state.submit_clicked and st.session_state.last_query:
100
  except Exception as e:
101
  st.error(f"Error processing query: {str(e)}")
102
 
103
- # Footer
104
  st.markdown("---")
105
  st.markdown("""
106
  ### About this app
 
1
  import streamlit as st
2
  import time
3
 
4
+ # FIRST: Set page config before ANY other Streamlit command
5
+ st.set_page_config(page_title="Indian Spirituality Q&A")
6
 
7
+ # Initialize ALL session state variables at the start
8
  if 'initialized' not in st.session_state:
9
  st.session_state.initialized = False
10
+ if 'model' not in st.session_state:
11
+ st.session_state.model = None
12
+ if 'tokenizer' not in st.session_state:
13
+ st.session_state.tokenizer = None
14
  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 '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
23
  from utils import setup_all_auth
24
 
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 */
32
  .main-title {
33
  font-size: 2.5rem;
34
  color: #FF5722;
35
  text-align: center;
36
  margin-bottom: 1rem;
37
  }
38
+
39
+ /* Input field: Green by default, Red when editing */
40
+ div[data-baseweb="input"] {
41
+ border: 2px solid #81C784 !important;
42
+ border-radius: 8px !important;
43
+ }
44
+ div[data-baseweb="input"]:focus-within {
45
+ border: 2px solid #FF5722 !important;
46
+ border-radius: 8px !important;
47
+ }
48
+
49
+ /* Custom Button: Green with Hover */
50
  .stButton>button {
51
+ background-color: #81C784 !important;
52
  color: white !important;
53
  border-radius: 8px !important;
54
+ border: none !important;
55
+ padding: 0.6rem 1.2rem !important;
56
+ font-size: 1rem !important;
57
+ transition: 0.3s ease-in-out;
58
  }
59
+ .stButton>button:hover {
60
+ background-color: #66BB6A !important;
 
 
61
  }
62
+
63
+ /* Remove orange focus outline */
64
+ .stButton>button:focus {
65
+ outline: none !important;
66
+ box-shadow: 0px 0px 8px rgba(129, 199, 132, 0.8) !important;
67
  }
68
+
69
  </style>
70
+ <div class="main-title">Indian Spirituality Q&A</div>
71
  """, unsafe_allow_html=True)
72
 
73
+ # Handle initialization and success message timing
74
  if not st.session_state.initialized:
 
75
  init_message.info("Hang in there! We are setting the system up for you. 😊")
76
+
77
  try:
78
  setup_all_auth()
79
  load_model()
80
+
81
  st.session_state.initialized = True
82
+ st.session_state.init_time = time.time()
83
+
84
  init_message.success("System initialized successfully!")
85
+ time.sleep(0.1) # Ensure message appears before rerun
86
+ st.rerun()
87
  except Exception as e:
88
  init_message.error(f"Error initializing: {str(e)}")
89
 
90
+ # Remove success message after 2 seconds
91
+ elif st.session_state.init_time is not None:
92
+ if time.time() - st.session_state.init_time >= 2.0:
93
+ init_message.empty()
94
+ st.session_state.init_time = None
95
+
96
  # Function to handle form submission
97
  def handle_form_submit():
98
+ if st.session_state.query_input:
99
+ st.session_state.last_query = st.session_state.query_input
100
+ st.session_state.submit_clicked = True
101
+ st.session_state.query_input = "" # Clear input field
102
+ st.rerun()
103
 
104
  # Form for user input
105
  with st.form(key="query_form"):
106
+ query = st.text_input("Ask your question:", key="query_input", value="", placeholder="Press enter to submit question")
 
 
 
 
107
  submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
108
 
109
+ # Display last submitted question
110
  if st.session_state.last_query:
111
  st.markdown("### Current Question:")
112
  st.info(st.session_state.last_query)
 
118
  with col2:
119
  word_limit = st.slider("Word limit:", 50, 500, 200)
120
 
121
+ # Process query on submission
122
  if st.session_state.submit_clicked and st.session_state.last_query:
123
  st.session_state.submit_clicked = False
124
+
125
  with st.spinner("Processing your question..."):
126
  try:
127
  result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
 
133
  except Exception as e:
134
  st.error(f"Error processing query: {str(e)}")
135
 
136
+ # About Section
137
  st.markdown("---")
138
  st.markdown("""
139
  ### About this app