Anne31415 commited on
Commit
27dbdfd
·
1 Parent(s): 24ecc54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -54
app.py CHANGED
@@ -1,27 +1,17 @@
1
  import streamlit as st
2
- import os
3
  import pickle
4
- from streamlit_extras.add_vertical_space import add_vertical_space
5
  from huggingface_hub import Repository
6
  from PyPDF2 import PdfReader
 
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  from langchain.embeddings.openai import OpenAIEmbeddings
9
  from langchain.vectorstores import FAISS
10
  from langchain.llms import OpenAI
11
  from langchain.chains.question_answering import load_qa_chain
12
  from langchain.callbacks import get_openai_callback
 
13
 
14
- # Step 1: Clone the Dataset Repository
15
- repo = Repository(
16
- local_dir="Private_Book", # Local directory to clone the repository
17
- repo_type="dataset", # Specify that this is a dataset repository
18
- clone_from="Anne31415/Private_Book", # Replace with your repository URL
19
- token=os.getenv("HUB_TOKEN") # Use the secret token to authenticate
20
- )
21
- repo.git_pull() # Pull the latest changes (if any)
22
-
23
- # Step 2: Load the PDF File
24
- pdf_file_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
25
 
26
  def cloud_button(label, key=None, color=None, overlap=30):
27
  button_id = f"cloud-button-{key or label}"
@@ -99,11 +89,43 @@ def cloud_button(label, key=None, color=None, overlap=30):
99
 
100
 
101
 
102
- # Example usage with a specified overlap value
103
- cloud_button("Short Text", color="1", overlap=30)
104
- cloud_button("This is a longer piece of text", color="2", overlap=30)
105
- cloud_button("This is an even longer piece of text to test the cloud button", color="3", overlap=30)
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
 
109
  def load_pdf(file_path):
@@ -132,58 +154,37 @@ def load_pdf(file_path):
132
 
133
  return VectorStore
134
 
 
 
135
  def load_chatbot():
136
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
137
 
138
- def display_chat_history(chat_history):
139
- for chat in chat_history:
140
- background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
141
- 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)
142
-
143
  def main():
144
- with st.sidebar:
145
- st.title('BinDoc GmbH')
146
- st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
147
-
148
- add_vertical_space(1) # Adjust as per the desired spacing
149
-
150
- st.markdown("""
151
- Hello! I’m here to assist you with:<br><br>
152
- 📘 **Glossary Inquiries:**<br>
153
- I can clarify terms like "DiGA", "AOP", or "BfArM", providing clear and concise explanations to help you understand our content better.<br><br>
154
-
155
- 🆘 **Help Page Navigation:**<br>
156
- Ask me if you forgot your password or want to know more about topics related to the platform.<br><br>
157
-
158
- 📰 **Latest Whitepapers Insights:**<br>
159
- Curious about our recent publications? Feel free to ask about our latest whitepapers!<br><br>
160
- """, unsafe_allow_html=True)
161
-
162
- add_vertical_space(1) # Adjust as per the desired spacing
163
- st.write('Made with ❤️ by BinDoc GmbH')
164
- api_key = os.getenv("OPENAI_API_KEY")
165
 
166
  hide_streamlit_style = """
167
- <style>
168
- #MainMenu {visibility: hidden;}
169
- footer {visibility: hidden;}
170
- </style>
171
- """
172
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
173
 
 
174
  # Main content
175
  st.title("Welcome to BinDocs ChatBot! 🤖")
176
-
177
-
178
-
179
-
180
- pdf_path = "path_to_your_pdf_file.pdf" # Update this to the path of your PDF file
181
  if not os.path.exists(pdf_path):
182
  st.error("File not found. Please check the file path.")
183
  return
184
 
185
  if "chat_history" not in st.session_state:
186
  st.session_state['chat_history'] = []
 
 
 
 
187
 
188
  display_chat_history(st.session_state['chat_history'])
189
 
@@ -195,24 +196,57 @@ def main():
195
 
196
  if pdf_path is not None:
197
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
 
 
 
 
 
 
 
 
 
 
 
 
198
 
 
199
  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]):
200
  st.session_state['chat_history'].append(("User", query, "new"))
 
201
  loading_message = st.empty()
202
  loading_message.text('Bot is thinking...')
 
203
  VectorStore = load_pdf(pdf_path)
204
  chain = load_chatbot()
205
  docs = VectorStore.similarity_search(query=query, k=3)
206
- response = chain.run(input_documents=docs, question=query)
 
 
207
  st.session_state['chat_history'].append(("Bot", response, "new"))
 
 
208
  new_messages = st.session_state['chat_history'][-2:]
209
  for chat in new_messages:
210
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
211
  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)
 
 
212
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
 
213
  loading_message.empty()
 
 
214
  query = ""
 
 
215
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
216
 
 
 
 
 
 
 
 
217
  if __name__ == "__main__":
218
  main()
 
1
  import streamlit as st
2
+ from dotenv import load_dotenv
3
  import pickle
 
4
  from huggingface_hub import Repository
5
  from PyPDF2 import PdfReader
6
+ from streamlit_extras.add_vertical_space import add_vertical_space
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  from langchain.embeddings.openai import OpenAIEmbeddings
9
  from langchain.vectorstores import FAISS
