Update app.py
Browse files4 unmodified lines567891011121314151617183 unmodified lines222324252627282 unmodified lines313233343536374 unmodified lines4243444546474849505152538 unmodified lines62636465666768697071724 unmodified linesfrom langchain_community.llms import HuggingFaceHubfrom langchain.chains import ConversationalRetrievalChainfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.memory import ConversationBufferMemoryfrom langchain_community.document_loaders import PyPDFLoaderimport osload_dotenv()# Get the Hugging Face API token from the .env filehf_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")if hf_api_token is None:raise ValueError("HUGGINGFACEHUB_API_TOKEN not found in .env file")3 unmodified lines# Initialize the LLaMA 2 model from Hugging Face Hub using the token from .envllm = HuggingFaceEndpoint(repo_id="meta-llama/Llama-2-7b-hf",model_kwargs={"temperature": 0.7, "max_length": 512},huggingfacehub_api_token=hf_api_token)2 unmodified lines# Create a conversational chain with retrieval capabilitiesmemory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)qa_chain = ConversationalRetrievalChain.from_llm(llm, retriever=vectorstore.as_retriever(), memory=memory)def upload_docs(docs):# Load and process the uploaded PDF documents4 unmodified lines# 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):8 unmodified linesdoc_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())with gr.Column():chat_input = gr.Textbox(label="Ask a question:")chat_output = gr.Textbox(label="Answer:")chat_button = gr.Button("Send")chat_button.click(chat, inputs=chat_input, outputs=chat_output)demo.launch()4 unmodified lines5678910111213141516171819203 unmodified lines24252627282930312 unmodified lines3435363738394041424 unmodified lines4748495051525354555657588 unmodified lines676869707172737475764 unmodified linesfrom langchain_community.llms import HuggingFaceHubfrom langchain.chains import ConversationalRetrievalChainfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.memoryimport ConversationBufferMemoryfrom langchain_community.document_loadersimport PyPDFLoaderimport osload_dotenv()# Get the Hugging Face API token from the .env filehf_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")if hf_api_token is None:raise ValueError("HUGGINGFACEHUB_API_TOKEN not found in .env file")3 unmodified lines# Initialize the LLaMA 2 model from Hugging Face Hub using the token from .envllm = HuggingFaceEndpoint(repo_id="meta-llama/Llama-2-7b-hf",temperature=0.7, # Specify temperature explicitlymax_length=512,huggingfacehub_api_token=hf_api_token)2 unmodified lines# Create a conversational chain with retrieval capabilitiesmemory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)qa_chain = ConversationalRetrievalChain.from_llm(llm,retriever=vectorstore.as_retriever(), memory=memory)def upload_docs(docs):# Load and process the uploaded PDF documents4 unmodified lines# 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):8 unmodified linesdoc_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())with gr.Column():chat_input = gr.Textbox(label="Ask a question:")chat_output = gr.Textbox(label="Answer:")chat_button = gr.Button("Send")chat_button.click(chat, inputs=chat_input, outputs=chat_output)demo.launch()
app.py+13-9