forked from youssefHosni/Chat-with-Pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
100 lines (78 loc) · 2.98 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import tempfile
from PIL import Image
# Import os to set API key
import os
# Import OpenAI as main LLM service
from langchain.llms import OpenAI
from langchain.embeddings import OpenAIEmbeddings
# Bring in streamlit for UI/app interface
import streamlit as st
# Import PDF document loaders...there's other ones as well!
from langchain.document_loaders import PyPDFLoader
# Import chroma as the vector store
from langchain.vectorstores import Chroma
# Import vector store stuff
from langchain.agents.agent_toolkits import (
create_vectorstore_agent,
VectorStoreToolkit,
VectorStoreInfo
)
# Set the title and subtitle of the app
st.title('🦜🔗 PDF-Chat: Interact with Your PDFs in a Conversational Way')
st.subheader('Load your PDF, ask questions, and receive answers directly from the document.')
# Load the image
image = Image.open('PDF-Chat App.png')
st.image(image)
# Loading the Pdf file and return a temporary path for it
st.subheader('Upload your pdf')
uploaded_file = st.file_uploader('', type=(['pdf',"tsv","csv","txt","tab","xlsx","xls"]))
temp_file_path = os.getcwd()
while uploaded_file is None:
x = 1
if uploaded_file is not None:
# Save the uploaded file to a temporary location
temp_dir = tempfile.TemporaryDirectory()
temp_file_path = os.path.join(temp_dir.name, uploaded_file.name)
with open(temp_file_path, "wb") as temp_file:
temp_file.write(uploaded_file.read())
st.write("Full path of the uploaded file:", temp_file_path)
# Set APIkey for OpenAI Service
# Can sub this out for other LLM providers
os.environ['OPENAI_API_KEY'] = # Your OpenAI API Key
# Create instance of OpenAI LLM
llm = OpenAI(temperature=0.1, verbose=True)
embeddings = OpenAIEmbeddings()
# Create and load PDF Loader
loader = PyPDFLoader(temp_file_path)
# Split pages from pdf
pages = loader.load_and_split()
# Load documents into vector database aka ChromaDB
store = Chroma.from_documents(pages, embeddings, collection_name='Pdf')
# Create vectorstore info object
vectorstore_info = VectorStoreInfo(
name="Pdf",
description=" A pdf file to answer your questions",
vectorstore=store
)
# Convert the document store into a langchain toolkit
toolkit = VectorStoreToolkit(vectorstore_info=vectorstore_info)
# Add the toolkit to an end-to-end LC
agent_executor = create_vectorstore_agent(
llm=llm,
toolkit=toolkit,
verbose=True
)
# Create a text input box for the user
prompt = st.text_input('Input your prompt here')
# If the user hits enter
if prompt:
# Then pass the prompt to the LLM
response = agent_executor.run(prompt)
# ...and write it out to the screen
st.write(response)
# With a streamlit expander
with st.expander('Document Similarity Search'):
# Find the relevant pages
search = store.similarity_search_with_score(prompt)
# Write out the first
st.write(search[0][0].page_content)