Anne31415 commited on
Commit
4a1415a
·
1 Parent(s): c678561

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -39
app.py CHANGED
@@ -49,18 +49,6 @@ with st.sidebar:
49
 
50
  add_vertical_space(1) # Adjust as per the desired spacing
51
 
52
- glossary_option = st.selectbox(
53
- 'Want to see more Glossary Topics to ask about?',
54
- ('Basisfallwert', 'Cash Flow', 'Arzneimittelgesetz (AMG)')
55
- )
56
-
57
- add_vertical_space(1)
58
-
59
- whitepaper_option = st.selectbox(
60
- 'Did you know we\'ve authored some really insightful and helpful whitepapers as well?',
61
- ('Die Value Story als strategisches Instrument', 'Patientenmagnet Robotik: Best Practice Beispiel ', 'Das AGAPLesion-Konzept ')
62
- )
63
-
64
  st.write('Made with ❤️ by BinDoc GmbH')
65
 
66
  api_key = os.getenv("OPENAI_API_KEY")
@@ -94,13 +82,12 @@ def load_pdf(file_path):
94
  return VectorStore
95
 
96
 
 
97
  def load_chatbot():
98
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
99
 
100
-
101
-
102
  def main():
103
- # Hide Streamlit style
104
  hide_streamlit_style = """
105
  <style>
106
  #MainMenu {visibility: hidden;}
@@ -109,9 +96,10 @@ def main():
109
  """
110
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
111
 
 
112
  # Main content
113
  st.title("Welcome to BinDocs ChatBot! 🤖")
114
-
115
  # Directly specifying the path to the PDF file
116
  pdf_path = pdf_file_path
117
  if not os.path.exists(pdf_path):
@@ -128,7 +116,6 @@ def main():
128
  st.write("<!-- End Spacer -->", unsafe_allow_html=True)
129
 
130
  new_messages_placeholder = st.empty()
131
- loading_message = None # Initialize the loading_message variable
132
 
133
  if pdf_path is not None:
134
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
@@ -144,27 +131,21 @@ def main():
144
  if st.button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"):
145
  query = "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"
146
  if st.button("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?"):
147
- query = "Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?"
148
 
 
149
  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]):
150
  st.session_state['chat_history'].append(("User", query, "new"))
151
 
152
- loading_message = st.empty() # Assign the loading_message variable here
153
  loading_message.text('Bot is thinking...')
154
 
155
  VectorStore = load_pdf(pdf_path)
156
  chain = load_chatbot()
157
  docs = VectorStore.similarity_search(query=query, k=3)
158
-
159
- # Add user's query to conversation memory
160
- conversation_memory.append(("User", query))
161
-
162
  with get_openai_callback() as cb:
163
  response = chain.run(input_documents=docs, question=query)
164
 
165
- # Add bot's response to conversation memory
166
- conversation_memory.append(("Bot", response))
167
-
168
  st.session_state['chat_history'].append(("Bot", response, "new"))
169
 
170
  # Display new messages at the bottom
@@ -176,24 +157,14 @@ def main():
176
  # Scroll to the latest response using JavaScript
177
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
178
 
 
 
179
  # Clear the input field by setting the query variable to an empty string
180
  query = ""
181
 
182
  # Mark all messages as old after displaying
183
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
184
-
185
-
186
- # Display new messages at the bottom
187
- new_messages = st.session_state['chat_history'][-2:]
188
- for chat in new_messages:
189
- background_color = "#FFA07A" if chat[2] == "new" else "#D1D1E0" if chat[0] == "User" else "#D1D1E0" # Use a very light purple-grey color
190
- 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)
191
-
192
- # Scroll to the latest response using JavaScript
193
- st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
194
 
195
- if loading_message is not None:
196
- loading_message.empty() # Call empty() only if loading_message is not None
197
 
198
 
199
  def display_chat_history(chat_history):
@@ -202,4 +173,4 @@ def display_chat_history(chat_history):
202
  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)
203
 
204
  if __name__ == "__main__":
205
- main()
 
49
 
50
  add_vertical_space(1) # Adjust as per the desired spacing
51
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  st.write('Made with ❤️ by BinDoc GmbH')
53
 
54
  api_key = os.getenv("OPENAI_API_KEY")
 
82
  return VectorStore
83
 
84
 
85
+
86
  def load_chatbot():
87
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
88
 
 
 
89
  def main():
90
+
91
  hide_streamlit_style = """
92
  <style>
93
  #MainMenu {visibility: hidden;}
 
96
  """
97
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
98
 
99
+
100
  # Main content
101
  st.title("Welcome to BinDocs ChatBot! 🤖")
102
+
103
  # Directly specifying the path to the PDF file
104
  pdf_path = pdf_file_path
105
  if not os.path.exists(pdf_path):
 
116
  st.write("<!-- End Spacer -->", unsafe_allow_html=True)
117
 
118
  new_messages_placeholder = st.empty()
 
119
 
120
  if pdf_path is not None:
121
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
 
131
  if st.button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"):
132
  query = "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"
133
  if st.button("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?"):
134
+ query = ("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?")
135
 
136
+
137
  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]):
138
  st.session_state['chat_history'].append(("User", query, "new"))
139
 
140
+ loading_message = st.empty()
141
  loading_message.text('Bot is thinking...')
142
 
143
  VectorStore = load_pdf(pdf_path)
144
  chain = load_chatbot()
145
  docs = VectorStore.similarity_search(query=query, k=3)
 
 
 
 
146
  with get_openai_callback() as cb:
147
  response = chain.run(input_documents=docs, question=query)
148
 
 
 
 
149
  st.session_state['chat_history'].append(("Bot", response, "new"))
150
 
151
  # Display new messages at the bottom
 
157
  # Scroll to the latest response using JavaScript
158
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
159
 
160
+ loading_message.empty()
161
+
162
  # Clear the input field by setting the query variable to an empty string
163
  query = ""
164
 
165
  # Mark all messages as old after displaying
166
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
 
 
 
 
 
 
 
 
 
 
167
 
 
 
168
 
169
 
170
  def display_chat_history(chat_history):
 
173
  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)
174
 
175
  if __name__ == "__main__":
176
+ main()