Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain==0.0.154
|
| 2 |
+
PyPDF2==3.0.1
|
| 3 |
+
python-dotenv==1.0.0
|
| 4 |
+
streamlit==1.18.1
|
| 5 |
+
faiss-cpu==1.7.4
|
| 6 |
+
streamlit-extras
|
| 7 |
+
'''
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
import streamlit as st
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
import pickle
|
| 13 |
+
from PyPDF2 import PdfReader
|
| 14 |
+
from streamlit_extras.add_vertical_space import add_vertical_space
|
| 15 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 16 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 17 |
+
from langchain.vectorstores import FAISS
|
| 18 |
+
from langchain.llms import OpenAI
|
| 19 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 20 |
+
from langchain.callbacks import get_openai_callback
|
| 21 |
+
import os
|
| 22 |
+
|
| 23 |
+
# Sidebar contents
|
| 24 |
+
with st.sidebar:
|
| 25 |
+
st.title('🤗💬 LLM Chat App')
|
| 26 |
+
st.markdown('''
|
| 27 |
+
## About
|
| 28 |
+
This app is an LLM-powered chatbot built using:
|
| 29 |
+
- [Streamlit](https://streamlit.io/)
|
| 30 |
+
- [LangChain](https://python.langchain.com/)
|
| 31 |
+
- [OpenAI](https://platform.openai.com/docs/models) LLM model
|
| 32 |
+
|
| 33 |
+
''')
|
| 34 |
+
add_vertical_space(5)
|
| 35 |
+
st.write('Made with ❤️ by [Prompt Engineer](https://youtube.com/@engineerprompt)')
|
| 36 |
+
|
| 37 |
+
load_dotenv()
|
| 38 |
+
|
| 39 |
+
def main():
|
| 40 |
+
st.header("Chat with PDF 💬")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# upload a PDF file
|
| 44 |
+
pdf = st.file_uploader("Upload your PDF", type='pdf')
|
| 45 |
+
|
| 46 |
+
# st.write(pdf)
|
| 47 |
+
if pdf is not None:
|
| 48 |
+
pdf_reader = PdfReader(pdf)
|
| 49 |
+
|
| 50 |
+
text = ""
|
| 51 |
+
for page in pdf_reader.pages:
|
| 52 |
+
text += page.extract_text()
|
| 53 |
+
|
| 54 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 55 |
+
chunk_size=1000,
|
| 56 |
+
chunk_overlap=200,
|
| 57 |
+
length_function=len
|
| 58 |
+
)
|
| 59 |
+
chunks = text_splitter.split_text(text=text)
|
| 60 |
+
|
| 61 |
+
# # embeddings
|
| 62 |
+
store_name = pdf.name[:-4]
|
| 63 |
+
st.write(f'{store_name}')
|
| 64 |
+
# st.write(chunks)
|
| 65 |
+
|
| 66 |
+
if os.path.exists(f"{store_name}.pkl"):
|
| 67 |
+
with open(f"{store_name}.pkl", "rb") as f:
|
| 68 |
+
VectorStore = pickle.load(f)
|
| 69 |
+
# st.write('Embeddings Loaded from the Disk')s
|
| 70 |
+
else:
|
| 71 |
+
embeddings = OpenAIEmbeddings()
|
| 72 |
+
VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
|
| 73 |
+
with open(f"{store_name}.pkl", "wb") as f:
|
| 74 |
+
pickle.dump(VectorStore, f)
|
| 75 |
+
|
| 76 |
+
# embeddings = OpenAIEmbeddings()
|
| 77 |
+
# VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
|
| 78 |
+
|
| 79 |
+
# Accept user questions/query
|
| 80 |
+
query = st.text_input("Ask questions about your PDF file:")
|
| 81 |
+
# st.write(query)
|
| 82 |
+
|
| 83 |
+
if query:
|
| 84 |
+
docs = VectorStore.similarity_search(query=query, k=3)
|
| 85 |
+
|
| 86 |
+
llm = OpenAI()
|
| 87 |
+
chain = load_qa_chain(llm=llm, chain_type="stuff")
|
| 88 |
+
with get_openai_callback() as cb:
|
| 89 |
+
response = chain.run(input_documents=docs, question=query)
|
| 90 |
+
print(cb)
|
| 91 |
+
st.write(response)
|
| 92 |
+
|
| 93 |
+
if __name__ == '__main__':
|
| 94 |
+
main()
|