Update app.py
Browse files229 unmodified lines2302312322332342352362372382392402412422432442452462472489 unmodified lines25825926026126226326 unmodified lines290291292293294295296297298299300301302303304229 unmodified linesmodel = get_model(temperature, top_p, repetition_penalty)embed = get_embeddings()# Check if the FAISS database exists, if not create an empty oneif os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:database = FAISS.from_documents([], embed)database.save_local("faiss_database")if web_search:search_results = google_search(question)web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]# Add web search results to the existing databasedatabase.add_documents(web_docs)database.save_local("faiss_database")context_str = "\n".join([doc.page_content for doc in web_docs])9 unmodified linesprompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=question)else:history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])if is_related_to_history(question, conversation_history):26 unmodified linesembed = get_embeddings()total_chunks = 0for file in files:if use_recursive_splitter:data = load_and_split_document_recursive(file)else:data = load_and_split_document_basic(file)create_or_update_database(data, embed)total_chunks += len(data)return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files."def extract_db_to_excel():embed = get_embeddings()229 unmodified lines2302312322332342352362372382392402412422432442452462472482492502512522539 unmodified lines26326426526626726826927027126 unmodified lines298299300301302303304305306307308309310311312313314315316317318319320321229 unmodified linesmodel = get_model(temperature, top_p, repetition_penalty)embed = get_embeddings()# Check if the FAISS database existsif os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:# If no database exists, we'll create it with the first web search or document uploaddatabase = Noneif web_search:search_results = google_search(question)web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:# Create the database with web search results if it doesn't existdatabase = FAISS.from_documents(web_docs, embed)else:# Add web search results to the existing databasedatabase.add_documents(web_docs)database.save_local("faiss_database")context_str = "\n".join([doc.page_content for doc in web_docs])9 unmodified linesprompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=question)else:if database is None:return "No documents or web search results available. Please upload documents or enable web search."history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])if is_related_to_history(question, conversation_history):26 unmodified linesembed = get_embeddings()total_chunks = 0all_data = []for file in files:if use_recursive_splitter:data = load_and_split_document_recursive(file)else:data = load_and_split_document_basic(file)all_data.extend(data)total_chunks += len(data)if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)database.add_documents(all_data)else:database = FAISS.from_documents(all_data, embed)database.save_local("faiss_database")return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files.""def extract_db_to_excel():embed = get_embeddings()
app.py+24-7