Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -101,31 +101,31 @@ def page1():
|
|
| 101 |
metadata={'session_id': st.session_state.session_id}
|
| 102 |
)
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 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 |
-
# Filter for assistant messages
|
| 113 |
-
assistant_messages = [m for m in st.session_state.messages.data if m.role == "assistant"]
|
| 114 |
-
|
| 115 |
-
# Get the last assistant message
|
| 116 |
-
if assistant_messages:
|
| 117 |
-
final_assistant_message = assistant_messages[-1]
|
| 118 |
-
with st.chat_message('assistant'):
|
| 119 |
-
for content_part in final_assistant_message.content:
|
| 120 |
-
if content_part.type == 'text':
|
| 121 |
-
st.markdown(content_part.text.value)
|
| 122 |
-
elif hasattr(content_part, 'image_file') and content_part.image_file:
|
| 123 |
-
image = handle_and_display_image(content_part.image_file.file_id)
|
| 124 |
-
if image:
|
| 125 |
-
st.image(image, caption="Generated Image")
|
| 126 |
-
# User input and submission
|
| 127 |
prompt = st.text_input("Wie kann ich dir helfen?")
|
| 128 |
if st.button("Submit"):
|
|
|
|
|
|
|
| 129 |
# Add message to the thread
|
| 130 |
st.session_state.messages = client.beta.threads.messages.create(
|
| 131 |
thread_id=st.session_state.thread.id,
|
|
@@ -138,30 +138,34 @@ def page1():
|
|
| 138 |
thread_id=st.session_state.thread.id,
|
| 139 |
assistant_id=st.session_state.assistant.id,
|
| 140 |
)
|
| 141 |
-
|
| 142 |
if st.session_state.retry_error < 3:
|
| 143 |
-
time.sleep(1)
|
| 144 |
st.rerun()
|
| 145 |
-
|
| 146 |
-
|
| 147 |
if hasattr(st.session_state.run, 'status'):
|
|
|
|
| 148 |
if st.session_state.run.status == "running":
|
| 149 |
with st.chat_message('assistant'):
|
| 150 |
st.write("Thinking ......")
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
elif st.session_state.run.status == "failed":
|
| 155 |
st.session_state.retry_error += 1
|
| 156 |
with st.chat_message('assistant'):
|
| 157 |
if st.session_state.retry_error < 3:
|
| 158 |
-
|
|
|
|
| 159 |
st.rerun()
|
| 160 |
else:
|
| 161 |
st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
|
| 162 |
|
| 163 |
-
|
| 164 |
elif st.session_state.run.status != "completed":
|
|
|
|
| 165 |
st.session_state.run = client.beta.threads.runs.retrieve(
|
| 166 |
thread_id=st.session_state.thread.id,
|
| 167 |
run_id=st.session_state.run.id,
|
|
@@ -170,8 +174,5 @@ def page1():
|
|
| 170 |
time.sleep(3)
|
| 171 |
st.rerun()
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
if __name__ == "__main__":
|
| 177 |
-
main()
|
|
|
|
| 101 |
metadata={'session_id': st.session_state.session_id}
|
| 102 |
)
|
| 103 |
|
| 104 |
+
# Check if the run is completed and display the last assistant message
|
| 105 |
+
if hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
| 106 |
+
# Retrieve the list of messages
|
| 107 |
+
st.session_state.messages = client.beta.threads.messages.list(
|
| 108 |
+
thread_id=st.session_state.thread.id
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Find the last assistant message
|
| 112 |
+
last_assistant_message = next((message for message in reversed(st.session_state.messages.data) if message.role == "assistant"), None)
|
| 113 |
+
if last_assistant_message:
|
| 114 |
+
with st.chat_message('assistant'):
|
| 115 |
+
for content_part in last_assistant_message.content:
|
| 116 |
+
if content_part.type == 'text':
|
| 117 |
+
st.markdown(content_part.text.value)
|
| 118 |
+
elif hasattr(content_part, 'image_file') and content_part.image_file:
|
| 119 |
+
image = handle_and_display_image(content_part.image_file.file_id)
|
| 120 |
+
if image:
|
| 121 |
+
st.image(image, caption="Generated Image")
|
| 122 |
+
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
prompt = st.text_input("Wie kann ich dir helfen?")
|
| 126 |
if st.button("Submit"):
|
| 127 |
+
|
| 128 |
+
|
| 129 |
# Add message to the thread
|
| 130 |
st.session_state.messages = client.beta.threads.messages.create(
|
| 131 |
thread_id=st.session_state.thread.id,
|
|
|
|
| 138 |
thread_id=st.session_state.thread.id,
|
| 139 |
assistant_id=st.session_state.assistant.id,
|
| 140 |
)
|
|
|
|
| 141 |
if st.session_state.retry_error < 3:
|
| 142 |
+
time.sleep(1) # Wait 1 second before checking run status
|
| 143 |
st.rerun()
|
| 144 |
+
|
| 145 |
+
# Check if 'run' object has 'status' attribute
|
| 146 |
if hasattr(st.session_state.run, 'status'):
|
| 147 |
+
# Handle the 'running' status
|
| 148 |
if st.session_state.run.status == "running":
|
| 149 |
with st.chat_message('assistant'):
|
| 150 |
st.write("Thinking ......")
|
| 151 |
+
if st.session_state.retry_error < 3:
|
| 152 |
+
time.sleep(1) # Short delay to prevent immediate rerun
|
| 153 |
+
st.rerun()
|
| 154 |
+
|
| 155 |
+
# Handle the 'failed' status
|
| 156 |
elif st.session_state.run.status == "failed":
|
| 157 |
st.session_state.retry_error += 1
|
| 158 |
with st.chat_message('assistant'):
|
| 159 |
if st.session_state.retry_error < 3:
|
| 160 |
+
st.write("Run failed, retrying ......")
|
| 161 |
+
time.sleep(3) # Longer delay before retrying
|
| 162 |
st.rerun()
|
| 163 |
else:
|
| 164 |
st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
|
| 165 |
|
| 166 |
+
# Handle any status that is not 'completed'
|
| 167 |
elif st.session_state.run.status != "completed":
|
| 168 |
+
# Attempt to retrieve the run again
|
| 169 |
st.session_state.run = client.beta.threads.runs.retrieve(
|
| 170 |
thread_id=st.session_state.thread.id,
|
| 171 |
run_id=st.session_state.run.id,
|
|
|
|
| 174 |
time.sleep(3)
|
| 175 |
st.rerun()
|
| 176 |
|
|
|
|
|
|
|
|
|
|
| 177 |
if __name__ == "__main__":
|
| 178 |
+
main()
|