Update app.py
Browse files14 unmodified lines15161718192038 unmodified lines596061626364656667686925 unmodified lines959697989910010110210310414 unmodified linesfrom langchain_core.documents import Documenthuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")def load_and_split_document_basic(file):"""Loads and splits the document into pages."""loader = PyPDFLoader(file.name)38 unmodified lines"temperature": temperature,"top_p": top_p,"repetition_penalty": repetition_penalty,"max_length": 1000},huggingfacehub_api_token=huggingface_token)def generate_chunked_response(model, prompt, max_tokens=1000, max_chunks=5):full_response = ""for i in range(max_chunks):chunk = model(prompt + full_response, max_new_tokens=max_tokens)25 unmodified linesdef ask_question(question, temperature, top_p, repetition_penalty):if not question:return "Please enter a question."embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)model = get_model(temperature, top_p, repetition_penalty)return response(database, model, question)def extract_db_to_excel():embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)14 unmodified lines151617181920212238 unmodified lines616263646566676869707125 unmodified lines97989910010110210310410510610710810911011111211314 unmodified linesfrom langchain_core.documents import Documenthuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")# Memory database to store question-answer pairsmemory_database = {}def load_and_split_document_basic(file):"""Loads and splits the document into pages."""loader = PyPDFLoader(file.name)38 unmodified lines"temperature": temperature,"top_p": top_p,"repetition_penalty": repetition_penalty,"max_length": 512},huggingfacehub_api_token=huggingface_token)def generate_chunked_response(model, prompt, max_tokens=500, max_chunks=5):full_response = ""for i in range(max_chunks):chunk = model(prompt + full_response, max_new_tokens=max_tokens)25 unmodified linesdef ask_question(question, temperature, top_p, repetition_penalty):if not question:return "Please enter a question."# Check if the question exists in the memory databaseif question in memory_database:return memory_database[question]embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)model = get_model(temperature, top_p, repetition_penalty)# Generate response from document databaseanswer = response(database, model, question)# Store the question and answer in the memory databasememory_database[question] = answerreturn answerdef extract_db_to_excel():embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
app.py+12-3