10
  from langchain.llms import OpenAI
11
  from langchain.chains.question_answering import load_qa_chain
12
  from langchain.callbacks import get_openai_callback
13
+ import os
14
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def cloud_button(label, key=None, color=None, overlap=30):
17
  button_id = f"cloud-button-{key or label}"
 
89
 
90
 
91
 
 
 
 
 
92
 
93
+ # Step 1: Clone the Dataset Repository
94
+ repo = Repository(
95
+ local_dir="Private_Book", # Local directory to clone the repository
96
+ repo_type="dataset", # Specify that this is a dataset repository
97
+
98
+ clone_from="Anne31415/Private_Book", # Replace with your repository URL
99
+
100
+ token=os.environ["HUB_TOKEN"] # Use the secret token to authenticate
101
+ )
102
+ repo.git_pull() # Pull the latest changes (if any)
103
+
104
+ # Step 2: Load the PDF File
105
+ pdf_file_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
106
+
107
+ with st.sidebar:
108
+ st.title('BinDoc GmbH')
109
+ st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
110
+
111
+ add_vertical_space(1) # Adjust as per the desired spacing
112
+
113
+ st.markdown("""
114
+ Hello! I’m here to assist you with:<br><br>
115
+ 📘 **Glossary Inquiries:**<br>
116
+ I can clarify terms like "DiGA", "AOP", or "BfArM", providing clear and concise explanations to help you understand our content better.<br><br>
117
+ 🆘 **Help Page Navigation:**<br>
118
+ Ask me if you forgot your password or want to know more about topics related to the platform.<br><br>
119
+ 📰 **Latest Whitepapers Insights:**<br>
120
+ Curious about our recent publications? Feel free to ask about our latest whitepapers!<br><br>
121
+ """, unsafe_allow_html=True)
122
+
123
+ add_vertical_space(1) # Adjust as per the desired spacing
124
+
125
+ st.write('Made with ❤️ by BinDoc GmbH')
126
+
127
+ api_key = os.getenv("OPENAI_API_KEY")
128
+ # Retrieve the API key from st.secrets
129
 
130
 
131
  def load_pdf(file_path):
 
154
 
155
  return VectorStore
156
 
157
+
158
+
159
  def load_chatbot():
160
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
161
 
 
 
 
 
 
162
  def main():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  hide_streamlit_style = """
165
+ <style>
166
+ #MainMenu {visibility: hidden;}
167
+ footer {visibility: hidden;}
168
+ </style>
169
+ """
170
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
171
 
172
+
173
  # Main content
174
  st.title("Welcome to BinDocs ChatBot! 🤖")
175
+
176
+ # Directly specifying the path to the PDF file
177
+ pdf_path = pdf_file_path
 
 
178
  if not os.path.exists(pdf_path):
179
  st.error("File not found. Please check the file path.")
180
  return
181
 
182
  if "chat_history" not in st.session_state:
183
  st.session_state['chat_history'] = []
184
+
185
+ if "button_clicked" not in st.session_state:
186
+ st.session_state['button_clicked'] = None
187
+
188
 
189
  display_chat_history(st.session_state['chat_history'])
190
 
 
196
 
197
  if pdf_path is not None:
198
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
199
+
200
+ cloud_button("Was genau ist ein Belegarzt?", key="button1", color="1")
201
+ cloud_button("Wofür wird die Alpha-ID verwendet?", key="button2", color="2")
202
+ cloud_button("Was sind die Vorteile des ambulanten operierens?", key="button3", color="3")
203
+ cloud_button("Was kann ich mit dem Prognose-Analyse Toll machen?", key="button4", color="4")
204
+ cloud_button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?", key="button5", color="5")
205
+ cloud_button("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?", key="button6", color="6")
206
+
207
+ # Handle button clicks
208
+ if st.session_state['button_clicked']:
209
+ query = st.session_state['button_clicked']
210
+ st.session_state['button_clicked'] = None
211
 
212
+
213
  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]):
214
  st.session_state['chat_history'].append(("User", query, "new"))
215
+
216
  loading_message = st.empty()
217
  loading_message.text('Bot is thinking...')
218
+
219
  VectorStore = load_pdf(pdf_path)
220
  chain = load_chatbot()
221
  docs = VectorStore.similarity_search(query=query, k=3)
222
+ with get_openai_callback() as cb:
223
+ response = chain.run(input_documents=docs, question=query)
224
+
225
  st.session_state['chat_history'].append(("Bot", response, "new"))
226
+
227
+ # Display new messages at the bottom
228
  new_messages = st.session_state['chat_history'][-2:]
229
  for chat in new_messages:
230
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
231
  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)
232
+
233
+ # Scroll to the latest response using JavaScript
234
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
235
+
236
  loading_message.empty()
237
+
238
+ # Clear the input field by setting the query variable to an empty string
239
  query = ""
240
+
241
+ # Mark all messages as old after displaying
242
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
243
 
244
+
245
+
246
+ def display_chat_history(chat_history):
247
+ for chat in chat_history:
248
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
249
+ 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)
250
+
251
  if __name__ == "__main__":
252
  main()