Anne31415 commited on
Commit
b06fd67
·
1 Parent(s): 5a28160

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -85
app.py CHANGED
@@ -87,95 +87,95 @@ def load_chatbot():
87
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
88
 
89
  def main():
90
- hide_streamlit_style = """
91
- <style>
92
- #MainMenu {visibility: hidden;}
93
- footer {visibility: hidden;}
94
- </style>
95
- """
96
- st.markdown(hide_streamlit_style, unsafe_allow_html=True)
97
-
98
- # Main content
99
- st.title("Welcome to BinDocs ChatBot! 🤖")
100
-
101
- # Start tracking user interactions
102
- with streamlit_analytics.track():
103
- if not os.path.exists(pdf_path):
104
- st.error("File not found. Please check the file path.")
105
- return
106
-
107
- VectorStore = load_vector_store(pdf_path, "my_vector_store", force_reload=False)
108
-
109
-
110
- if "chat_history" not in st.session_state:
111
- st.session_state['chat_history'] = []
112
-
113
- display_chat_history(st.session_state['chat_history'])
114
-
115
- st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
116
- st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
117
- st.write("<!-- End Spacer -->", unsafe_allow_html=True)
118
-
119
- new_messages_placeholder = st.empty()
120
-
121
- query = st.text_input("Ask questions about your PDF file (in any preferred language):")
122
-
123
- if st.button("Was genau ist ein Belegarzt?"):
124
- query = "Was genau ist ein Belegarzt?"
125
- if st.button("Wofür wird die Alpha-ID verwendet?"):
126
- query = "Wofür wird die Alpha-ID verwendet?"
127
- if st.button("Was sind die Vorteile des ambulanten Operierens?"):
128
- query = "Was sind die Vorteile des ambulanten Operierens?"
129
- if st.button("Was kann ich mit dem Prognose-Analyse-Tool machen?"):
130
- query = "Was kann ich mit dem Prognose-Analyse-Tool machen?"
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
- if query:
137
- st.session_state['chat_history'].append(("User", query, "new"))
138
-
139
- loading_message = st.empty()
140
- loading_message.text('Bot is thinking...')
141
 
142
-
143
- # Start timing
144
- start_time = time.time()
145
-
146
- # Use the VectorStore loaded at the start from the session state
147
- chain = load_chatbot()
148
- docs = VectorStore.similarity_search(query=query, k=3)
149
- with get_openai_callback() as cb:
150
- response = chain.run(input_documents=docs, question=query)
151
-
152
- # Stop timing
153
- end_time = time.time()
154
-
155
- # Calculate duration
156
- duration = end_time - start_time
157
-
158
- # You can use Streamlit's text function to display the timing
159
- st.text(f"Response time: {duration:.2f} seconds")
160
-
161
- st.session_state['chat_history'].append(("Bot", response, "new"))
162
-
163
- # Display new messages at the bottom
164
- new_messages = st.session_state['chat_history'][-2:]
165
- for chat in new_messages:
166
- background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
167
- 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)
168
-
169
- # Scroll to the latest response using JavaScript
170
- st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
171
 
172
- loading_message.empty()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
- # Clear the input field after the query is made
175
- query = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
- # Mark all messages as old after displaying
178
- st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
 
179
 
180
 
181
  def display_chat_history(chat_history):
@@ -183,5 +183,6 @@ def display_chat_history(chat_history):
183
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
184
  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)
185
 
 
186
  if __name__ == "__main__":
187
  main()
 
87
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
88
 
89
  def main():
90
+ try:
91
+ hide_streamlit_style = """
92
+ <style>
93
+ #MainMenu {visibility: hidden;}
94
+ footer {visibility: hidden;}
95
+ </style>
96
+ """
97
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
98
+
99
+ # Main content
100
+ st.title("Welcome to BinDocs ChatBot! 🤖")
101
+
102
+ # Start tracking user interactions
103
+ with streamlit_analytics.track():
104
+ if not os.path.exists(pdf_path):
105
+ st.error("File not found. Please check the file path.")
106
+ return
107
+
108
+ VectorStore = load_vector_store(pdf_path, "my_vector_store", force_reload=False)
109
+
110
+
111
+ if "chat_history" not in st.session_state:
112
+ st.session_state['chat_history'] = []
113
+
114
+ display_chat_history(st.session_state['chat_history'])
115
+
116
+ st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
117
+ st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
118
+ st.write("<!-- End Spacer -->", unsafe_allow_html=True)
119
+
120
+ new_messages_placeholder = st.empty()
121
+
122
+ query = st.text_input("Ask questions about your PDF file (in any preferred language):")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
+ if st.button("Was genau ist ein Belegarzt?"):
125
+ query = "Was genau ist ein Belegarzt?"
126
+ if st.button("Wofür wird die Alpha-ID verwendet?"):
127
+ query = "Wofür wird die Alpha-ID verwendet?"
128
+ if st.button("Was sind die Vorteile des ambulanten Operierens?"):
129
+ query = "Was sind die Vorteile des ambulanten Operierens?"
130
+ if st.button("Was kann ich mit dem Prognose-Analyse-Tool machen?"):
131
+ query = "Was kann ich mit dem Prognose-Analyse-Tool machen?"
132
+ if st.button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"):
133
+ query = "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"
134
+ if st.button("Ich habe mein Meta-Password vergessen, wie kann ich es zurücksetzen?"):
135
+ query = "Ich habe mein Meta-Password vergessen, wie kann ich es zurücksetzen?"
136
+
137
+ if query:
138
+ st.session_state['chat_history'].append(("User", query, "new"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
+ # Start timing
141
+ start_time = time.time()
142
+
143
+ with st.spinner('Bot is thinking...'):
144
+ # Use the VectorStore loaded at the start from the session state
145
+ chain = load_chatbot()
146
+ docs = VectorStore.similarity_search(query=query, k=3)
147
+ with get_openai_callback() as cb:
148
+ response = chain.run(input_documents=docs, question=query)
149
+
150
+
151
+ # Stop timing
152
+ end_time = time.time()
153
+
154
+ # Calculate duration
155
+ duration = end_time - start_time
156
 
157
+ # You can use Streamlit's text function to display the timing
158
+ st.text(f"Response time: {duration:.2f} seconds")
159
+
160
+ st.session_state['chat_history'].append(("Bot", response, "new"))
161
+
162
+
163
+ # Display new messages at the bottom
164
+ new_messages = st.session_state['chat_history'][-2:]
165
+ for chat in new_messages:
166
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
167
+ 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)
168
+
169
+
170
+ # Clear the input field after the query is made
171
+ query = ""
172
+
173
+ # Mark all messages as old after displaying
174
+ st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
175
 
176
+ except Exception as e:
177
+ st.error(f"Upsi, an unexpected error occurred: {e}")
178
+ # Optionally log the exception details to a file or error tracking service
179
 
180
 
181
  def display_chat_history(chat_history):
 
183
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
184
  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)
185
 
186
+
187
  if __name__ == "__main__":
188
  main()