dorumiruu commited on
Commit
bb156d3
·
verified ·
1 Parent(s): 270d10a

Create app.py

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