DrishtiSharma commited on
Commit
e32fbfc
·
verified ·
1 Parent(s): ae9100f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -150,6 +150,10 @@ class DocumentRAG:
150
  except Exception as e:
151
  return history + [("System", f"Error: {str(e)}")]
152
 
 
 
 
 
153
  # Streamlit UI
154
  st.title("Document Analyzer and Podcast Generator")
155
 
@@ -159,13 +163,6 @@ if "OPENAI_API_KEY" not in os.environ or not os.getenv("OPENAI_API_KEY"):
159
  else:
160
  st.success("API Key successfully loaded from environment variable.")
161
 
162
- # Initialize RAG system
163
- try:
164
- rag_system = DocumentRAG()
165
- except ValueError as e:
166
- st.error(str(e))
167
- st.stop()
168
-
169
  # File upload
170
  st.subheader("Step 1: Upload Documents")
171
  uploaded_files = st.file_uploader("Upload files (PDF, TXT, CSV)", accept_multiple_files=True)
@@ -173,26 +170,22 @@ uploaded_files = st.file_uploader("Upload files (PDF, TXT, CSV)", accept_multipl
173
  if st.button("Process Documents"):
174
  if uploaded_files:
175
  # Process the uploaded files
176
- result = rag_system.process_documents(uploaded_files)
177
-
178
- # Ensure that result is a string and display appropriately
179
- if isinstance(result, str):
180
- if "successfully" in result:
181
- st.success(result)
182
- else:
183
- st.error(result)
184
  else:
185
- st.error("An unexpected error occurred during document processing.")
186
  else:
187
  st.warning("No files uploaded.")
188
 
189
  # Document Q&A
190
  st.subheader("Step 2: Ask Questions")
191
- if rag_system.qa_chain:
192
  history = []
193
  user_question = st.text_input("Ask a question:")
194
  if st.button("Submit Question"):
195
- history = rag_system.handle_query(user_question, history)
 
196
  for question, answer in history:
197
  st.chat_message("user").write(question)
198
  st.chat_message("assistant").write(answer)
 
150
  except Exception as e:
151
  return history + [("System", f"Error: {str(e)}")]
152
 
153
+ # Initialize RAG system in session state
154
+ if "rag_system" not in st.session_state:
155
+ st.session_state.rag_system = DocumentRAG()
156
+
157
  # Streamlit UI
158
  st.title("Document Analyzer and Podcast Generator")
159
 
 
163
  else:
164
  st.success("API Key successfully loaded from environment variable.")
165
 
 
 
 
 
 
 
 
166
  # File upload
167
  st.subheader("Step 1: Upload Documents")
168
  uploaded_files = st.file_uploader("Upload files (PDF, TXT, CSV)", accept_multiple_files=True)
 
170
  if st.button("Process Documents"):
171
  if uploaded_files:
172
  # Process the uploaded files
173
+ result = st.session_state.rag_system.process_documents(uploaded_files)
174
+ if "successfully" in result:
175
+ st.success(result)
 
 
 
 
 
176
  else:
177
+ st.error(result)
178
  else:
179
  st.warning("No files uploaded.")
180
 
181
  # Document Q&A
182
  st.subheader("Step 2: Ask Questions")
183
+ if st.session_state.rag_system.qa_chain:
184
  history = []
185
  user_question = st.text_input("Ask a question:")
186
  if st.button("Submit Question"):
187
+ # Handle the user query
188
+ history = st.session_state.rag_system.handle_query(user_question, history)
189
  for question, answer in history:
190
  st.chat_message("user").write(question)
191
  st.chat_message("assistant").write(answer)