Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
immport os
|
| 2 |
from langchain_openai import ChatOpenAI
|
| 3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
@@ -26,41 +27,49 @@ Helpful answer:
|
|
| 26 |
|
| 27 |
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
| 28 |
|
| 29 |
-
|
| 30 |
# Load and process the PDF
|
| 31 |
-
loader = PyPDFLoader(pdf_file.name)
|
| 32 |
-
pdf_data = loader.load()
|
| 33 |
-
|
| 34 |
-
# Split the text into chunks
|
| 35 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 36 |
-
docs = text_splitter.split_documents(pdf_data)
|
| 37 |
-
|
| 38 |
-
# Create a Chroma vector store
|
| 39 |
-
embeddings = HuggingFaceEmbeddings(model_name="embaas/sentence-transformers-multilingual-e5-base")
|
| 40 |
-
db = Chroma.from_documents(docs, embeddings)
|
| 41 |
-
|
| 42 |
-
# Initialize message history for conversation
|
| 43 |
-
message_history = ChatMessageHistory()
|
| 44 |
-
|
| 45 |
-
# Memory for conversational context
|
| 46 |
-
memory = ConversationBufferMemory(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Create a chain that uses the Chroma vector store
|
| 54 |
-
chain = ConversationalRetrievalChain.from_llm(
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Process the question
|
| 64 |
-
res = chain({"question": question})
|
| 65 |
-
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
immport os
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 27 |
|
| 28 |
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
| 29 |
|
| 30 |
+
def process_pdf_and_ask_question(pdf_file,question)
|
| 31 |
# Load and process the PDF
|
| 32 |
+
loader = PyPDFLoader(pdf_file.name)
|
| 33 |
+
pdf_data = loader.load()
|
| 34 |
+
|
| 35 |
+
# Split the text into chunks
|
| 36 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 37 |
+
docs = text_splitter.split_documents(pdf_data)
|
| 38 |
+
|
| 39 |
+
# Create a Chroma vector store
|
| 40 |
+
embeddings = HuggingFaceEmbeddings(model_name="embaas/sentence-transformers-multilingual-e5-base")
|
| 41 |
+
db = Chroma.from_documents(docs, embeddings)
|
| 42 |
+
|
| 43 |
+
# Initialize message history for conversation
|
| 44 |
+
message_history = ChatMessageHistory()
|
| 45 |
+
|
| 46 |
+
# Memory for conversational context
|
| 47 |
+
memory = ConversationBufferMemory(
|
| 48 |
+
memory_key="chat_history",
|
| 49 |
+
output_key="answer",
|
| 50 |
+
chat_memory=message_history,
|
| 51 |
+
return_messages=True,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Create a chain that uses the Chroma vector store
|
| 55 |
+
chain = ConversationalRetrievalChain.from_llm(
|
| 56 |
+
llm=llm,
|
| 57 |
+
chain_type="stuff",
|
| 58 |
+
retriever=db.as_retriever(),
|
| 59 |
+
memory=memory,
|
| 60 |
+
return_source_documents=False,
|
| 61 |
+
combine_docs_chain_kwargs={'prompt': prompt}
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Process the question
|
| 65 |
+
res = chain({"question": question})
|
| 66 |
+
return res["answer"]
|
| 67 |
|
| 68 |
+
app=gr.Interface(fn=process_pdf_and_ask_question,
|
| 69 |
+
inputs=[gr.File(file_count="single", type="filepath"), gr.Textbox(lines=2, placeholder="Ask a question...")],
|
| 70 |
+
outputs="text",
|
| 71 |
+
title="PDF Q&A",
|
| 72 |
+
description="Upload a PDF and ask questions about it.",
|
| 73 |
+
|
| 74 |
+
)
|
| 75 |
+
app.launch()
|