Anne31415 commited on
Commit
eb020fb
·
1 Parent(s): 1c37dea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -26
app.py CHANGED
@@ -101,29 +101,55 @@ def page1():
101
  image = Image.open('BinDoc Logo (Quadratisch).png')
102
  st.image(image, use_column_width='always')
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
- # Initialize prompt in session state if not exists
106
- if 'prompt' not in st.session_state:
107
- st.session_state.prompt = ''
 
108
 
109
  # Example query buttons
110
  col3, col4 = st.columns(2) # Create two columns for buttons
111
 
112
  with col3:
113
  if st.button("Plotte mir die Anzahl der Kliniken von 2010 bis 2020."):
114
- st.session_state.prompt = "Plotte mir die Anzahl der Kliniken von 2010 bis 2020."
115
  if st.button("Erstelle mir ein Bar Chart über die Bettenanzahl zwischen 1994 und 2002."):
116
- st.session_state.prompt = "Erstelle mir ein Bar Chart über die Bettenanzahl zwischen 1994 und 2002."
117
 
118
  with col4:
119
  if st.button("Hat die Bettenanzahl zwischen 2018 und 2021 zu- oder abgenommen?"):
120
- st.session_state.prompt = "Hat die Bettenanzahl zwischen 2018 und 2021 zu- oder abgenommen?"
121
  if st.button("Wie hat sich die Fremdkapitalquote des Malteser Krankenhaus St. Franziskus-Hospital verändert?"):
122
- st.session_state.prompt = "Wie hat sich die Fremdkapitalquote des Malteser Krankenhaus St. Franziskus-Hospital verändert?"
123
 
 
124
 
125
-
126
- prompt = st.text_input("Wie kann ich dir helfen?", value=st.session_state.prompt)
127
 
128
  if "assistant" not in st.session_state:
129
  openai.api_key = api_key
@@ -154,22 +180,15 @@ def page1():
154
  if image:
155
  st.image(image, caption="Generated Image")
156
 
157
- if st.button("Submit"):
158
- # Add message to the thread
159
- st.session_state.messages = client.beta.threads.messages.create(
160
- thread_id=st.session_state.thread.id,
161
- role="user",
162
- content=st.session_state.prompt # Use the prompt from session state
163
- )
164
 
165
- # Do a run to process the messages in the thread
166
- st.session_state.run = client.beta.threads.runs.create(
167
- thread_id=st.session_state.thread.id,
168
- assistant_id=st.session_state.assistant.id,
169
- )
170
- if st.session_state.retry_error < 3:
171
- time.sleep(1) # Wait 1 second before checking run status
172
- st.rerun()
173
 
174
  # Check if 'run' object has 'status' attribute
175
  if hasattr(st.session_state.run, 'status'):
@@ -190,8 +209,7 @@ def page1():
190
  time.sleep(3) # Longer delay before retrying
191
  st.rerun()
192
  else:
193
- st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later")
194
-
195
 
196
  # Handle any status that is not 'completed'
197
  elif st.session_state.run.status != "completed":
@@ -204,5 +222,6 @@ def page1():
204
  time.sleep(3)
205
  st.rerun()
206
 
 
207
  if __name__ == "__main__":
208
  main()
 
101
  image = Image.open('BinDoc Logo (Quadratisch).png')
102
  st.image(image, use_column_width='always')
103
 
104
+ # Initialize chat history in session state if not exists
105
+ if 'chat_history' not in st.session_state:
106
+ st.session_state.chat_history = []
107
+
108
+ # Function to handle question submission
109
+ def submit_question(question):
110
+ # Add question to chat history
111
+ st.session_state.chat_history.append(("User", question))
112
+ process_question(question)
113
+
114
+ # Function to process the question
115
+ def process_question(question):
116
+ # Add message to the thread
117
+ st.session_state.messages = client.beta.threads.messages.create(
118
+ thread_id=st.session_state.thread.id,
119
+ role="user",
120
+ content=question
121
+ )
122
+
123
+ # Do a run to process the messages in the thread
124
+ st.session_state.run = client.beta.threads.runs.create(
125
+ thread_id=st.session_state.thread.id,
126
+ assistant_id=st.session_state.assistant.id,
127
+ )
128
 
129
+ # Wait and rerun logic
130
+ if st.session_state.retry_error < 3:
131
+ time.sleep(1) # Wait 1 second before checking run status
132
+ st.rerun()
133
 
134
  # Example query buttons
135
  col3, col4 = st.columns(2) # Create two columns for buttons
136
 
137
  with col3:
138
  if st.button("Plotte mir die Anzahl der Kliniken von 2010 bis 2020."):
139
+ submit_question("Plotte mir die Anzahl der Kliniken von 2010 bis 2020.")
140
  if st.button("Erstelle mir ein Bar Chart über die Bettenanzahl zwischen 1994 und 2002."):
141
+ submit_question("Erstelle mir ein Bar Chart über die Bettenanzahl zwischen 1994 und 2002.")
142
 
143
  with col4:
144
  if st.button("Hat die Bettenanzahl zwischen 2018 und 2021 zu- oder abgenommen?"):
145
+ submit_question("Hat die Bettenanzahl zwischen 2018 und 2021 zu- oder abgenommen?")
146
  if st.button("Wie hat sich die Fremdkapitalquote des Malteser Krankenhaus St. Franziskus-Hospital verändert?"):
147
+ submit_question("Wie hat sich die Fremdkapitalquote des Malteser Krankenhaus St. Franziskus-Hospital verändert?")
148
 
149
+ prompt = st.text_input("Wie kann ich dir helfen?")
150
 
151
+ if st.button("Submit"):
152
+ submit_question(prompt)
153
 
154
  if "assistant" not in st.session_state:
155
  openai.api_key = api_key
 
180
  if image:
181
  st.image(image, caption="Generated Image")
182
 
183
+ # Add assistant's response to chat history
184
+ st.session_state.chat_history.append(("Assistant", last_assistant_message.content[0].text.value))
 
 
 
 
 
185
 
186
+ # Display chat history
187
+ for role, message in st.session_state.chat_history:
188
+ if role == "User":
189
+ st.text(f"User: {message}")
190
+ elif role == "Assistant":
191
+ st.text(f"Assistant: {message}")
 
 
192
 
193
  # Check if 'run' object has 'status' attribute
194
  if hasattr(st.session_state.run, 'status'):
 
209
  time.sleep(3) # Longer delay before retrying
210
  st.rerun()
211
  else:
212
+ st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
 
213
 
214
  # Handle any status that is not 'completed'
215
  elif st.session_state.run.status != "completed":
 
222
  time.sleep(3)
223
  st.rerun()
224
 
225
+
226
  if __name__ == "__main__":
227
  main()