IAMTFRMZA commited on
Commit
3432a8c
·
verified ·
1 Parent(s): a97081a
Files changed (1) hide show
  1. app.py +11 -3
app.py CHANGED
@@ -15,6 +15,7 @@ from langchain_community.document_loaders import UnstructuredExcelLoader
15
  from langchain.text_splitter import CharacterTextSplitter
16
  from langchain.embeddings import HuggingFaceEmbeddings
17
 
 
18
  os.environ["TOGETHER_API_KEY"] = os.getenv("TOGETHER_API_KEY")
19
 
20
 
@@ -111,6 +112,10 @@ def scrape_url(url):
111
  response.raise_for_status() # Ensure we notice bad responses
112
  soup = BeautifulSoup(response.content, 'html.parser')
113
  text = soup.get_text()
 
 
 
 
114
  # Save the text content to a file for processing
115
  text_file_path = "data/scraped_content.txt"
116
  with open(text_file_path, "w") as file:
@@ -119,6 +124,9 @@ def scrape_url(url):
119
  except requests.RequestException as e:
120
  st.error(f"Error fetching the URL: {e}")
121
  return None
 
 
 
122
 
123
 
124
  def process_document(path, input_query):
@@ -147,7 +155,7 @@ def main():
147
  with open(os.path.join(tmp_folder, file.name), 'wb') as f:
148
  f.write(file.getbuffer())
149
  st.success('Files successfully uploaded. Start prompting!')
150
-
151
  if 'chat_history' not in st.session_state:
152
  st.session_state.chat_history = []
153
 
@@ -157,10 +165,10 @@ def main():
157
  if st.form_submit_button("Ask") and user_query:
158
  response = process_document(tmp_folder, user_query)
159
  st.session_state.chat_history.append({"question": user_query, "answer": response})
160
-
161
  if st.button("Clear Chat History"):
162
  st.session_state.chat_history = []
163
-
164
  for chat in st.session_state.chat_history:
165
  st.markdown(f"**Q:** {chat['question']}")
166
  st.markdown(f"**A:** {chat['answer']}")
 
15
  from langchain.text_splitter import CharacterTextSplitter
16
  from langchain.embeddings import HuggingFaceEmbeddings
17
 
18
+ # Set API key environment variable
19
  os.environ["TOGETHER_API_KEY"] = os.getenv("TOGETHER_API_KEY")
20
 
21
 
 
112
  response.raise_for_status() # Ensure we notice bad responses
113
  soup = BeautifulSoup(response.content, 'html.parser')
114
  text = soup.get_text()
115
+
116
+ # Ensure the data directory exists
117
+ os.makedirs("data", exist_ok=True)
118
+
119
  # Save the text content to a file for processing
120
  text_file_path = "data/scraped_content.txt"
121
  with open(text_file_path, "w") as file:
 
124
  except requests.RequestException as e:
125
  st.error(f"Error fetching the URL: {e}")
126
  return None
127
+ except Exception as e:
128
+ st.error(f"An unexpected error occurred: {e}")
129
+ return None
130
 
131
 
132
  def process_document(path, input_query):
 
155
  with open(os.path.join(tmp_folder, file.name), 'wb') as f:
156
  f.write(file.getbuffer())
157
  st.success('Files successfully uploaded. Start prompting!')
158
+
159
  if 'chat_history' not in st.session_state:
160
  st.session_state.chat_history = []
161
 
 
165
  if st.form_submit_button("Ask") and user_query:
166
  response = process_document(tmp_folder, user_query)
167
  st.session_state.chat_history.append({"question": user_query, "answer": response})
168
+
169
  if st.button("Clear Chat History"):
170
  st.session_state.chat_history = []
171
+
172
  for chat in st.session_state.chat_history:
173
  st.markdown(f"**Q:** {chat['question']}")
174
  st.markdown(f"**A:** {chat['answer']}")