Update app.py
Browse files12345678910119 unmodified lines212223242526272829303132333435363738394 unmodified lines44454647484950import gradio as grfrom langchain.vectorstores import Chromafrom langchain.embeddings import HuggingFaceEmbeddingsfrom langchain.llms import HuggingFaceHubfrom langchain.chains import ConversationalRetrievalChainfrom langchain.document_loaders import SimpleDocumentLoaderfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.memory import ConversationBufferMemory# Initialize the Hugging Face embedding modelembedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")9 unmodified linesqa_chain = ConversationalRetrievalChain.from_llm(llm, retriever=vectorstore.as_retriever(), memory=memory)def upload_docs(docs):# Load and process the uploaded documentsloader = SimpleDocumentLoader(docs)documents = loader.load()# Split documents into manageable chunkstext_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)texts = text_splitter.split_documents(documents)# Add documents to the vector store and persist themvectorstore.add_documents(texts)vectorstore.persist()return "Documents uploaded and processed successfully!"def chat(query):# Process the query with the conversational chain and return the result4 unmodified lineswith gr.Blocks() as demo:with gr.Row():with gr.Column():doc_upload = gr.File(label="Upload your documents", file_types=[".txt", ".pdf", ".docx"], multiple=True)upload_button = gr.Button("Upload")upload_button.click(upload_docs, inputs=doc_upload, outputs=gr.Textbox())12345678910119 unmodified lines2122232425262728293031323334353637383940414 unmodified lines46474849505152import gradio as grfrom langchain_community.vectorstores import Chromafrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.llms import HuggingFaceHubfrom langchain.chains import ConversationalRetrievalChainfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.memory import ConversationBufferMemoryfrom langchain_community.document_loaders import PyPDFLoader# Initialize the Hugging Face embedding modelembedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")9 unmodified linesqa_chain = ConversationalRetrievalChain.from_llm(llm, retriever=vectorstore.as_retriever(), memory=memory)def upload_docs(docs):# Load and process the uploaded PDF documentsloaded_docs = []for doc in docs:loader = PyPDFLoader(doc.name)loaded_docs.extend(loader.load())# Split documents into manageable chunkstext_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)texts = text_splitter.split_documents(loaded_docs)# Add documents to the vector store and persist themvectorstore.add_documents(texts)vectorstore.persist()return "PDF documents uploaded and processed successfully!"def chat(query):# Process the query with the conversational chain and return the result4 unmodified lineswith gr.Blocks() as demo:with gr.Row():with gr.Column():doc_upload = gr.File(label="Upload your PDF documents", file_types=[".pdf"], multiple=True)upload_button = gr.Button("Upload")upload_button.click(upload_docs, inputs=doc_upload, outputs=gr.Textbox())
app.py+15-13