Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
from langchain_openai import ChatOpenAI
|
3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
@@ -26,32 +27,32 @@ 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 |
llm=llm,
|
56 |
chain_type="stuff",
|
57 |
retriever=db.as_retriever(),
|
@@ -59,8 +60,15 @@ chain = ConversationalRetrievalChain.from_llm(
|
|
59 |
return_source_documents=False,
|
60 |
combine_docs_chain_kwargs={'prompt': prompt}
|
61 |
)
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
from langchain_openai import ChatOpenAI
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
27 |
|
28 |
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
29 |
|
30 |
+
def process_pdf_and_answer(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(),
|
|
|
60 |
return_source_documents=False,
|
61 |
combine_docs_chain_kwargs={'prompt': prompt}
|
62 |
)
|
63 |
+
|
64 |
+
# Process the question
|
65 |
+
res = chain({"question": question})
|
66 |
+
answer = res["answer"]
|
67 |
|
68 |
+
gr.Interface(
|
69 |
+
fn=process_pdf_and_answer,
|
70 |
+
inputs=[gr.File(file_count="single", type="filepath"), gr.Textbox(lines=2, placeholder="Ask a question...")],
|
71 |
+
outputs="text",
|
72 |
+
title="PDF Q&A",
|
73 |
+
description="Upload a pdf and ask questions about it"
|
74 |
+
).launch()
|