Update app.py
Browse files132 unmodified lines1331341351361371381392 unmodified lines1421431441451461471481491501511521531541551561571581591601617 unmodified lines1691701711721731741757 unmodified lines183184185186187188189190191192132 unmodified linesif os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:return "No documents available. Please upload PDF documents to answer questions.", ""retriever = database.as_retriever()relevant_docs = retriever.get_relevant_documents(query)2 unmodified linesprompt = f"""<s>[INST] Using the following context from the PDF documents:{context_str}Write a detailed and complete response that answers the following user question: '{query}'After writing the response, please provide a list of sources used (document names) in your answer. [/INST]"""generated_text = generate_chunked_response(prompt)# Remove all instruction-related contentclean_text = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', generated_text, flags=re.DOTALL)clean_text = clean_text.replace("Using the following context from the PDF documents:", "").strip()# Split the content and sourcesparts = clean_text.split("Sources:", 1)main_content = parts[0].strip()sources = parts[1].strip() if len(parts) > 1 else ""return main_content, sourcesdef get_response_with_search(query):search_results = duckduckgo_search(query)7 unmodified linesgenerated_text = generate_chunked_response(prompt)# Remove all instruction-related contentclean_text = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', generated_text, flags=re.DOTALL)clean_text = clean_text.replace("Using the following context:", "").strip()7 unmodified linesdef chatbot_interface(message, history, use_web_search):if use_web_search:main_content, sources = get_response_with_search(message)else:main_content, sources = get_response_from_pdf(message)formatted_response = f"{main_content}\n\nSources:\n{sources}"history.append((message, formatted_response))return history132 unmodified lines1331341351361371381392 unmodified lines1421431441451461471481491501511521531541551567 unmodified lines1641651661671681691707 unmodified lines178179180181182183184185186187188132 unmodified linesif os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:return "No documents available. Please upload PDF documents to answer questions."retriever = database.as_retriever()relevant_docs = retriever.get_relevant_documents(query)2 unmodified linesprompt = f"""<s>[INST] Using the following context from the PDF documents:{context_str}Write a detailed and complete response that answers the following user question: '{query}'Do not include a list of sources in your response. [/INST]"""generated_text = generate_chunked_response(prompt)# Clean the responseclean_text = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', generated_text, flags=re.DOTALL)clean_text = clean_text.replace("Using the following context from the PDF documents:", "").strip()return clean_textdef get_response_with_search(query):search_results = duckduckgo_search(query)7 unmodified linesgenerated_text = generate_chunked_response(prompt)# Clean the responseclean_text = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', generated_text, flags=re.DOTALL)clean_text = clean_text.replace("Using the following context:", "").strip()7 unmodified linesdef chatbot_interface(message, history, use_web_search):if use_web_search:main_content, sources = get_response_with_search(message)formatted_response = f"{main_content}\n\nSources:\n{sources}"else:response = get_response_from_pdf(message)formatted_response = response # No sources for PDF responseshistory.append((message, formatted_response))return history
app.py+9-13