Shreyas094 commited on
Commit
d1b3556
·
verified ·
1 Parent(s): eff0811

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -280,13 +280,17 @@ def get_web_search_database():
280
  temp_dir = tempfile.mkdtemp()
281
 
282
  try:
283
- # Initialize with an empty list of documents
284
- database = WebSearchFAISS.from_documents([], embed)
285
- logging.info("Successfully initialized empty WebSearchFAISS database")
 
 
 
 
286
  except Exception as e:
287
  logging.error(f"Error initializing WebSearchFAISS: {str(e)}", exc_info=True)
288
  # If initialization fails, create an empty database
289
- database = WebSearchFAISS(embed, None, {}, {}, None)
290
  logging.info("Created empty WebSearchFAISS database manually")
291
 
292
  return database, temp_dir
@@ -306,7 +310,7 @@ def duckduckgo_search(query):
306
  documents = []
307
  for result in results:
308
  content = f"{result['title']}\n{result['body']}"
309
- doc = WebSearchDocument(page_content=content, metadata={"source": result['href']})
310
  documents.append(doc)
311
 
312
  logging.info(f"Number of documents created: {len(documents)}")
@@ -317,7 +321,7 @@ def duckduckgo_search(query):
317
  except Exception as e:
318
  logging.error(f"Error adding documents to database: {str(e)}", exc_info=True)
319
  # If adding documents fails, create a new database with these documents
320
- database = WebSearchFAISS.from_documents(documents, get_embeddings())
321
  logging.info("Created new WebSearchFAISS database with search results")
322
  return database, temp_dir, results
323
  except Exception as e:
 
280
  temp_dir = tempfile.mkdtemp()
281
 
282
  try:
283
+ # Create a dummy document to initialize the database
284
+ dummy_doc = Document(page_content="Dummy content", metadata={"source": "dummy"})
285
+ database = FAISS.from_documents([dummy_doc], embed)
286
+ logging.info("Successfully initialized WebSearchFAISS database with dummy document")
287
+ # Remove the dummy document
288
+ database.delete(["dummy"])
289
+ logging.info("Removed dummy document from database")
290
  except Exception as e:
291
  logging.error(f"Error initializing WebSearchFAISS: {str(e)}", exc_info=True)
292
  # If initialization fails, create an empty database
293
+ database = FAISS(embed, None, {}, {}, None)
294
  logging.info("Created empty WebSearchFAISS database manually")
295
 
296
  return database, temp_dir
 
310
  documents = []
311
  for result in results:
312
  content = f"{result['title']}\n{result['body']}"
313
+ doc = Document(page_content=content, metadata={"source": result['href']})
314
  documents.append(doc)
315
 
316
  logging.info(f"Number of documents created: {len(documents)}")
 
321
  except Exception as e:
322
  logging.error(f"Error adding documents to database: {str(e)}", exc_info=True)
323
  # If adding documents fails, create a new database with these documents
324
+ database = FAISS.from_documents(documents, get_embeddings())
325
  logging.info("Created new WebSearchFAISS database with search results")
326
  return database, temp_dir, results
327
  except Exception as e: