Update app.py
Browse files12 unmodified lines1314151617181920212234 unmodified lines575859606162636416 unmodified lines8182838485868788899091929394959697989910010112 unmodified lines11411511611711811912012112212312412512612712812913013113213313413513613713813910 unmodified lines15015115215315415515615715815916010 unmodified lines17117217317417517617717817918018118218318412 unmodified linesfrom langchain_community.llms import HuggingFaceHubfrom langchain_core.runnables import RunnableParallel, RunnablePassthroughfrom 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."""34 unmodified linesreturn "No cache to clear."prompt = """Answer the question based only on the following context:{context}Question: {question}Provide a concise and direct answer to the question:16 unmodified linesfor i in range(max_chunks):chunk = model(prompt + full_response, max_new_tokens=max_tokens)chunk = chunk.strip()# Check for final sentence endingsif chunk.endswith((".", "!", "?")):full_response += chunkbreakfull_response += chunkreturn full_response.strip()def response(database, model, question):prompt_val = ChatPromptTemplate.from_template(prompt)retriever = database.as_retriever()context = retriever.get_relevant_documents(question)context_str = "\n".join([doc.page_content for doc in context])formatted_prompt = prompt_val.format(context=context_str, question=question)ans = generate_chunked_response(model, formatted_prompt)return ans.split("Question:")[-1].strip() # Return only the answer partdef update_vectors(files, use_recursive_splitter):if not files:12 unmodified linesreturn f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files."def 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)10 unmodified linesdef export_memory_db_to_excel():data = [{"question": question, "answer": answer} for question, answer in memory_database.items()]df = pd.DataFrame(data)with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:excel_path = tmp.namedf.to_excel(excel_path, index=False)return excel_path10 unmodified linesupdate_button.click(update_vectors, inputs=[file_input, use_recursive_splitter], outputs=update_output)with gr.Row():question_input = gr.Textbox(label="Ask a question about your documents")temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.5, step=0.1)top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)repetition_penalty_slider = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.0, step=0.1)submit_button = gr.Button("Submit")answer_output = gr.Textbox(label="Answer")submit_button.click(ask_question, inputs=[question_input, temperature_slider, top_p_slider, repetition_penalty_slider], outputs=answer_output)extract_button = gr.Button("Extract Database to Excel")excel_output = gr.File(label="Download Excel File")12 unmodified lines13141516171819202122232434 unmodified lines5960616263646566676869707116 unmodified lines88899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213312 unmodified lines14614714814915015110 unmodified lines16216316416516616716816917017117217317417517617710 unmodified lines18818919019119219319419519619719819920020120220320420520620720812 unmodified linesfrom langchain_community.llms import HuggingFaceHubfrom langchain_core.runnables import RunnableParallel, RunnablePassthroughfrom langchain_core.documents import Documenthuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")# Memory database to store question-answer pairsmemory_database = {}conversation_history = []def load_and_split_document_basic(file):"""Loads and splits the document into pages."""34 unmodified linesreturn "No cache to clear."prompt = """Answer the question based on the following context and conversation history:Conversation History:{history}Context from documents:{context}Question: {question}Provide a concise and direct answer to the question:16 unmodified linesfor i in range(max_chunks):chunk = model(prompt + full_response, max_new_tokens=max_tokens)chunk = chunk.strip()if chunk.endswith((".", "!", "?")):full_response += chunkbreakfull_response += chunkreturn full_response.strip()def manage_conversation_history(question, answer, history, max_history=5):history.append({"question": question, "answer": answer})if len(history) > max_history:history.pop(0)return historydef ask_question(question, temperature, top_p, repetition_penalty):global conversation_historyif not question:return "Please enter a question."if question in memory_database:answer = memory_database[question]else:embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)model = get_model(temperature, top_p, repetition_penalty)history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])prompt_val = ChatPromptTemplate.from_template(prompt)retriever = database.as_retriever()relevant_docs = retriever.get_relevant_documents(question)context_str = "\n".join([doc.page_content for doc in relevant_docs])formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)answer = generate_chunked_response(model, formatted_prompt)answer = answer.split("Question:")[-1].strip()memory_database[question] = answerconversation_history = manage_conversation_history(question, answer, conversation_history)return answerdef update_vectors(files, use_recursive_splitter):if not files:12 unmodified linesreturn f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files."def extract_db_to_excel():embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)10 unmodified linesdef export_memory_db_to_excel():data = [{"question": question, "answer": answer} for question, answer in memory_database.items()]df_memory = pd.DataFrame(data)data_history = [{"question": item["question"], "answer": item["answer"]} for item in conversation_history]df_history = pd.DataFrame(data_history)with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:excel_path = tmp.namewith pd.ExcelWriter(excel_path, engine='openpyxl') as writer:df_memory.to_excel(writer, sheet_name='Memory Database', index=False)df_history.to_excel(writer, sheet_name='Conversation History', index=False)return excel_path10 unmodified linesupdate_button.click(update_vectors, inputs=[file_input, use_recursive_splitter], outputs=update_output)with gr.Row():with gr.Column(scale=2):chatbot = gr.Chatbot(label="Conversation")question_input = gr.Textbox(label="Ask a question about your documents")submit_button = gr.Button("Submit")with gr.Column(scale=1):temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.5, step=0.1)top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)repetition_penalty_slider = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.0, step=0.1)def chat(question, history):answer = ask_question(question, temperature_slider.value, top_p_slider.value, repetition_penalty_slider.value)history.append((question, answer))return "", historysubmit_button.click(chat, inputs=[question_input, chatbot], outputs=[question_input, chatbot])extract_button = gr.Button("Extract Database to Excel")excel_output = gr.File(label="Download Excel File")
app.py+64-40