dineshabeysinghe
commited on
Commit
•
2057158
1
Parent(s):
558c79e
Create app.py
Browse files
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 |
+
def get_text_chunks(text):
|
33 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)
|
34 |
+
chunks = text_splitter.split_text(text)
|
35 |
+
return chunks
|
36 |
+
|
37 |
+
def get_vector_store(text_chunks):
|
38 |
+
embeddings = GooglePalmEmbeddings()
|
39 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
40 |
+
return vector_store
|
41 |
+
|
42 |
+
def get_conversational_chain(vector_store):
|
43 |
+
llm = GooglePalm()
|
44 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
45 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=vector_store.as_retriever(), memory=memory)
|
46 |
+
return conversation_chain
|
47 |
+
|
48 |
+
def get_user_input(user_question):
|
49 |
+
with st.container():
|
50 |
+
response = st.session_state.conversation({'question': user_question})
|
51 |
+
st.session_state.chatHistory = response['chat_history']
|
52 |
+
file_contents = ""
|
53 |
+
left , right = st.columns((2,1))
|
54 |
+
with left:
|
55 |
+
for i, message in enumerate(st.session_state.chatHistory):
|
56 |
+
if i % 2 == 0:
|
57 |
+
st.markdown(f'<div style="background-color: rgb(30 24 17 / 77%); border-radius: 10px; padding: 10px; margin-bottom: 5px; text-align: end;"><span style="text-align: end;">User:</span> {message.content}</div>', unsafe_allow_html=True)
|
58 |
+
else:
|
59 |
+
st.markdown(f'<div style="background-color: rgb(145 74 1 / 25%); border-radius: 10px; padding: 10px; margin-bottom: 5px; ">Bot: {message.content}</div>', unsafe_allow_html=True)
|
60 |
+
with right:
|
61 |
+
for message in st.session_state.chatHistory:
|
62 |
+
file_contents += f"{message.content}\n"
|
63 |
+
file_name = "Chat_History.txt"
|
64 |
+
|
65 |
+
def main():
|
66 |
+
st.set_page_config("DocChat")
|
67 |
+
# Define Streamlit app layout
|
68 |
+
st.markdown("<h3 style='color: orange;'>🧾 DocChat - Chat with multiple documents</h3>", unsafe_allow_html=True)
|
69 |
+
st.caption("🚀 Chat bot developed By :- [Dinesh Abeysinghe](https://www.linkedin.com/in/dinesh-abeysinghe-bb773293) | [GitHub Source Code](https://github.com/dineshabey/AI-TypeTalkChat.git) | [About model](https://arxiv.org/abs/2004.13637) ")
|
70 |
+
st.markdown("<div style= 'text-align: center;'>First need to upload PDF file or Excel file. Then you can start chat with document related things <span style='color: orange;'>Please click like button</span>❤️ and support me and enjoy it.</div>", unsafe_allow_html=True)
|
71 |
+
st.write("---")
|
72 |
+
with st.container():
|
73 |
+
with st.sidebar:
|
74 |
+
st.title("Settings")
|
75 |
+
st.subheader("Upload Documents")
|
76 |
+
st.markdown("**PDF files:**")
|
77 |
+
pdf_docs = st.file_uploader("Upload PDF Files", accept_multiple_files=True)
|
78 |
+
if st.button("Process PDF file"):
|
79 |
+
with st.spinner("Processing PDFs..."):
|
80 |
+
raw_text = get_pdf_text(pdf_docs)
|
81 |
+
text_chunks = get_text_chunks(raw_text)
|
82 |
+
vector_store = get_vector_store(text_chunks)
|
83 |
+
st.session_state.conversation = get_conversational_chain(vector_store)
|
84 |
+
st.success("PDF processed successfully!")
|
85 |
+
|
86 |
+
st.markdown("**Excel files:**")
|
87 |
+
excel_docs = st.file_uploader("Upload Excel Files", accept_multiple_files=True)
|
88 |
+
if st.button("Process Excel file"):
|
89 |
+
with st.spinner("Processing Excel files..."):
|
90 |
+
raw_text = get_excel_text(excel_docs)
|
91 |
+
text_chunks = get_text_chunks(raw_text)
|
92 |
+
vector_store = get_vector_store(text_chunks)
|
93 |
+
st.session_state.conversation = get_conversational_chain(vector_store)
|
94 |
+
st.success("Excel file processed successfully!")
|
95 |
+
|
96 |
+
with st.container():
|
97 |
+
st.subheader("Document Q&A")
|
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()
|