Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -279,17 +279,15 @@ def get_web_search_database():
|
|
279 |
embed = get_embeddings()
|
280 |
temp_dir = tempfile.mkdtemp()
|
281 |
|
282 |
-
# Create a dummy document to initialize the database
|
283 |
-
dummy_doc = WebSearchDocument(page_content="Dummy content", metadata={"source": "dummy"})
|
284 |
-
|
285 |
try:
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
except Exception as e:
|
290 |
logging.error(f"Error initializing WebSearchFAISS: {str(e)}", exc_info=True)
|
291 |
# If initialization fails, create an empty database
|
292 |
-
database = WebSearchFAISS(embed
|
|
|
293 |
|
294 |
return database, temp_dir
|
295 |
|
@@ -313,7 +311,14 @@ def duckduckgo_search(query):
|
|
313 |
|
314 |
logging.info(f"Number of documents created: {len(documents)}")
|
315 |
if documents:
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
return database, temp_dir, results
|
318 |
except Exception as e:
|
319 |
logging.error(f"Error in duckduckgo_search: {str(e)}", exc_info=True)
|
@@ -364,7 +369,7 @@ def retry_last_response(history, use_web_search, model, temperature, num_calls):
|
|
364 |
|
365 |
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
|
366 |
|
367 |
-
def respond(message
|
368 |
logging.info(f"User Query: {message}")
|
369 |
logging.info(f"Model Used: {model}")
|
370 |
logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
|
@@ -373,24 +378,28 @@ def respond(message: str, history: List[Tuple[str, str]], model: str, temperatur
|
|
373 |
try:
|
374 |
if use_web_search:
|
375 |
database, temp_dir, search_results = duckduckgo_search(message)
|
376 |
-
if database is None:
|
377 |
-
yield "I'm sorry, but I
|
378 |
return
|
379 |
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
|
|
|
|
|
|
|
|
394 |
else:
|
395 |
# PDF search logic
|
396 |
embed = get_embeddings()
|
|
|
279 |
embed = get_embeddings()
|
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
|
293 |
|
|
|
311 |
|
312 |
logging.info(f"Number of documents created: {len(documents)}")
|
313 |
if documents:
|
314 |
+
try:
|
315 |
+
database.add_documents(documents)
|
316 |
+
logging.info(f"Successfully added {len(documents)} documents to the database")
|
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:
|
324 |
logging.error(f"Error in duckduckgo_search: {str(e)}", exc_info=True)
|
|
|
369 |
|
370 |
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
|
371 |
|
372 |
+
def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
|
373 |
logging.info(f"User Query: {message}")
|
374 |
logging.info(f"Model Used: {model}")
|
375 |
logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
|
|
|
378 |
try:
|
379 |
if use_web_search:
|
380 |
database, temp_dir, search_results = duckduckgo_search(message)
|
381 |
+
if database is None or not search_results:
|
382 |
+
yield "I'm sorry, but I couldn't find any search results for your query. Could you please rephrase or ask a different question?"
|
383 |
return
|
384 |
|
385 |
+
try:
|
386 |
+
context = retrieve_web_search_results(database, message)
|
387 |
+
logging.info(f"Retrieved context length: {len(context)}")
|
388 |
+
|
389 |
+
if model == "@cf/meta/llama-3.1-8b-instruct":
|
390 |
+
# Use Cloudflare API
|
391 |
+
for partial_response in get_response_from_cloudflare(prompt="", context=context, query=message, num_calls=num_calls, temperature=temperature, search_type="web"):
|
392 |
+
logging.debug(f"Partial response: {partial_response[:100]}...") # Log first 100 chars
|
393 |
+
yield partial_response
|
394 |
+
else:
|
395 |
+
# Use Hugging Face API
|
396 |
+
for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
|
397 |
+
response = f"{main_content}\n\n{sources}"
|
398 |
+
logging.debug(f"Response: {response[:100]}...") # Log first 100 chars
|
399 |
+
yield response
|
400 |
+
finally:
|
401 |
+
# Clean up the temporary database
|
402 |
+
cleanup_web_search_database(temp_dir)
|
403 |
else:
|
404 |
# PDF search logic
|
405 |
embed = get_embeddings()
|