ZarinT commited on
Commit
75b6d0b
·
verified ·
1 Parent(s): 543e566

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -21
app.py CHANGED
@@ -85,6 +85,17 @@ def load_environment():
85
  load_dotenv()
86
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
87
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  def convert_pdf_to_xml(pdf_file, xml_path):
90
  os.makedirs("temp", exist_ok=True)
@@ -243,31 +254,26 @@ def handle_user_query(query):
243
  def main():
244
  load_environment()
245
 
 
 
246
  if "chat_ready" not in st.session_state:
247
  st.session_state.chat_ready = False
248
  if "chat_history" not in st.session_state:
249
  st.session_state.chat_history = []
250
- if "vectorstore" not in st.session_state:
251
- st.session_state.vectorstore = None
252
-
253
- st.header("Chat with MODTRAN Documents :satellite:")
254
- user_question = st.text_input("Ask a question about your uploaded files:")
255
-
256
- with st.sidebar:
257
- uploaded_files = st.file_uploader("Upload PDF, HTML, or MODTRAN output files:", accept_multiple_files=True)
258
- if st.button("Process") and uploaded_files:
259
- with st.spinner("Processing..."):
260
- raw_text = get_uploaded_text(uploaded_files)
261
- text_chunks = get_text_chunks(raw_text)
262
- st.session_state.vectorstore = get_vectorstore(text_chunks)
263
- set_global_vectorstore(st.session_state.vectorstore)
264
- st.session_state.chat_ready = True
265
- st.success("Files processed successfully!")
266
-
267
- if st.session_state.chat_ready and user_question:
268
- # Restore the global vectorstore reference
269
- set_global_vectorstore(st.session_state.vectorstore)
270
- response = handle_user_query(user_question)
271
  st.session_state.chat_history.append({"user": user_question, "bot": response})
272
 
273
  for chat in st.session_state.chat_history:
 
85
  load_dotenv()
86
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
87
 
88
+ def preload_modtran_document():
89
+ with open("MODTRAN 6 User's Manual.pdf", "rb") as f:
90
+ file_obj = io.BytesIO(f.read())
91
+ file_obj.name = "MODTRAN 6 User's Manual.pdf"
92
+ uploaded_files = [file_obj]
93
+
94
+ raw_text = get_uploaded_text(uploaded_files)
95
+ text_chunks = get_text_chunks(raw_text)
96
+ vectorstore = get_vectorstore(text_chunks)
97
+ set_global_vectorstore(vectorstore)
98
+ st.session_state.agent = initialize_chatbot_agent()
99
 
100
  def convert_pdf_to_xml(pdf_file, xml_path):
101
  os.makedirs("temp", exist_ok=True)
 
254
  def main():
255
  load_environment()
256
 
257
+ if "agent" not in st.session_state:
258
+ st.session_state.agent = None
259
  if "chat_ready" not in st.session_state:
260
  st.session_state.chat_ready = False
261
  if "chat_history" not in st.session_state:
262
  st.session_state.chat_history = []
263
+
264
+ st.header("Chat with MODTRAN Documents 📄🤖")
265
+
266
+ # Preload the document once when app starts
267
+ if not st.session_state.chat_ready:
268
+ with st.spinner("Loading MODTRAN document..."):
269
+ preload_modtran_document()
270
+ st.session_state.chat_ready = True
271
+ st.success("MODTRAN User Manual loaded successfully!")
272
+
273
+ user_question = st.text_input("Ask your question:")
274
+
275
+ if st.session_state.agent and user_question:
276
+ response = handle_user_query(user_question, st.session_state.agent)
 
 
 
 
 
 
 
277
  st.session_state.chat_history.append({"user": user_question, "bot": response})
278
 
279
  for chat in st.session_state.chat_history: