MahatirTusher commited on
Commit
9d9024e
Β·
verified Β·
1 Parent(s): e471a32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -12
app.py CHANGED
@@ -100,12 +100,22 @@ st.markdown("""
100
  # Display large logo at the top of the main page
101
  st.image("https://i.postimg.cc/2j0QWF3Z/Removal-575.png", width=390)
102
 
 
 
103
 
104
  # Initialize session state
105
  if "index_created" not in st.session_state:
106
  st.session_state.index_created = False
107
  if "url_content" not in st.session_state:
108
  st.session_state.url_content = None
 
 
 
 
 
 
 
 
109
 
110
  # Sidebar for URL input
111
  with st.sidebar:
@@ -187,24 +197,29 @@ if process_url_clicked:
187
  st.text(f"Split into {len(docs)} document chunks.")
188
 
189
  st.text("Embedding Vector Started Building...βœ…βœ…βœ…")
190
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
191
- vectorstore_openai = FAISS.from_documents(docs, embeddings)
192
 
193
  faiss_index_path = "faiss_index"
194
- save_faiss_index(vectorstore_openai, faiss_index_path)
 
195
  st.session_state.index_created = True
196
  st.text("FAISS index saved successfully! βœ…βœ…βœ…")
197
  time.sleep(2)
198
  except Exception as e:
199
  st.error(f"Error processing URL: {str(e)}")
200
 
201
- # Display summary if content is available
202
- if st.session_state.url_content:
 
 
 
 
 
 
203
  with main_container:
204
  st.header("Summary of the URL Content")
205
- with st.spinner("Generating summary..."):
206
- summary = summarize_content(st.session_state.url_content, llm)
207
- st.write(summary)
208
 
209
  # Query input with Ask button
210
  with main_container:
@@ -214,16 +229,14 @@ with main_container:
214
 
215
  if ask_clicked and query:
216
  with main_container:
217
- if not st.session_state.index_created or not os.path.exists("faiss_index"):
218
  st.error("No FAISS index found. Please process a URL first.")
219
  else:
220
  with st.spinner("Processing your question..."):
221
  try:
222
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
223
- vectorstore = load_faiss_index("faiss_index", embeddings)
224
  chain = RetrievalQAWithSourcesChain.from_llm(
225
  llm=llm,
226
- retriever=vectorstore.as_retriever(),
227
  question_prompt=qa_prompt
228
  )
229
  result = chain({"question": query}, return_only_outputs=True)
 
100
  # Display large logo at the top of the main page
101
  st.image("https://i.postimg.cc/2j0QWF3Z/Removal-575.png", width=390)
102
 
103
+ # Set Streamlit app title
104
+ st.title("WebChatter πŸ’¬")
105
 
106
  # Initialize session state
107
  if "index_created" not in st.session_state:
108
  st.session_state.index_created = False
109
  if "url_content" not in st.session_state:
110
  st.session_state.url_content = None
111
+ if "vectorstore" not in st.session_state:
112
+ st.session_state.vectorstore = None
113
+ if "summary" not in st.session_state:
114
+ st.session_state.summary = None
115
+
116
+ # Initialize embeddings once at the start
117
+ if "embeddings" not in st.session_state:
118
+ st.session_state.embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
119
 
120
  # Sidebar for URL input
121
  with st.sidebar:
 
197
  st.text(f"Split into {len(docs)} document chunks.")
198
 
199
  st.text("Embedding Vector Started Building...βœ…βœ…βœ…")
200
+ embeddings = st.session_state.embeddings
201
+ vectorstore = FAISS.from_documents(docs, embeddings)
202
 
203
  faiss_index_path = "faiss_index"
204
+ save_faiss_index(vectorstore, faiss_index_path)
205
+ st.session_state.vectorstore = vectorstore # Cache the vectorstore
206
  st.session_state.index_created = True
207
  st.text("FAISS index saved successfully! βœ…βœ…βœ…")
208
  time.sleep(2)
209
  except Exception as e:
210
  st.error(f"Error processing URL: {str(e)}")
211
 
212
+ # Summary button
213
+ with main_container:
214
+ if st.session_state.url_content and st.button("Generate Summary"):
215
+ with st.spinner("Generating summary..."):
216
+ st.session_state.summary = summarize_content(st.session_state.url_content, llm)
217
+
218
+ # Display summary if generated
219
+ if st.session_state.summary:
220
  with main_container:
221
  st.header("Summary of the URL Content")
222
+ st.write(st.session_state.summary)
 
 
223
 
224
  # Query input with Ask button
225
  with main_container:
 
229
 
230
  if ask_clicked and query:
231
  with main_container:
232
+ if not st.session_state.index_created or st.session_state.vectorstore is None:
233
  st.error("No FAISS index found. Please process a URL first.")
234
  else:
235
  with st.spinner("Processing your question..."):
236
  try:
 
 
237
  chain = RetrievalQAWithSourcesChain.from_llm(
238
  llm=llm,
239
+ retriever=st.session_state.vectorstore.as_retriever(search_kwargs={"k": 2}), # Limit to top 2 documents
240
  question_prompt=qa_prompt
241
  )
242
  result = chain({"question": query}, return_only_outputs=True)