Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import fitz
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
5 |
+
from langchain.text_splitter import CharacterTextSplitter
|
6 |
+
from langchain.vectorstores import FAISS
|
7 |
+
from langchain.chains.question_answering import load_qa_chain
|
8 |
+
from langchain.llms import OpenAI
|
9 |
+
|
10 |
+
def read_pdf(pdf_file, prompt):
|
11 |
+
try:
|
12 |
+
text = ""
|
13 |
+
with open(pdf_file.name, "rb") as file:
|
14 |
+
doc = fitz.open(file)
|
15 |
+
for page in doc:
|
16 |
+
text += page.get_text()
|
17 |
+
|
18 |
+
# split the text into several chunks
|
19 |
+
text_splitter = CharacterTextSplitter(
|
20 |
+
separator = "\n",
|
21 |
+
chunk_size = 1000,
|
22 |
+
chunk_overlap = 200,
|
23 |
+
length_function = len,
|
24 |
+
)
|
25 |
+
texts = text_splitter.split_text(text)
|
26 |
+
|
27 |
+
# download embedding from OpenAI
|
28 |
+
embeddings = OpenAIEmbeddings()
|
29 |
+
docsearch = FAISS.from_texts(texts, embeddings)
|
30 |
+
|
31 |
+
chain = load_qa_chain(OpenAI(), chain_type="stuff")
|
32 |
+
|
33 |
+
docs = docsearch.similarity_search(prompt)
|
34 |
+
answer = chain.run(input_documents = docs, question = prompt)
|
35 |
+
|
36 |
+
return answer
|
37 |
+
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error: {str(e)}"
|
40 |
+
|
41 |
+
iface = gr.Interface(
|
42 |
+
read_pdf,
|
43 |
+
inputs=["file","text"],
|
44 |
+
outputs="text",
|
45 |
+
title="PDF Reader",
|
46 |
+
description="Upload a PDF file!",
|
47 |
+
)
|
48 |
+
iface.launch()
|