Anne31415 commited on
Commit
d47dd8a
·
1 Parent(s): 976f538

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -28
app.py CHANGED
@@ -12,8 +12,13 @@ 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}"
18
  color_class = f"color-{color}" if color else ""
19
  num_circles = max(3, min(35, len(label) // 4))
@@ -63,25 +68,26 @@ def cloud_button(label, key=None, color=None, overlap=30):
63
  white-space: nowrap; /* Prevent line breaks */
64
  text-align: center; /* Center the text horizontally and vertically */
65
  }}
66
- /* Add this CSS for the hover effect and shadow */
67
  .cloud:hover .circle {{
68
- transform: scale(1.1); /* Scale up the circles on hover */
69
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Add a shadow on hover */
70
  }}
71
-
72
  .color-1 .circle {{ background-color: #FFA07A; }}
73
  .color-2 .circle {{ background-color: #FF7F50; }}
74
  .color-3 .circle {{ background-color: #FF6347; }}
75
  </style>
76
  <script>
77
  document.getElementById("{button_id}").onclick = function() {{
78
- // Your existing JavaScript code for handling button click
 
 
 
 
79
  }};
80
  </script>
81
  """
82
  st.markdown(cloud_button_html, unsafe_allow_html=True)
83
 
84
-
85
  # Step 1: Clone the Dataset Repository
86
  repo = Repository(
87
  local_dir="Private_Book", # Local directory to clone the repository
@@ -152,7 +158,6 @@ def load_chatbot():
152
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
153
 
154
  def main():
155
-
156
  hide_streamlit_style = """
157
  <style>
158
  #MainMenu {visibility: hidden;}
@@ -161,10 +166,9 @@ def main():
161
  """
162
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
163
 
164
-
165
  # Main content
166
  st.title("Welcome to BinDocs ChatBot! 🤖")
167
-
168
  # Directly specifying the path to the PDF file
169
  pdf_path = pdf_file_path
170
  if not os.path.exists(pdf_path):
@@ -177,7 +181,6 @@ def main():
177
  if "button_clicked" not in st.session_state:
178
  st.session_state['button_clicked'] = None
179
 
180
-
181
  display_chat_history(st.session_state['chat_history'])
182
 
183
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
@@ -189,18 +192,17 @@ def main():
189
  if pdf_path is not None:
190
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
191
 
192
- cloud_button("Was genau ist ein Belegarzt?", key="button1", color="1")
193
- cloud_button("Wofür wird die Alpha-ID verwendet?", key="button2", color="2")
194
- cloud_button("Was sind die Vorteile des ambulanten operierens?", key="button3", color="3")
195
- cloud_button("Was kann ich mit dem Prognose-Analyse Toll machen?", key="button4", color="4")
196
- cloud_button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?", key="button5", color="5")
197
- cloud_button("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?", key="button6", color="6")
198
 
199
- # Handle button clicks
200
  if st.session_state['button_clicked']:
201
- query = st.session_state['button_clicked']
 
202
  st.session_state['button_clicked'] = None
203
-
204
 
205
  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]):
206
  st.session_state['chat_history'].append(("User", query, "new"))
@@ -216,29 +218,22 @@ def main():
216
 
217
  st.session_state['chat_history'].append(("Bot", response, "new"))
218
 
219
- # Display new messages at the bottom
220
  new_messages = st.session_state['chat_history'][-2:]
221
  for chat in new_messages:
222
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
223
  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)
224
 
225
- # Scroll to the latest response using JavaScript
226
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
227
 
228
  loading_message.empty()
229
-
230
- # Clear the input field by setting the query variable to an empty string
231
  query = ""
232
 
233
- # Mark all messages as old after displaying
234
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
235
 
236
-
237
-
238
  def display_chat_history(chat_history):
239
  for chat in chat_history:
240
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
241
  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)
242
 
243
  if __name__ == "__main__":
244
- main()
 
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
+ import streamlit as st
20
+
21
+ def cloud_button(label, query, key=None, color=None, overlap=30):
22
  button_id = f"cloud-button-{key or label}"
23
  color_class = f"color-{color}" if color else ""
24
  num_circles = max(3, min(35, len(label) // 4))
 
68
  white-space: nowrap; /* Prevent line breaks */
69
  text-align: center; /* Center the text horizontally and vertically */
70
  }}
 
71
  .cloud:hover .circle {{
72
+ transform: scale(1.1); /* Scale up the circles on hover */
73
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Add a shadow on hover */
74
  }}
 
75
  .color-1 .circle {{ background-color: #FFA07A; }}
76
  .color-2 .circle {{ background-color: #FF7F50; }}
77
  .color-3 .circle {{ background-color: #FF6347; }}
78
  </style>
79
  <script>
80
  document.getElementById("{button_id}").onclick = function() {{
81
+ window.parent.postMessage({{
82
+ 'type': 'streamlit:setComponentValue',
83
+ 'value': {{"label": "{label}", "query": "{query}"}},
84
+ 'key': 'button_clicked'
85
+ }}, '*');
86
  }};
87
  </script>
88
  """
89
  st.markdown(cloud_button_html, unsafe_allow_html=True)
90
 
 
91
  # Step 1: Clone the Dataset Repository
92
  repo = Repository(
93
  local_dir="Private_Book", # Local directory to clone the repository
 
158
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
159
 
160
  def main():
 
161
  hide_streamlit_style = """
162
  <style>
163
  #MainMenu {visibility: hidden;}
 
166
  """
167
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
168
 
 
169
  # Main content
170
  st.title("Welcome to BinDocs ChatBot! 🤖")
171
+
172
  # Directly specifying the path to the PDF file
173
  pdf_path = pdf_file_path
174
  if not os.path.exists(pdf_path):
 
181
  if "button_clicked" not in st.session_state:
182
  st.session_state['button_clicked'] = None
183
 
 
184
  display_chat_history(st.session_state['chat_history'])
185
 
186
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
 
192
  if pdf_path is not None:
193
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
194
 
195
+ cloud_button("Was genau ist ein Belegarzt?", "Was genau ist ein Belegarzt?", key="button1", color="1")
196
+ cloud_button("Wofür wird die Alpha-ID verwendet?", "Wofür wird die Alpha-ID verwendet?, key="button2", color="2")
197
+ cloud_button("Was sind die Vorteile des ambulanten operierens?", "Was sind die Vorteile des ambulanten operierens?", key="button3", color="3")
198
+ cloud_button("Was kann ich mit dem Prognose-Analyse Toll machen?", "Was kann ich mit dem Prognose-Analyse Toll machen?", key="button4", color="4")
199
+ cloud_button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?", "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?", key="button5", color="5")
200
+ cloud_button("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?", "Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?", key="button6", color="6")
201
 
 
202
  if st.session_state['button_clicked']:
203
+ label, query = st.session_state['button_clicked'].values()
204
+ st.session_state['chat_history'].append(("User", label, "new"))
205
  st.session_state['button_clicked'] = None
 
206
 
207
  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]):
208
  st.session_state['chat_history'].append(("User", query, "new"))
 
218
 
219
  st.session_state['chat_history'].append(("Bot", response, "new"))
220
 
 
221
  new_messages = st.session_state['chat_history'][-2:]
222
  for chat in new_messages:
223
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
224
  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)
225
 
 
226
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
227
 
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
  def display_chat_history(chat_history):
234
  for chat in chat_history:
235
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
236
  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)
237
 
238
  if __name__ == "__main__":
239
+ main()