notabaka commited on
Commit
b21d556
·
1 Parent(s): b238fb0
Files changed (2) hide show
  1. app.py +107 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from PyPDF2 import PdfReader
4
+ import openpyxl
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain.embeddings import GooglePalmEmbeddings
7
+ from langchain.llms import GooglePalm
8
+ from langchain.vectorstores import FAISS
9
+ from langchain.chains import ConversationalRetrievalChain
10
+ from langchain.memory import ConversationBufferMemory
11
+
12
+ os.environ['GOOGLE_API_KEY'] = 'AIzaSyD8uzXToT4I2ABs7qo_XiuKh8-L2nuWCEM'
13
+
14
+ def get_pdf_text(pdf_docs):
15
+ text = ""
16
+ for pdf in pdf_docs:
17
+ pdf_reader = PdfReader(pdf)
18
+ for page in pdf_reader.pages:
19
+ text += page.extract_text()
20
+ return text
21
+
22
+ def get_excel_text(excel_docs):
23
+ text = ""
24
+ for excel_doc in excel_docs:
25
+ workbook = openpyxl.load_workbook(filename=excel_doc)
26
+ for sheet in workbook:
27
+ for row in sheet:
28
+ for cell in row:
29
+ text += str(cell.value) + " "
30
+ return text.strip()
31
+
32
+
33
+ def get_text_chunks(text):
34
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)
35
+ chunks = text_splitter.split_text(text)
36
+ return chunks
37
+
38
+ def get_vector_store(text_chunks):
39
+ embeddings = GooglePalmEmbeddings()
40
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
41
+ return vector_store
42
+
43
+ def get_conversational_chain(vector_store):
44
+ llm = GooglePalm()
45
+ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
46
+ conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=vector_store.as_retriever(), memory=memory)
47
+ return conversation_chain
48
+
49
+ def get_user_input(user_question):
50
+ with st.container():
51
+ response = st.session_state.conversation({'question': user_question})
52
+ st.session_state.chatHistory = response['chat_history']
53
+ file_contents = ""
54
+ left , right = st.columns((2,1))
55
+ with left:
56
+ for i, message in enumerate(st.session_state.chatHistory):
57
+ if i % 2 == 0:
58
+ st.write("User: ", message.content)
59
+ else:
60
+ st.write("Bot: ", message.content)
61
+ st.success("Done !")
62
+ with right:
63
+ for message in st.session_state.chatHistory:
64
+ file_contents += f"{message.content}\n"
65
+ file_name = "Chat_History.txt"
66
+
67
+ def main():
68
+ st.set_page_config("DocChat")
69
+ st.header("DocChat - Chat with multiple documents")
70
+ st.write("---")
71
+ with st.container():
72
+ with st.sidebar:
73
+ st.title("Settings")
74
+ st.subheader("Upload Documents")
75
+ st.markdown("**PDF files:**")
76
+ pdf_docs = st.file_uploader("Upload PDF Files", accept_multiple_files=True)
77
+ if st.button("Process PDF file"):
78
+ with st.spinner("Processing PDFs..."):
79
+ raw_text = get_pdf_text(pdf_docs)
80
+ text_chunks = get_text_chunks(raw_text)
81
+ vector_store = get_vector_store(text_chunks)
82
+ st.session_state.conversation = get_conversational_chain(vector_store)
83
+ st.success("PDF processed successfully!")
84
+
85
+ st.markdown("**Excel files:**")
86
+ excel_docs = st.file_uploader("Upload Excel Files", accept_multiple_files=True)
87
+ if st.button("Process Excel file"):
88
+ with st.spinner("Processing Excel files..."):
89
+ raw_text = get_excel_text(excel_docs)
90
+ text_chunks = get_text_chunks(raw_text)
91
+ vector_store = get_vector_store(text_chunks)
92
+ st.session_state.conversation = get_conversational_chain(vector_store)
93
+ st.success("Excel file processed successfully!")
94
+
95
+ with st.container():
96
+ st.subheader("Document Q&A")
97
+ st.write('Ask a question : ')
98
+ user_question = st.text_input("Ask a Question from the document")
99
+ if "conversation" not in st.session_state:
100
+ st.session_state.conversation = None
101
+ if "chatHistory" not in st.session_state:
102
+ st.session_state.chatHistory = None
103
+ if user_question:
104
+ get_user_input(user_question)
105
+
106
+ if __name__ == "__main__":
107
+ main()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ google-generativeai
2
+ langchain
3
+ PyPDF2
4
+ faiss-cpu
5
+ streamlit
6
+ openpyxl