viboognesh commited on
Commit
430c4c1
1 Parent(s): 6ec547e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +96 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ import streamlit as st
3
+ from PyPDF2 import PdfReader
4
+ from langchain.text_splitter import CharacterTextSplitter
5
+ from langchain_openai import OpenAIEmbeddings
6
+ from langchain_community.vectorstores import Chroma
7
+ from langchain_openai import ChatOpenAI
8
+ from langchain.memory import ConversationBufferMemory
9
+ from langchain.chains import ConversationalRetrievalChain
10
+
11
+ def extract_text_from_pdf(pdf_file_obj):
12
+ pdf_reader = PdfReader(BytesIO(pdf_file_obj.getbuffer()))
13
+ text = ""
14
+ for page_num in range(len(pdf_reader.pages)):
15
+ page_obj = pdf_reader.pages[page_num]
16
+ text += page_obj.extract_text()
17
+ return text
18
+
19
+ def get_text_chunks(text):
20
+ text_splitter = CharacterTextSplitter(
21
+ separator="\n",
22
+ chunk_size=1000,
23
+ chunk_overlap=200,
24
+ length_function=len
25
+ )
26
+ chunks = text_splitter.split_text(text)
27
+ return chunks
28
+
29
+
30
+ def get_vectorstore(text_chunks):
31
+ metadatas = [{"source": f"{i}-pl"} for i in range(len(text_chunks))]
32
+ embeddings = OpenAIEmbeddings()
33
+ vectorstore = Chroma.from_texts(texts=text_chunks, embedding=embeddings)
34
+ return vectorstore
35
+
36
+
37
+ def get_conversation_chain(vectorstore):
38
+ llm = ChatOpenAI()
39
+
40
+ memory = ConversationBufferMemory(
41
+ memory_key='chat_history', return_messages=True)
42
+ conversation_chain = ConversationalRetrievalChain.from_llm(
43
+ llm=llm,
44
+ retriever=vectorstore.as_retriever(),
45
+ memory=memory
46
+ )
47
+ return conversation_chain
48
+
49
+
50
+ def handle_userinput(user_question):
51
+ response = st.session_state.conversation({'question': user_question})
52
+ st.session_state.chat_history = response['chat_history']
53
+
54
+ for i, message in enumerate(st.session_state.chat_history):
55
+ if i % 2 == 0:
56
+ st.markdown(("User: "+message.content))
57
+ else:
58
+ st.markdown(("AI: "+message.content))
59
+
60
+
61
+ def main():
62
+ load_dotenv()
63
+
64
+ if "conversation" not in st.session_state:
65
+ st.session_state.conversation = None
66
+ if "chat_history" not in st.session_state:
67
+ st.session_state.chat_history = None
68
+
69
+ if st.session_state.conversation is not None:
70
+ st.header("Ask questions from your PDF:books:")
71
+ user_question = st.chat_input("Ask a question about your documents:")
72
+ if user_question:
73
+ handle_userinput(user_question)
74
+
75
+ if st.session_state.conversation is None:
76
+ st.header("Upload your PDF here")
77
+ pdf_doc = st.file_uploader("Browse your file here",type="pdf")
78
+ if pdf_doc is not None:
79
+ with st.spinner("Processing"):
80
+ # get pdf text
81
+ raw_text = extract_text_from_pdf(pdf_doc)
82
+
83
+ # get the text chunks
84
+ text_chunks = get_text_chunks(raw_text)
85
+
86
+ # create vector store
87
+ vectorstore = get_vectorstore(text_chunks)
88
+
89
+ # create conversation chain
90
+ st.session_state.conversation = get_conversation_chain(
91
+ vectorstore)
92
+
93
+ st.rerun()
94
+
95
+ if __name__ == '__main__':
96
+ main()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain
2
+ langchain-community
3
+ langchain-openai
4
+ pypdf2
5
+ chromadb
6
+ streamlit