Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,40 +4,53 @@ from PyPDF2 import PdfReader
|
|
4 |
from langchain.text_splitter import CharacterTextSplitter
|
5 |
from langchain import vectorstores
|
6 |
from langchain import chains
|
7 |
-
from langchain import llms
|
8 |
from langchain.embeddings import HuggingFaceEmbeddings
|
9 |
import gradio as gr
|
10 |
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
if pdf is not None:
|
15 |
-
pdf_reader = PdfReader(pdf)
|
16 |
-
texts = ""
|
17 |
-
for page in pdf_reader.pages:
|
18 |
-
texts += page.extract_text()
|
19 |
-
text_splitter = CharacterTextSplitter(
|
20 |
-
separator="\n",
|
21 |
-
chunk_size=1000,
|
22 |
-
chunk_overlap=0
|
23 |
-
)
|
24 |
-
chunks = text_splitter.split_text(texts)
|
25 |
-
embeddings = HuggingFaceEmbeddings()
|
26 |
-
db = vectorstores.Chroma.from_texts(chunks, embeddings)
|
27 |
-
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 10})
|
28 |
-
qa = chains.ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever)
|
29 |
-
chat_history = []
|
30 |
-
if query:
|
31 |
-
result = qa({"question": query, "chat_history": chat_history})
|
32 |
-
return result["answer"]
|
33 |
-
return "Please upload a PDF and enter a query."
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from langchain.text_splitter import CharacterTextSplitter
|
5 |
from langchain import vectorstores
|
6 |
from langchain import chains
|
7 |
+
from langchain import llms
|
8 |
from langchain.embeddings import HuggingFaceEmbeddings
|
9 |
import gradio as gr
|
10 |
|
11 |
+
load_dotenv()
|
12 |
|
13 |
+
llm = llms.AI21(ai21_api_key=os.getenv('AI21_API_KEY'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
def process_pdf(pdf_file):
|
16 |
+
pdf_reader = PdfReader(pdf_file)
|
17 |
+
texts = ""
|
18 |
+
for page in pdf_reader.pages:
|
19 |
+
texts += page.extract_text()
|
20 |
+
|
21 |
+
text_splitter = CharacterTextSplitter(
|
22 |
+
separator="\n",
|
23 |
+
chunk_size=1000,
|
24 |
+
chunk_overlap=0
|
25 |
+
)
|
26 |
+
chunks = text_splitter.split_text(texts)
|
27 |
+
embeddings = HuggingFaceEmbeddings()
|
28 |
+
db = vectorstores.Chroma.from_texts(chunks, embeddings)
|
29 |
+
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k":10})
|
30 |
+
qa = chains.ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever)
|
31 |
+
return qa
|
32 |
|
33 |
+
def answer_question(pdf_file, question, chat_history):
|
34 |
+
if not pdf_file:
|
35 |
+
return "Please upload a PDF file first."
|
36 |
+
|
37 |
+
qa = process_pdf(pdf_file)
|
38 |
+
result = qa({"question": question, "chat_history": chat_history})
|
39 |
+
chat_history.append((question, result["answer"]))
|
40 |
+
return result["answer"]
|
41 |
|
42 |
+
def main():
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("# PDF QA")
|
45 |
+
with gr.Row():
|
46 |
+
pdf_file = gr.File(label="Upload your PDF", file_types=[".pdf"])
|
47 |
+
question = gr.Textbox(label="Ask a question about the PDF")
|
48 |
+
output = gr.Textbox(label="Answer")
|
49 |
+
chat_history = gr.State([])
|
50 |
+
submit_btn = gr.Button("Submit")
|
51 |
+
submit_btn.click(answer_question, inputs=[pdf_file, question, chat_history], outputs=output)
|
52 |
+
|
53 |
+
demo.launch()
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
main()
|