Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,6 +21,7 @@ import inspect
|
|
21 |
import logging
|
22 |
import shutil
|
23 |
import tempfile
|
|
|
24 |
|
25 |
|
26 |
# Set up basic configuration for logging
|
@@ -346,7 +347,7 @@ def retry_last_response(history, use_web_search, model, temperature, num_calls):
|
|
346 |
|
347 |
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
|
348 |
|
349 |
-
def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
|
350 |
logging.info(f"User Query: {message}")
|
351 |
logging.info(f"Model Used: {model}")
|
352 |
logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
|
@@ -355,47 +356,59 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
|
|
355 |
try:
|
356 |
if use_web_search:
|
357 |
database, temp_dir, search_results = duckduckgo_search(message)
|
|
|
|
|
358 |
if not search_results:
|
359 |
yield "I'm sorry, but I couldn't find any search results for your query. Could you please rephrase or ask a different question?"
|
360 |
return
|
361 |
|
362 |
-
|
363 |
-
|
364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
else:
|
|
|
366 |
embed = get_embeddings()
|
367 |
-
if os.path.exists("faiss_database"):
|
368 |
-
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
369 |
-
retriever = database.as_retriever(search_kwargs={"k": 20})
|
370 |
-
|
371 |
-
# Filter relevant documents based on user selection
|
372 |
-
all_relevant_docs = retriever.get_relevant_documents(message)
|
373 |
-
relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]
|
374 |
-
|
375 |
-
if not relevant_docs:
|
376 |
-
yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
|
377 |
-
return
|
378 |
-
|
379 |
-
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
380 |
-
else:
|
381 |
-
context_str = "No documents available."
|
382 |
yield "No documents available. Please upload PDF documents to answer questions."
|
383 |
return
|
384 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
385 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
386 |
# Use Cloudflare API
|
387 |
for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
|
388 |
-
|
389 |
-
# logging.info(f"Generated Response (first line): {first_line}")
|
390 |
yield partial_response
|
391 |
else:
|
392 |
# Use Hugging Face API
|
393 |
for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
|
394 |
-
|
395 |
-
# logging.info(f"Generated Response (first line): {first_line}")
|
396 |
yield partial_response
|
|
|
397 |
except Exception as e:
|
398 |
-
logging.error(f"Error with {model}: {str(e)}")
|
399 |
if "microsoft/Phi-3-mini-4k-instruct" in model:
|
400 |
logging.info("Falling back to Mistral model due to Phi-3 error")
|
401 |
fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
|
|
|
21 |
import logging
|
22 |
import shutil
|
23 |
import tempfile
|
24 |
+
from typing import List, Tuple
|
25 |
|
26 |
|
27 |
# Set up basic configuration for logging
|
|
|
347 |
|
348 |
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
|
349 |
|
350 |
+
def respond(message: str, history: List[Tuple[str, str]], model: str, temperature: float, num_calls: int, use_web_search: bool, selected_docs: List[str]) -> str:
|
351 |
logging.info(f"User Query: {message}")
|
352 |
logging.info(f"Model Used: {model}")
|
353 |
logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
|
|
|
356 |
try:
|
357 |
if use_web_search:
|
358 |
database, temp_dir, search_results = duckduckgo_search(message)
|
359 |
+
logging.info(f"Number of search results: {len(search_results)}")
|
360 |
+
|
361 |
if not search_results:
|
362 |
yield "I'm sorry, but I couldn't find any search results for your query. Could you please rephrase or ask a different question?"
|
363 |
return
|
364 |
|
365 |
+
context = retrieve_web_search_results(database, message)
|
366 |
+
logging.info(f"Retrieved context length: {len(context)}")
|
367 |
+
|
368 |
+
if model == "@cf/meta/llama-3.1-8b-instruct":
|
369 |
+
# Use Cloudflare API
|
370 |
+
for partial_response in get_response_from_cloudflare(prompt="", context=context, query=message, num_calls=num_calls, temperature=temperature, search_type="web"):
|
371 |
+
logging.debug(f"Partial response: {partial_response[:100]}...") # Log first 100 chars
|
372 |
+
yield partial_response
|
373 |
+
else:
|
374 |
+
# Use Hugging Face API
|
375 |
+
for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
|
376 |
+
response = f"{main_content}\n\n{sources}"
|
377 |
+
logging.debug(f"Response: {response[:100]}...") # Log first 100 chars
|
378 |
+
yield response
|
379 |
else:
|
380 |
+
# PDF search logic
|
381 |
embed = get_embeddings()
|
382 |
+
if not os.path.exists("faiss_database"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
383 |
yield "No documents available. Please upload PDF documents to answer questions."
|
384 |
return
|
385 |
|
386 |
+
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
387 |
+
retriever = database.as_retriever(search_kwargs={"k": 20})
|
388 |
+
|
389 |
+
all_relevant_docs = retriever.get_relevant_documents(message)
|
390 |
+
relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]
|
391 |
+
|
392 |
+
if not relevant_docs:
|
393 |
+
yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
|
394 |
+
return
|
395 |
+
|
396 |
+
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
397 |
+
logging.info(f"Context length for PDF search: {len(context_str)}")
|
398 |
+
|
399 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
400 |
# Use Cloudflare API
|
401 |
for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
|
402 |
+
logging.debug(f"Partial response: {partial_response[:100]}...") # Log first 100 chars
|
|
|
403 |
yield partial_response
|
404 |
else:
|
405 |
# Use Hugging Face API
|
406 |
for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
|
407 |
+
logging.debug(f"Partial response: {partial_response[:100]}...") # Log first 100 chars
|
|
|
408 |
yield partial_response
|
409 |
+
|
410 |
except Exception as e:
|
411 |
+
logging.error(f"Error in respond function with {model}: {str(e)}", exc_info=True)
|
412 |
if "microsoft/Phi-3-mini-4k-instruct" in model:
|
413 |
logging.info("Falling back to Mistral model due to Phi-3 error")
|
414 |
fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
|