Anne31415 commited on
Commit
7c95914
1 Parent(s): 7d51017

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(':orange[BinDoc GmbH]')
17
+ st.markdown(
18
+ "Experience the future of document interaction with the revolutionary"
19
+ )
20
+
21
+ st.markdown("**BinDocs Chat App**.")
22
+
23
+
24
+ st.markdown("Harnessing the power of a Large Language Model and AI technology,")
25
+
26
+
27
+
28
+ st.markdown("this innovative platform redefines PDF engagement,")
29
+
30
+ st.markdown("enabling dynamic conversations that bridge the gap between")
31
+ st.markdown("human and machine intelligence.")
32
+
33
+
34
+
35
+ add_vertical_space(3) # Add more vertical space between text blocks
36
+ st.write('Made with ❤️ by Anne')
37
+
38
+
39
+ openai_api_key = st.text_input("Enter your OpenAI API key:")
40
+ pdf_path = ""
41
+
42
+
43
+ def load_pdf(file_path):
44
+ pdf_reader = PdfReader(file_path)
45
+ text = ""
46
+ for page in pdf_reader.pages:
47
+ text += page.extract_text()
48
+
49
+ text_splitter = RecursiveCharacterTextSplitter(
50
+ chunk_size=1000,
51
+ chunk_overlap=200,
52
+ length_function=len
53
+ )
54
+ chunks = text_splitter.split_text(text=text)
55
+
56
+ store_name, _ = os.path.splitext(os.path.basename(file_path))
57
+
58
+ if os.path.exists(f"{store_name}.pkl"):
59
+ with open(f"{store_name}.pkl", "rb") as f:
60
+ VectorStore = pickle.load(f)
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
+ return VectorStore
68
+
69
+
70
+
71
+ def load_chatbot(openai_api_key):
72
+ openai_config = {
73
+ "api_key": openai_api_key
74
+ }
75
+ return load_qa_chain(llm=OpenAI(config=openai_config), chain_type="stuff")
76
+
77
+
78
+ def main():
79
+ st.title("BinDocs Chat App")
80
+
81
+
82
+ uploaded_pdf = st.file_uploader("Upload a PDF file:", type=["pdf"])
83
+
84
+ if uploaded_pdf is not None:
85
+ pdf_path = uploaded_pdf
86
+
87
+
88
+ if "chat_history" not in st.session_state:
89
+ st.session_state['chat_history'] = []
90
+
91
+ display_chat_history(st.session_state['chat_history'])
92
+
93
+ st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
94
+ st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
95
+ st.write("<!-- End Spacer -->", unsafe_allow_html=True)
96
+
97
+ new_messages_placeholder = st.empty()
98
+
99
+ if pdf_path is not None:
100
+ query = st.text_input("Ask questions about your PDF file (in any preferred language):")
101
+
102
+ if st.button("Ask") or (not st.session_state['chat_history'] and query) or (st.session_state['chat_history'] and query != st.session_state['chat_history'][-1][1]):
103
+ st.session_state['chat_history'].append(("User", query, "new"))
104
+
105
+ loading_message = st.empty()
106
+ loading_message.text('Bot is thinking...')
107
+
108
+ VectorStore = load_pdf(pdf_path)
109
+ chain = load_chatbot()
110
+ docs = VectorStore.similarity_search(query=query, k=3)
111
+ with get_openai_callback() as cb:
112
+ response = chain.run(input_documents=docs, question=query)
113
+
114
+ st.session_state['chat_history'].append(("Bot", response, "new"))
115
+
116
+ # Display new messages at the bottom
117
+ new_messages = st.session_state['chat_history'][-2:]
118
+ for chat in new_messages:
119
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
120
+ new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
121
+
122
+ # Scroll to the latest response using JavaScript
123
+ st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
124
+
125
+ loading_message.empty()
126
+
127
+ # Clear the input field by setting the query variable to an empty string
128
+ query = ""
129
+
130
+ # Mark all messages as old after displaying
131
+ st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
132
+
133
+
134
+
135
+ def display_chat_history(chat_history):
136
+ for chat in chat_history:
137
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
138
+ st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
139
+
140
+ if __name__ == "__main__":
141
+ main()
142
+