Upload 2 files
Browse files- app.py +109 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PyPDF2 import PdfReader
|
| 2 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain.vectorstores import FAISS
|
| 5 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 6 |
+
from langchain.chains import load_chain
|
| 7 |
+
from langchain.llms import OpenAI
|
| 8 |
+
import streamlit as st
|
| 9 |
+
import pyautogui
|
| 10 |
+
import os, shutil
|
| 11 |
+
|
| 12 |
+
def delete_directory(directory_path):
|
| 13 |
+
try:
|
| 14 |
+
shutil.rmtree(directory_path)
|
| 15 |
+
print(f"Directory '{directory_path}' successfully deleted.")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Error deleting directory '{directory_path}': {e}")
|
| 18 |
+
|
| 19 |
+
st.set_page_config(page_title="Query any Pdf", page_icon="📄")
|
| 20 |
+
|
| 21 |
+
st.title("📄 PDF Query Bot 📄")
|
| 22 |
+
st.write("Made with ❤️ by Mainak")
|
| 23 |
+
|
| 24 |
+
def return_response(query,document_search,chain):
|
| 25 |
+
query = query
|
| 26 |
+
docs = document_search.similarity_search(query)
|
| 27 |
+
result = chain.run(input_documents=docs, question=query)
|
| 28 |
+
return result
|
| 29 |
+
|
| 30 |
+
uploaded_file = st.file_uploader("Upload a PDF File", type=["pdf"])
|
| 31 |
+
|
| 32 |
+
# API key input box
|
| 33 |
+
api_key = st.text_input("Enter Your OpenAI API Key",type="password")
|
| 34 |
+
|
| 35 |
+
if not uploaded_file:
|
| 36 |
+
try:
|
| 37 |
+
delete_directory('faiss_index')
|
| 38 |
+
except:
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
if st.button('Submit'):
|
| 42 |
+
if api_key:
|
| 43 |
+
if uploaded_file is not None:
|
| 44 |
+
# Read text from the uploaded file
|
| 45 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
| 46 |
+
with st.spinner('Wait for it...'):
|
| 47 |
+
pdfreader = PdfReader(uploaded_file)
|
| 48 |
+
# read text from pdf
|
| 49 |
+
raw_text = ''
|
| 50 |
+
for i, page in enumerate(pdfreader.pages):
|
| 51 |
+
content = page.extract_text()
|
| 52 |
+
if content:
|
| 53 |
+
raw_text += content
|
| 54 |
+
|
| 55 |
+
text_splitter = CharacterTextSplitter(
|
| 56 |
+
separator = "\n",
|
| 57 |
+
chunk_size = 800,
|
| 58 |
+
chunk_overlap = 200,
|
| 59 |
+
length_function = len,
|
| 60 |
+
)
|
| 61 |
+
texts = text_splitter.split_text(raw_text)
|
| 62 |
+
embeddings = OpenAIEmbeddings()
|
| 63 |
+
document_search = FAISS.from_texts(texts, embeddings)
|
| 64 |
+
document_search.save_local("faiss_index")
|
| 65 |
+
else:
|
| 66 |
+
st.warning("Please enter your Pdf File")
|
| 67 |
+
else:
|
| 68 |
+
st.warning("Please enter your API key")
|
| 69 |
+
if os.path.exists("faiss_index"):
|
| 70 |
+
# if st.checkbox("chat"):
|
| 71 |
+
if api_key:
|
| 72 |
+
if uploaded_file is not None:
|
| 73 |
+
if "messages" not in st.session_state:
|
| 74 |
+
st.session_state.messages = []
|
| 75 |
+
|
| 76 |
+
# Display chat messages from history on app rerun
|
| 77 |
+
for message in st.session_state.messages:
|
| 78 |
+
with st.chat_message(message["role"]):
|
| 79 |
+
st.markdown(message["content"])
|
| 80 |
+
if prompt := st.chat_input("What is up? Type 'exit' for leave the chat"):
|
| 81 |
+
# Display user message in chat message container
|
| 82 |
+
with st.chat_message("user"):
|
| 83 |
+
st.markdown(prompt)
|
| 84 |
+
# Add user message to chat history
|
| 85 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 86 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
| 87 |
+
embeddings = OpenAIEmbeddings()
|
| 88 |
+
document_search = FAISS.load_local("faiss_index", embeddings)
|
| 89 |
+
chain = load_qa_chain(OpenAI(), chain_type="stuff")
|
| 90 |
+
if prompt is None:
|
| 91 |
+
re='Ask me anything about the pdf'
|
| 92 |
+
elif prompt=='exit':
|
| 93 |
+
delete_directory('faiss_index')
|
| 94 |
+
pyautogui.hotkey('f5') #Simulates F5 key press = page refresh
|
| 95 |
+
else:
|
| 96 |
+
with st.spinner('Typping...'):
|
| 97 |
+
re=return_response(str(prompt),document_search,chain)
|
| 98 |
+
response = f"PDF Mate: {re}"
|
| 99 |
+
# Display assistant response in chat message container
|
| 100 |
+
with st.chat_message("assistant"):
|
| 101 |
+
st.markdown(response)
|
| 102 |
+
# Add assistant response to chat history
|
| 103 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 104 |
+
else:
|
| 105 |
+
st.warning("Please enter your Pdf File")
|
| 106 |
+
else:
|
| 107 |
+
st.warning("Please enter your API key")
|
| 108 |
+
else:
|
| 109 |
+
pass
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
openai
|
| 3 |
+
PyPDF2
|
| 4 |
+
faiss-cpu
|
| 5 |
+
tiktoken
|
| 6 |
+
pinecone-client
|
| 7 |
+
streamlit
|
| 8 |
+
pyautogui
|