Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -30,53 +30,43 @@ load_dotenv()
|
|
30 |
def main():
|
31 |
st.header('Chat With PDF')
|
32 |
|
33 |
-
pdf = st.file_uploader('Upload Your PDF',type='pdf')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
if
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
text = ''
|
40 |
-
for page in pdf_reader.pages:
|
41 |
-
text = page.extract_text()
|
42 |
-
|
43 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
44 |
-
chunk_size = 1000,
|
45 |
-
chunk_overlap = 200,
|
46 |
-
length_function = len
|
47 |
-
)
|
48 |
-
|
49 |
-
chunks = text_splitter.split_text(text=text)
|
50 |
-
|
51 |
-
# st.write(chunks)
|
52 |
-
|
53 |
-
# embeddings
|
54 |
-
|
55 |
-
store_name = pdf.name[:-4]
|
56 |
-
|
57 |
-
if os.path.exists(f'{store_name}.pkl'):
|
58 |
-
with open(f'{store_name}.pkl','rb') as f:
|
59 |
-
VectorStore = pickle.load(f)
|
60 |
-
else:
|
61 |
-
embeddings = HuggingFaceEmbeddings()
|
62 |
-
VectorStore = FAISS.from_texts(chunks,embedding=embeddings)
|
63 |
-
with open(f'{store_name}.pkl','wb') as f:
|
64 |
-
pickle.dump(VectorStore,f)
|
65 |
-
|
66 |
-
|
67 |
-
# accept user query's
|
68 |
-
|
69 |
-
ask_query = st.text_input('Ask question about PDF : ')
|
70 |
-
|
71 |
-
|
72 |
-
if ask_query:
|
73 |
-
docs = VectorStore.similarity_search(query=ask_query, k=3)
|
74 |
-
# st.write(docs)
|
75 |
-
llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature": 0, "max_length": 64})
|
76 |
-
chain = load_qa_chain(llm=llm, chain_type='stuff')
|
77 |
-
response = chain.run(input_documents=docs, question=ask_query)
|
78 |
-
st.write(response)
|
79 |
-
# st.write(text)
|
80 |
|
81 |
|
82 |
|
|
|
30 |
def main():
|
31 |
st.header('Chat With PDF')
|
32 |
|
33 |
+
pdf = st.file_uploader('Upload Your PDF', type='pdf')
|
34 |
+
|
35 |
+
if pdf is not None:
|
36 |
+
pdf_reader = PdfReader(pdf)
|
37 |
+
|
38 |
+
text = ''
|
39 |
+
for page in pdf_reader.pages:
|
40 |
+
text += page.extract_text()
|
41 |
+
|
42 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
43 |
+
chunk_size=1000,
|
44 |
+
chunk_overlap=200,
|
45 |
+
length_function=len
|
46 |
+
)
|
47 |
+
|
48 |
+
chunks = text_splitter.split_text(text=text)
|
49 |
+
|
50 |
+
store_name = pdf.name[:-4]
|
51 |
+
|
52 |
+
if os.path.exists(f'{store_name}.pkl'):
|
53 |
+
with open(f'{store_name}.pkl', 'rb') as f:
|
54 |
+
VectorStore = pickle.load(f)
|
55 |
+
else:
|
56 |
+
embeddings = HuggingFaceEmbeddings()
|
57 |
+
VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
|
58 |
+
with open(f'{store_name}.pkl', 'wb') as f:
|
59 |
+
pickle.dump(VectorStore, f)
|
60 |
+
|
61 |
+
ask_query = st.text_input('Ask question about PDF: ')
|
62 |
|
63 |
+
if ask_query:
|
64 |
+
docs = VectorStore.similarity_search(query=ask_query, k=3)
|
65 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature": 0, "max_length": 64})
|
66 |
+
chain = load_qa_chain(llm=llm, chain_type='stuff')
|
67 |
+
response = chain.run(input_documents=docs, question=ask_query)
|
68 |
+
st.write(response)
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
|
72 |
|