Anne31415 commited on
Commit
ca8148f
·
1 Parent(s): e03ef2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -101,6 +101,27 @@ def page1():
101
  metadata={'session_id': st.session_state.session_id}
102
  )
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  prompt = st.text_input("Wie kann ich dir helfen?")
105
  if st.button("Submit"):
106
  # Add message to the thread
@@ -120,9 +141,8 @@ def page1():
120
  time.sleep(1) # Wait before checking run status
121
  st.rerun()
122
 
123
- # Check if 'run' object has 'status' attribute
124
  if hasattr(st.session_state.run, 'status'):
125
- # Handle the 'running' status
126
  if st.session_state.run.status == "running":
127
  with st.chat_message('assistant'):
128
  st.write("Thinking ......")
@@ -130,7 +150,6 @@ def page1():
130
  time.sleep(1) # Short delay to prevent immediate rerun
131
  st.rerun()
132
 
133
- # Handle the 'failed' status
134
  elif st.session_state.run.status == "failed":
135
  st.session_state.retry_error += 1
136
  with st.chat_message('assistant'):
@@ -141,7 +160,6 @@ def page1():
141
  else:
142
  st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
143
 
144
- # Handle any status that is not 'completed'
145
  elif st.session_state.run.status != "completed":
146
  st.session_state.run = client.beta.threads.runs.retrieve(
147
  thread_id=st.session_state.thread.id,
@@ -151,25 +169,6 @@ def page1():
151
  time.sleep(3)
152
  st.rerun()
153
 
154
- # If the run is completed, display the messages
155
- elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
156
- # Retrieve the list of messages
157
- st.session_state.messages = client.beta.threads.messages.list(
158
- thread_id=st.session_state.thread.id
159
- )
160
-
161
- # Display messages
162
- for message in reversed(st.session_state.messages.data):
163
- if message.role in ["user", "assistant"]:
164
- with st.chat_message(message.role):
165
- for content_part in message.content:
166
- if content_part.type == 'text': # For text responses
167
- message_text = content_part.text.value
168
- st.markdown(message_text)
169
- elif hasattr(content_part, 'image_file') and content_part.image_file:
170
- image = handle_and_display_image(content_part.image_file.file_id)
171
- if image:
172
- st.image(image, caption="Generated Image")
173
 
174
 
175
  if __name__ == "__main__":
 
101
  metadata={'session_id': st.session_state.session_id}
102
  )
103
 
104
+ # Check if 'run' object has 'status' attribute and display messages
105
+ if "run" in st.session_state and hasattr(st.session_state.run, 'status'):
106
+ if st.session_state.run.status == "completed":
107
+ # Retrieve the list of messages
108
+ st.session_state.messages = client.beta.threads.messages.list(
109
+ thread_id=st.session_state.thread.id
110
+ )
111
+
112
+ # Display messages
113
+ for message in reversed(st.session_state.messages.data):
114
+ if message.role in ["user", "assistant"]:
115
+ with st.chat_message(message.role):
116
+ for content_part in message.content:
117
+ if content_part.type == 'text':
118
+ st.markdown(content_part.text.value)
119
+ elif hasattr(content_part, 'image_file') and content_part.image_file:
120
+ image = handle_and_display_image(content_part.image_file.file_id)
121
+ if image:
122
+ st.image(image, caption="Generated Image")
123
+
124
+ # User input and submission
125
  prompt = st.text_input("Wie kann ich dir helfen?")
126
  if st.button("Submit"):
127
  # Add message to the thread
 
141
  time.sleep(1) # Wait before checking run status
142
  st.rerun()
143
 
144
+ # Handle the run's different statuses (running, failed, etc.)
145
  if hasattr(st.session_state.run, 'status'):
 
146
  if st.session_state.run.status == "running":
147
  with st.chat_message('assistant'):
148
  st.write("Thinking ......")
 
150
  time.sleep(1) # Short delay to prevent immediate rerun
151
  st.rerun()
152
 
 
153
  elif st.session_state.run.status == "failed":
154
  st.session_state.retry_error += 1
155
  with st.chat_message('assistant'):
 
160
  else:
161
  st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
162
 
 
163
  elif st.session_state.run.status != "completed":
164
  st.session_state.run = client.beta.threads.runs.retrieve(
165
  thread_id=st.session_state.thread.id,
 
169
  time.sleep(3)
170
  st.rerun()
171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
 
173
 
174
  if __name__ == "__main__":