AlirezaHSZ commited on
Commit
2069c31
·
verified ·
1 Parent(s): e830462

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -39,8 +39,8 @@ def get_vector_store(chunks):
39
  model="models/embedding-001") # type: ignore
40
  vector_store = FAISS.from_texts(chunks, embedding=embeddings)
41
  vector_store.save_local("faiss_index")
42
- except (GoogleAPIError, InvalidArgument) as e:
43
- st.error("Error processing embeddings. Please try again in a minute.")
44
 
45
  # Function to get conversational chain
46
  def get_conversational_chain():
@@ -60,8 +60,8 @@ def get_conversational_chain():
60
  input_variables=["context", "question"])
61
  chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
62
  return chain
63
- except (GoogleAPIError, InvalidArgument) as e:
64
- st.error("Error creating conversational chain. Please try again in a minute.")
65
 
66
  # Function to clear chat history
67
  def clear_chat_history():
@@ -79,8 +79,8 @@ def user_input(user_question):
79
  response = chain(
80
  {"input_documents": docs, "question": user_question}, return_only_outputs=True)
81
  return response
82
- except (GoogleAPIError, InvalidArgument) as e:
83
- st.error("Error processing request. Please try again in a minute.")
84
 
85
  # Main function to run the Streamlit app
86
  def main():
@@ -136,11 +136,14 @@ def main():
136
  "Upload your PDF files", accept_multiple_files=True)
137
  if st.button("Submit & Process", key="submit_button"):
138
  if pdf_docs:
139
- with st.spinner("Processing..."):
140
- raw_text = get_pdf_text(pdf_docs)
141
- text_chunks = get_text_chunks(raw_text)
142
- get_vector_store(text_chunks)
143
- st.success("Processing completed!")
 
 
 
144
  else:
145
  st.error("Please upload at least one PDF file.")
146
 
@@ -167,16 +170,17 @@ def main():
167
 
168
  # Generate bot response
169
  if st.session_state.messages[-1]["role"] != "assistant":
170
- with st.chat_message("assistant"):
171
- with st.spinner("Thinking..."):
172
- response = user_input(prompt)
173
- if response:
174
- full_response = ''.join(response['output_text'])
175
- st.write(full_response)
176
- message = {"role": "assistant", "content": full_response}
177
- st.session_state.messages.append(message)
178
- else:
179
- st.error("Please try again in a minute.")
 
180
 
181
  if __name__ == "__main__":
182
  main()
 
39
  model="models/embedding-001") # type: ignore
40
  vector_store = FAISS.from_texts(chunks, embedding=embeddings)
41
  vector_store.save_local("faiss_index")
42
+ except (GoogleAPIError, InvalidArgument):
43
+ raise RuntimeError("Error processing embeddings. Please try again in a minute.")
44
 
45
  # Function to get conversational chain
46
  def get_conversational_chain():
 
60
  input_variables=["context", "question"])
61
  chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
62
  return chain
63
+ except (GoogleAPIError, InvalidArgument):
64
+ raise RuntimeError("Error creating conversational chain. Please try again in a minute.")
65
 
66
  # Function to clear chat history
67
  def clear_chat_history():
 
79
  response = chain(
80
  {"input_documents": docs, "question": user_question}, return_only_outputs=True)
81
  return response
82
+ except (GoogleAPIError, InvalidArgument):
83
+ raise RuntimeError("Error processing request. Please try again in a minute.")
84
 
85
  # Main function to run the Streamlit app
86
  def main():
 
136
  "Upload your PDF files", accept_multiple_files=True)
137
  if st.button("Submit & Process", key="submit_button"):
138
  if pdf_docs:
139
+ try:
140
+ with st.spinner("Processing..."):
141
+ raw_text = get_pdf_text(pdf_docs)
142
+ text_chunks = get_text_chunks(raw_text)
143
+ get_vector_store(text_chunks)
144
+ st.success("Processing completed!")
145
+ except RuntimeError as e:
146
+ st.error(str(e))
147
  else:
148
  st.error("Please upload at least one PDF file.")
149
 
 
170
 
171
  # Generate bot response
172
  if st.session_state.messages[-1]["role"] != "assistant":
173
+ try:
174
+ with st.chat_message("assistant"):
175
+ with st.spinner("Thinking..."):
176
+ response = user_input(prompt)
177
+ if response:
178
+ full_response = ''.join(response['output_text'])
179
+ st.write(full_response)
180
+ message = {"role": "assistant", "content": full_response}
181
+ st.session_state.messages.append(message)
182
+ except RuntimeError as e:
183
+ st.error(str(e))
184
 
185
  if __name__ == "__main__":
186
  main()