Anne31415 commited on
Commit
723663c
·
verified ·
1 Parent(s): 05cd51e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -33
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # Importing required packages
2
  import streamlit as st
3
  import openai
4
  import uuid
@@ -94,7 +94,7 @@ def page1():
94
 
95
  with col1:
96
  st.title('Welcome to MediMetrics!')
97
- st.subheader('powered by :orange_heart:[BinDoc GmbH] ')
98
 
99
  with col2:
100
  # Load and display the image in the right column
@@ -105,41 +105,31 @@ def page1():
105
  if 'chat_history' not in st.session_state:
106
  st.session_state.chat_history = []
107
 
108
- # Define flags in the session state for processing control
109
- if 'processing' not in st.session_state:
110
- st.session_state.processing = False
111
-
112
  # Function to handle question submission
113
  def submit_question(question):
114
- if not st.session_state.processing:
115
- st.session_state.processing = True
116
- st.session_state.chat_history.append(("User", question))
117
- process_question(question)
118
- st.session_state.processing = False
119
- st.experimental_rerun()
120
 
121
  # Function to process the question
122
  def process_question(question):
123
- # Clear the previous run status and messages
124
  st.session_state.run = None
125
- st.session_state.messages = []
126
-
127
- # Start with the current user question in chat history
128
- st.session_state.chat_history = [("User", question)]
129
-
130
  # Add message to the thread
131
  client.beta.threads.messages.create(
132
  thread_id=st.session_state.thread.id,
133
  role="user",
134
  content=question
135
  )
136
-
137
  # Create a new run to process the messages in the thread
138
  st.session_state.run = client.beta.threads.runs.create(
139
  thread_id=st.session_state.thread.id,
140
  assistant_id=st.session_state.assistant.id,
141
  )
142
-
143
  # Wait for the run to complete
144
  while not hasattr(st.session_state.run, 'status') or st.session_state.run.status not in ["completed", "failed"]:
145
  time.sleep(1)
@@ -147,30 +137,20 @@ def page1():
147
  thread_id=st.session_state.thread.id,
148
  run_id=st.session_state.run.id,
149
  )
150
-
151
  if st.session_state.run.status == "completed":
152
  # Retrieve the list of messages
153
  st.session_state.messages = client.beta.threads.messages.list(
154
  thread_id=st.session_state.thread.id
155
  )
156
-
157
- # Find the last assistant message
158
  last_assistant_message = next((message for message in reversed(st.session_state.messages.data) if message.role == "assistant"), None)
159
  if last_assistant_message:
160
  # Update chat history with the assistant's response
161
  st.session_state.chat_history.append(("Assistant", last_assistant_message.content[0].text.value))
162
-
163
- # Display chat history
164
- for role, message in st.session_state.chat_history:
165
- if role == "User":
166
- with st.chat_message("User"):
167
- st.markdown(message)
168
- elif role == "Assistant":
169
- with st.chat_message("Assistant"):
170
- st.markdown(message)
171
 
172
 
173
-
174
  # Example query buttons
175
  col3, col4 = st.columns(2) # Create two columns for buttons
176
 
@@ -201,6 +181,7 @@ def page1():
201
  metadata={'session_id': st.session_state.session_id}
202
  )
203
 
 
204
  # Check if the run is completed and display the last assistant message
205
  if hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
206
  # Retrieve the list of messages
 
1
+ # Importing required packages
2
  import streamlit as st
3
  import openai
4
  import uuid
 
94
 
95
  with col1:
96
  st.title('Welcome to MediMetrics!')
97
+ st.subheader('powered by :orange[BinDoc GmbH] ')
98
 
99
  with col2:
100
  # Load and display the image in the right column
 
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
+ # Clear the previous run status and chat history
117
  st.session_state.run = None
118
+ st.session_state.chat_history = [("User", question)] # Start with the current user question
119
+
 
 
 
120
  # Add message to the thread
121
  client.beta.threads.messages.create(
122
  thread_id=st.session_state.thread.id,
123
  role="user",
124
  content=question
125
  )
126
+
127
  # Create a new run to process the messages in the thread
128
  st.session_state.run = client.beta.threads.runs.create(
129
  thread_id=st.session_state.thread.id,
130
  assistant_id=st.session_state.assistant.id,
131
  )
132
+
133
  # Wait for the run to complete
134
  while not hasattr(st.session_state.run, 'status') or st.session_state.run.status not in ["completed", "failed"]:
135
  time.sleep(1)
 
137
  thread_id=st.session_state.thread.id,
138
  run_id=st.session_state.run.id,
139
  )
140
+
141
  if st.session_state.run.status == "completed":
142
  # Retrieve the list of messages
143
  st.session_state.messages = client.beta.threads.messages.list(
144
  thread_id=st.session_state.thread.id
145
  )
146
+
147
+ # Get the latest message from the assistant
148
  last_assistant_message = next((message for message in reversed(st.session_state.messages.data) if message.role == "assistant"), None)
149
  if last_assistant_message:
150
  # Update chat history with the assistant's response
151
  st.session_state.chat_history.append(("Assistant", last_assistant_message.content[0].text.value))
 
 
 
 
 
 
 
 
 
152
 
153
 
 
154
  # Example query buttons
155
  col3, col4 = st.columns(2) # Create two columns for buttons
156
 
 
181
  metadata={'session_id': st.session_state.session_id}
182
  )
183
 
184
+
185
  # Check if the run is completed and display the last assistant message
186
  if hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
187
  # Retrieve the list of messages