Anne31415 commited on
Commit
a56964f
·
1 Parent(s): 763a6ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -51
app.py CHANGED
@@ -12,18 +12,27 @@ from langchain.chains.question_answering import load_qa_chain
12
  from langchain.callbacks import get_openai_callback
13
  import os
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Add this function to handle button clicks
16
  def on_button_click(query):
17
  st.session_state['button_clicked'] = query
18
 
19
 
20
 
21
- def display_chat_history(chat_history):
22
- for sender, msg, _ in chat_history:
23
- background_color = "#FFA07A" if sender == "User" else "#caf"
24
- st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{sender}: {msg}</div>", unsafe_allow_html=True)
25
-
26
-
27
 
28
 
29
  def cloud_button(label, query, key=None, color=None, overlap=30):
@@ -96,19 +105,13 @@ def cloud_button(label, query, key=None, color=None, overlap=30):
96
  """
97
  st.markdown(cloud_button_html, unsafe_allow_html=True)
98
 
99
- # Step 1: Clone the Dataset Repository
100
- repo = Repository(
101
- local_dir="Private_Book", # Local directory to clone the repository
102
- repo_type="dataset", # Specify that this is a dataset repository
103
-
104
- clone_from="Anne31415/Private_Book", # Replace with your repository URL
105
-
106
- token=os.environ["HUB_TOKEN"] # Use the secret token to authenticate
107
- )
108
- repo.git_pull() # Pull the latest changes (if any)
109
 
110
- # Step 2: Load the PDF File
111
- pdf_file_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
 
 
 
 
112
 
113
  with st.sidebar:
114
  st.title('BinDoc GmbH')
@@ -167,17 +170,15 @@ def load_chatbot():
167
 
168
  def main():
169
  hide_streamlit_style = """
170
- <style>
171
- #MainMenu {visibility: hidden;}
172
- footer {visibility: hidden;}
173
- </style>
174
- """
175
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
176
 
177
- # Main content
178
  st.title("Welcome to BinDocs ChatBot! 🤖")
179
 
180
- # Directly specifying the path to the PDF file
181
  pdf_path = pdf_file_path
182
  if not os.path.exists(pdf_path):
183
  st.error("File not found. Please check the file path.")
@@ -186,35 +187,37 @@ def main():
186
  if "chat_history" not in st.session_state:
187
  st.session_state['chat_history'] = []
188
 
189
-
190
-
191
-
192
  if "button_clicked" not in st.session_state:
193
  st.session_state['button_clicked'] = None
194
 
195
  display_chat_history(st.session_state['chat_history'])
196
- display_chat_history(st.session_state['chat_history'])
197
 
198
- query = st.text_input("Ask questions about your PDF file (in any preferred language):")
199
-
200
- # Define your cloud buttons with associated queries
201
- cloud_buttons = [
202
- ("Was genau ist ein Belegarzt?", "Was genau ist ein Belegarzt?"),
203
- ("Wofür wird die Alpha-ID verwendet?", "Wofür wird die Alpha-ID verwendet?"),
204
- # Add more buttons as needed
205
- ]
206
-
207
- # Create cloud buttons
208
- button_clicked = False
209
- for label, button_query in cloud_buttons:
210
- if st.button(label):
211
- st.session_state['chat_history'].append(("User", button_query, "new"))
212
- query = button_query
213
- button_clicked = True
214
-
215
- if st.button("Ask") or button_clicked:
216
- if query:
 
 
 
217
  st.session_state['chat_history'].append(("User", query, "new"))
 
 
 
218
  loading_message = st.empty()
219
  loading_message.text('Bot is thinking...')
220
 
@@ -225,10 +228,17 @@ def main():
225
  response = chain.run(input_documents=docs, question=query)
226
 
227
  st.session_state['chat_history'].append(("Bot", response, "new"))
 
 
 
 
 
 
 
 
228
  loading_message.empty()
229
- query = ""
230
 
231
- st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
232
 
233
  if __name__ == "__main__":
234
- main()
 
12
  from langchain.callbacks import get_openai_callback
13
  import os
14
 
15
+ # Step 1: Clone the Dataset Repository
16
+ repo = Repository(
17
+ local_dir="Private_Book", # Local directory to clone the repository
18
+ repo_type="dataset", # Specify that this is a dataset repository
19
+
20
+ clone_from="Anne31415/Private_Book", # Replace with your repository URL
21
+
22
+ token=os.environ["HUB_TOKEN"] # Use the secret token to authenticate
23
+ )
24
+ repo.git_pull() # Pull the latest changes (if any)
25
+
26
+ # Step 2: Load the PDF File
27
+ pdf_file_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
28
+
29
+
30
  # Add this function to handle button clicks
31
  def on_button_click(query):
32
  st.session_state['button_clicked'] = query
33
 
34
 
35
 
 
 
 
 
 
 
36
 
37
 
38
  def cloud_button(label, query, key=None, color=None, overlap=30):
 
105
  """
