Spaces:
Build error
Build error
Update app.py
Browse files17 unmodified lines1819202122232425262768 unmodified lines969798991009610110210310410510610710810911011111211311411511611711811912012112212312412511312612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716817 unmodified linesfrom langchain_core.documents import Documentfrom sentence_transformers import SentenceTransformerfrom llama_parse import LlamaParseimport logging
# Set up logginglogging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# Environment variables and configurationshuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")68 unmodified lines )
def duckduckgo_search(query): logging.debug(f"Performing DuckDuckGo search for query: {query}") with DDGS() as ddgs: results = ddgs.text(query, max_results=5) results = list(ddgs.text(query, max_results=5)) logging.debug(f"Search returned {len(results)} results") return results
def get_response_with_search(query, temperature, top_p, repetition_penalty, use_pdf=False): logging.debug(f"Getting response for query: {query}") logging.debug(f"Parameters: temperature={temperature}, top_p={top_p}, repetition_penalty={repetition_penalty}, use_pdf={use_pdf}") model = get_model(temperature, top_p, repetition_penalty) embed = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
if use_pdf and os.path.exists("faiss_database"): logging.debug("Using PDF database for context") database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True) retriever = database.as_retriever() relevant_docs = retriever.get_relevant_documents(query) context = "\n".join([f"Content: {doc.page_content}\nSource: {doc.metadata['source']}\n" for doc in relevant_docs]) else: logging.debug("Using web search for context") search_results = duckduckgo_search(query) context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n" for result in search_results if 'body' in result) logging.debug(f"Context generated. Length: {len(context)} characters")
prompt = f"""<s>[INST] Using the following context: prompt = f"""<s>[INST] Using the following context:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'After the main content, provide a list of sources used in your response, prefixed with 'Sources:'. Do not include any part of these instructions in your response. [/INST]"""
logging.debug("Sending prompt to model") response = model(prompt) logging.debug(f"Received response from model. Length: {len(response)} characters") main_content, sources = split_response(response) logging.debug(f"Split response. Main content length: {len(main_content)}, Sources length: {len(sources)}") return main_content, sources
def split_response(response): logging.debug("Splitting response") logging.debug(f"Original response: {response[:100]}...") # Log first 100 characters # Remove any remaining instruction text response = re.sub(r'\[/?INST\]', '', response) response = re.sub(r'~~.*?~~', '', response, flags=re.DOTALL) logging.debug(f"After removing instructions: {response[:100]}...") # Log first 100 characters # Split the response into main content and sources parts = response.split("Sources:", 1) main_content = parts[0].strip() sources = parts[1].strip() if len(parts) > 1 else "" logging.debug(f"Main content starts with: {main_content[:100]}...") # Log first 100 characters logging.debug(f"Sources: {sources[:100]}...") # Log first 100 characters return main_content, sources
def chatbot_interface(message, history, temperature, top_p, repetition_penalty, use_pdf): logging.debug(f"Chatbot interface called with message: {message}") main_content, sources = get_response_with_search(message, temperature, top_p, repetition_penalty, use_pdf) formatted_response = f"{main_content}\n\nSources:\n{sources}" logging.debug(f"Formatted response: {formatted_response[:100]}...") # Log first 100 characters return formatted_response
# Gradio interfaceapp.py+28-2