Anne31415 commited on
Commit
45a9294
1 Parent(s): 4c887e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -58
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from PIL import Image
3
  import time
 
4
  from dotenv import load_dotenv
5
  import pickle
6
  from huggingface_hub import Repository
@@ -13,7 +14,6 @@ from langchain.llms import OpenAI
13
  from langchain.chains.question_answering import load_qa_chain
14
  from langchain.callbacks import get_openai_callback
15
  import os
16
- import traceback
17
 
18
  import pandas as pd
19
  import pydeck as pdk
@@ -26,6 +26,8 @@ if 'chat_history_page1' not in st.session_state:
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(
31
  local_dir="Private_Book", # Local directory to clone the repository
@@ -36,15 +38,17 @@ repo = Repository(
36
  repo.git_pull() # Pull the latest changes (if any)
37
 
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
46
 
47
 
 
48
  # Updated caching mechanism using st.cache_data
49
  @st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
50
  def load_vector_store(file_path, store_name, force_reload=False):
@@ -70,12 +74,6 @@ def load_vector_store(file_path, store_name, force_reload=False):
70
 
71
  return VectorStore
72
 
73
- except Exception as e:
74
- st.error(f"An error occurred: {e}")
75
- traceback.print_exc()
76
- return None
77
-
78
-
79
  # Utility function to load text from a PDF
80
  def load_pdf_text(file_path):
81
  pdf_reader = PdfReader(file_path)
@@ -95,6 +93,7 @@ def display_chat_history(chat_history):
95
 
96
 
97
 
 
98
  def page1():
99
  try:
100
  hide_streamlit_style = """
@@ -105,73 +104,102 @@ def page1():
105
  """
106
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
107
 
108
- col1, col2 = st.columns([3, 1])
 
109
 
110
  with col1:
111
- st.title("Welcome to BinDocs ChatBot!")
112
 
113
  with col2:
 
114
  image = Image.open('BinDoc Logo (Quadratisch).png')
115
  st.image(image, use_column_width='always')
116
 
117
- if not os.path.exists(pdf_path):
118
- st.error("File not found. Please check the file path.")
119
- return
 
 
 
 
 
120
 
121
- VectorStore = load_vector_store(pdf_path, "vector_store_page1", force_reload=False)
122
- display_chat_history(st.session_state['chat_history_page1'])
 
 
 
 
 
123
 
124
- st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
125
- st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
126
- st.write("<!-- End Spacer -->", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
127
 
128
- query = st.text_input("Ask questions about your PDF file (in any preferred language):")
129
- add_vertical_space(2)
 
 
 
 
 
 
130
 
131
- col1, col2 = st.columns(2)
132
 
133
- with col1:
134
- if st.button("Was kann ich mit dem Prognose-Analyse-Tool machen?"):
135
- query = "Was kann ich mit dem Prognose-Analyse-Tool machen?"
136
- if st.button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"):
137
- query = "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"
138
- if st.button("Ich habe mein Meta-Password vergessen, wie kann ich es zurücksetzen?"):
139
- query = "Ich habe mein Meta-Password vergessen, wie kann ich es zurücksetzen?"
140
 
141
- with col2:
142
- if st.button("Dies ist eine reine Test Frage, welche aber eine ausreichende Länge hat."):
143
- query = "Dies ist eine reine Test Frage, welche aber eine ausreichende Länge hat."
144
- if st.button("Was sagt mir denn generell die wundervolle Bevölkerungsentwicklung?"):
145
- query = "Was sagt mir denn generell die wundervolle Bevölkerungsentwicklung?"
146
- if st.button("Ob ich hier wohl viel schreibe, dass die Fragen vom Layout her passen?"):
147
- query = "Ob ich hier wohl viel schreibe, dass die Fragen vom Layout her passen?"
148
-
149
- if query:
150
- st.session_state['chat_history_page1'].append(("User", query, "new"))
151
- start_time = time.time()
152
- with st.spinner('Bot is thinking...'):
153
- chain = load_chatbot()
154
- docs = VectorStore.similarity_search(query=query, k=3)
155
- with get_openai_callback() as cb:
156
- response = chain.run(input_documents=docs, question=query)
157
-
158
- end_time = time.time()
159
- duration = end_time - start_time
160
- st.text(f"Response time: {duration:.2f} seconds")
161
-
162
- st.session_state['chat_history_page1'].append(("Bot", response, "new"))
163
-
164
- new_messages = st.session_state['chat_history_page1'][-2:]
165
- for chat in new_messages:
166
- background_color = "#ffeecf"
167
- 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)
168
-
169
- query = ""
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  st.session_state['chat_history_page1'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page1']]
172
 
173
  except Exception as e:
174
  st.error(f"Upsi, an unexpected error occurred: {e}")
 
 
175
 
176
 
177
 
@@ -284,7 +312,9 @@ def page2():
284
  st.error(f"Upsi, an unexpected error occurred: {e}")
285
  # Optionally log the exception details to a file or error tracking service
286
 
287
-
 
 
288
 
289
  def main():
290
  # Sidebar content
 
1
  import streamlit as st
2
  from PIL import Image
3
  import time
4
+ import streamlit_analytics
5
  from dotenv import load_dotenv
6
  import pickle
7
  from huggingface_hub import Repository
 
14
  from langchain.chains.question_answering import load_qa_chain
15
  from langchain.callbacks import get_openai_callback
16
  import os
 
17
 
18
  import pandas as pd
19
  import pydeck as pdk
 
26
  if 'chat_history_page2' not in st.session_state:
27
  st.session_state['chat_history_page2'] = []
28
 
29
+
30
+
31
  # Step 1: Clone the Dataset Repository
32
  repo = Repository(
33
  local_dir="Private_Book", # Local directory to clone the repository
 
38
  repo.git_pull() # Pull the latest changes (if any)
39
 
40
  # Step 2: Load the PDF File
41
+ pdf_path = "Private_Book/141123_Kombi.pdf" # Replace with your PDF file path
42
 
43
  # Step 2: Load the PDF File
44
  pdf_path2 = "Private_Book/Deutsche_Kodierrichtlinien_23.pdf" # Replace with your PDF file path
45
 
46
+
47
  api_key = os.getenv("OPENAI_API_KEY")
48
  # Retrieve the API key from st.secrets
49
 
50
 
51
+
52
  # Updated caching mechanism using st.cache_data
53
  @st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
54
  def load_vector_store(file_path, store_name, force_reload=False):
 
74
 
75
  return VectorStore
76
 
 
 
 
 
 
 
77
  # Utility function to load text from a PDF
78
  def load_pdf_text(file_path):
79
  pdf_reader = PdfReader(file_path)
 
93
 
94
 
95
 
96
+
97
  def page1():
98
  try:
99
  hide_streamlit_style = """
 
104
  """
105
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
106
 
107
+ # Create columns for layout
108
+ col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
109
 
110
  with col1:
111
+ st.title("Welcome to BinDocs AI!")
112
 
113
  with col2:
114
+ # Load and display the image in the right column, which will be the top-right corner of the page
115
  image = Image.open('BinDoc Logo (Quadratisch).png')
116
  st.image(image, use_column_width='always')
117
 
118
+
119
+ # Start tracking user interactions
120
+ with streamlit_analytics.track():
121
+ if not os.path.exists(pdf_path):
122
+ st.error("File not found. Please check the file path.")
123
+ return
124
+
125
+ VectorStore = load_vector_store(pdf_path, "vector_store_page1", force_reload=False)
126
 
127
+ display_chat_history(st.session_state['chat_history_page1'])
128
+
129
+ st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
130
+ st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
131
+ st.write("<!-- End Spacer -->", unsafe_allow_html=True)
132
+
133
+ new_messages_placeholder = st.empty()
134
 
135
+ query = st.text_input("Geben Sie hier Ihre Frage ein / Enter your question here:")
136
+
137
+ add_vertical_space(2) # Adjust as per the desired spacing
138
+
139
+ # Create two columns for the buttons
140
+ col1, col2 = st.columns(2)
141
+
142
+ with col1:
143
+ if st.button("Was kann ich mit dem Prognose-Analyse-Tool machen?"):
144
+ query = "Was kann ich mit dem Prognose-Analyse-Tool machen?"
145
+ if st.button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"):
146
+ query = "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"
147
+ if st.button("Ich habe mein Meta-Password vergessen, wie kann ich es zurücksetzen?"):
148
+ query = "Ich habe mein Meta-Password vergessen, wie kann ich es zurücksetzen?"
149
 
150
+
151
+ with col2:
152
+ if st.button("Dies ist eine reine Test Frage, welche aber eine ausreichende Länge hat."):
153
+ query = "Dies ist eine reine Test Frage, welche aber eine ausreichende Länge hat."
154
+ if st.button("Was sagt mir denn generell die wundervolle Bevölkerungsentwicklung?"):
155
+ query = "Was sagt mir denn generell die wundervolle Bevölkerungsentwicklung?"
156
+ if st.button("Ob ich hier wohl viel schreibe, dass die Fragen vom Layout her passen?"):
157
+ query = "Ob ich hier wohl viel schreibe, dass die Fragen vom Layout her passen?"
158
 
 
159
 
160
+ if query:
161
+ st.session_state['chat_history_page1'].append(("User", query, "new"))
 
 
 
 
 
162
 
163
+ # Start timing
164
+ start_time = time.time()
165
+
166
+ with st.spinner('Bot is thinking...'):
167
+ # Use the VectorStore loaded at the start from the session state
168
+ chain = load_chatbot()
169
+ docs = VectorStore.similarity_search(query=query, k=3)
170
+ with get_openai_callback() as cb:
171
+ response = chain.run(input_documents=docs, question=query)
172
+
173
+
174
+ # Stop timing
175
+ end_time = time.time()
176
+
177
+ # Calculate duration
178
+ duration = end_time - start_time
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
+ # You can use Streamlit's text function to display the timing
181
+ st.text(f"Response time: {duration:.2f} seconds")
182
+
183
+ st.session_state['chat_history_page1'].append(("Bot", response, "new"))
184
+
185
+
186
+ # Display new messages at the bottom
187
+ new_messages = st.session_state['chat_history_page1'][-2:]
188
+ for chat in new_messages:
189
+ background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
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
+
193
+ # Clear the input field after the query is made
194
+ query = ""
195
+
196
+ # Mark all messages as old after displaying
197
  st.session_state['chat_history_page1'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page1']]
198
 
199
  except Exception as e:
200
  st.error(f"Upsi, an unexpected error occurred: {e}")
201
+ # Optionally log the exception details to a file or error tracking service
202
+
203
 
204
 
205
 
 
312
  st.error(f"Upsi, an unexpected error occurred: {e}")
313
  # Optionally log the exception details to a file or error tracking service
314
 
315
+
316
+
317
+
318
 
319
  def main():
320
  # Sidebar content