sanjay7178 commited on
Commit
aa395d3
·
1 Parent(s): 4cce5d5

Upload 2 files

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