106
  st.markdown(cloud_button_html, unsafe_allow_html=True)
107
 
 
 
 
 
 
 
 
 
 
 
108
 
109
+ def display_chat_history(chat_history):
110
+ for sender, msg, _ in chat_history:
111
+ background_color = "#FFA07A" if sender == "User" else "#caf"
112
+ st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{sender}: {msg}</div>", unsafe_allow_html=True)
113
+
114
+
115
 
116
  with st.sidebar:
117
  st.title('BinDoc GmbH')
 
170
 
171
  def main():
172
  hide_streamlit_style = """
173
+ <style>
174
+ #MainMenu {visibility: hidden;}
175
+ footer {visibility: hidden;}
176
+ </style>
177
+ """
178
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
179
 
 
180
  st.title("Welcome to BinDocs ChatBot! 🤖")
181
 
 
182
  pdf_path = pdf_file_path
183
  if not os.path.exists(pdf_path):
184
  st.error("File not found. Please check the file path.")
 
187
  if "chat_history" not in st.session_state:
188
  st.session_state['chat_history'] = []
189
 
 
 
 
190
  if "button_clicked" not in st.session_state:
191
  st.session_state['button_clicked'] = None
192
 
193
  display_chat_history(st.session_state['chat_history'])
 
194
 
195
+ st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
196
+ st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
197
+ st.write("<!-- End Spacer -->", unsafe_allow_html=True)
198
+
199
+ if pdf_path is not None:
200
+ query = st.text_input("Ask questions about your PDF file (in any preferred language):")
201
+
202
+ cloud_buttons = [
203
+ ("Was genau ist ein Belegarzt?", "Was genau ist ein Belegarzt?", "1"),
204
+ ("Wofür wird die Alpha-ID verwendet?", "Wofür wird die Alpha-ID verwendet?", "2"),
205
+ # Add more buttons as needed
206
+ ]
207
+
208
+ for label, query, color in cloud_buttons:
209
+ cloud_button(label, query, color=color)
210
+
211
+ if st.session_state['button_clicked']:
212
+ query = st.session_state['button_clicked']['query']
213
+ st.session_state['chat_history'].append(("User", query, "new"))
214
+ st.session_state['button_clicked'] = None
215
+
216
+ if st.button("Ask") or (query and query != st.session_state.get('last_query')):
217
  st.session_state['chat_history'].append(("User", query, "new"))
218
+ st.session_state['last_query'] = query
219
+
220
+ if st.session_state['chat_history']:
221
  loading_message = st.empty()
222
  loading_message.text('Bot is thinking...')
223
 
 
228
  response = chain.run(input_documents=docs, question=query)
229
 
230
  st.session_state['chat_history'].append(("Bot", response, "new"))
231
+
232
+ new_messages = st.session_state['chat_history'][-2:]
233
+ for chat in new_messages:
234
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
235
+ 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)
236
+
237
+ st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
238
+
239
  loading_message.empty()
 
240
 
241
+ st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
242
 
243
  if __name__ == "__main__":
244
+ main()