Anne31415 commited on
Commit
1c86f62
1 Parent(s): 11df7eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -2
app.py CHANGED
@@ -23,7 +23,8 @@ from urllib.error import URLError
23
  if 'chat_history_page1' not in st.session_state:
24
  st.session_state['chat_history_page1'] = []
25
 
26
-
 
27
 
28
  # Step 1: Clone the Dataset Repository
29
  repo = Repository(
@@ -37,6 +38,8 @@ repo.git_pull() # Pull the latest changes (if any)
37
  # Step 2: Load the PDF File
38
  pdf_path = "Private_Book/grunddaten-krankenhaeuser-2016.pdf" # Replace with your PDF file path
39
 
 
 
40
 
41
  api_key = os.getenv("OPENAI_API_KEY")
42
  # Retrieve the API key from st.secrets
@@ -185,7 +188,114 @@ def page1():
185
 
186
 
187
  def page2():
188
- st.title('BinDoc GmbH')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
190
 
191
  def main():
 
23
  if 'chat_history_page1' not in st.session_state:
24
  st.session_state['chat_history_page1'] = []
25
 
26
+ if 'chat_history_page2' not in st.session_state:
27
+ st.session_state['chat_history_page2'] = []
28
 
29
  # Step 1: Clone the Dataset Repository
30
  repo = Repository(
 
38
  # Step 2: Load the PDF File
39
  pdf_path = "Private_Book/grunddaten-krankenhaeuser-2016.pdf" # Replace with your PDF file path
40
 
41
+ # Step 2: Load the PDF File
42
+ pdf_path2 = "Private_Book/Deutsche_Kodierrichtlinien_23.pdf" # Replace with your PDF file path
43
 
44
  api_key = os.getenv("OPENAI_API_KEY")
45
  # Retrieve the API key from st.secrets
 
188
 
189
 
190
  def page2():
191
+ try:
192
+ hide_streamlit_style = """
193
+ <style>
194
+ #MainMenu {visibility: hidden;}
195
+ footer {visibility: hidden;}
196
+ </style>
197
+ """
198
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
199
+
200
+ # Create columns for layout
201
+ col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
202
+
203
+ with col1:
204
+ st.title("Kodieren statt Frustrieren!")
205
+
206
+ with col2:
207
+ # Load and display the image in the right column, which will be the top-right corner of the page
208
+ image = Image.open('BinDoc Logo (Quadratisch).png')
209
+ st.image(image, use_column_width='always')
210
+
211
+
212
+ # Start tracking user interactions
213
+ with streamlit_analytics.track():
214
+
215
+ if not os.path.exists(pdf_path2):
216
+ st.error("File not found. Please check the file path.")
217
+ return
218
+
219
+ VectorStore = load_vector_store(pdf_path2, "vector_store_page2", force_reload=False)
220
+
221
+
222
+
223
+ display_chat_history(st.session_state['chat_history_page2'])
224
+
225
+ st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
226
+ st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
227
+ st.write("<!-- End Spacer -->", unsafe_allow_html=True)
228
+
229
+ new_messages_placeholder = st.empty()
230
+
231
+ query = st.text_input("Ask questions about your PDF file (in any preferred language):")
232
+
233
+ add_vertical_space(2) # Adjust as per the desired spacing
234
+
235
+ # Create two columns for the buttons
236
+ col1, col2 = st.columns(2)
237
+
238
+ with col1:
239
+ if st.button("Wann kodiere ich etwas als Hauptdiagnose und wann als Nebendiagnose?"):
240
+ query = "Wann kodiere ich etwas als Hauptdiagnose und wann als Nebendiagnose?"
241
+ if st.button("Ein Patient wird mit Aszites bei bekannter Leberzirrhose stationär aufgenommen. Es wird nur der Aszites durch eine Punktion behandelt.Wie kodiere ich das?"):
242
+ query = ("Ein Patient wird mit Aszites bei bekannter Leberzirrhose stationär aufgenommen. Es wird nur der Aszites durch eine Punktion behandelt.Wie kodiere ich das?")
243
+ if st.button("Hauptdiagnose: Hirntumor wie kodiere ich das?"):
244
+ query = "Hauptdiagnose: Hirntumor wie kodiere ich das?"
245
+
246
+
247
+ with col2:
248
+ if st.button("Welche Prozeduren werden normalerweise nicht verschlüsselt?"):
249
+ query = "Welche Prozeduren werden normalerweise nicht verschlüsselt?"
250
+ if st.button("Was muss ich bei der Kodierung der Folgezusänden von Krankheiten beachten?"):
251
+ query = "Was muss ich bei der Kodierung der Folgezusänden von Krankheiten beachten?"
252
+ if st.button("Was mache ich bei einer Verdachtsdiagnose, wenn mein Patien nach Hause entlassen wird?"):
253
+ query = "Was mache ich bei einer Verdachtsdiagnose, wenn mein Patien nach Hause entlassen wird?"
254
+
255
+
256
+ if query:
257
+ st.session_state['chat_history_page2'].append(("User", query, "new"))
258
+
259
+ # Start timing
260
+ start_time = time.time()
261
+
262
+ with st.spinner('Bot is thinking...'):
263
+ # Use the VectorStore loaded at the start from the session state
264
+ chain = load_chatbot()
265
+ docs = VectorStore.similarity_search(query=query, k=3)
266
+ with get_openai_callback() as cb:
267
+ response = chain.run(input_documents=docs, question=query)
268
+
269
+
270
+ # Stop timing
271
+ end_time = time.time()
272
+
273
+ # Calculate duration
274
+ duration = end_time - start_time
275
+
276
+ # You can use Streamlit's text function to display the timing
277
+ st.text(f"Response time: {duration:.2f} seconds")
278
+
279
+ st.session_state['chat_history_page2'].append(("Bot", response, "new"))
280
+
281
+
282
+ # Display new messages at the bottom
283
+ new_messages = st.session_state['chat_history_page2'][-2:]
284
+ for chat in new_messages:
285
+ background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
286
+ 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)
287
+
288
+
289
+ # Clear the input field after the query is made
290
+ query = ""
291
+
292
+ # Mark all messages as old after displaying
293
+ st.session_state['chat_history_page2'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page2']]
294
+
295
+ except Exception as e:
296
+ st.error(f"Upsi, an unexpected error occurred: {e}")
297
+ # Optionally log the exception details to a file or error tracking service
298
+
299
 
300
 
301
  def main():