Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -101,13 +101,8 @@ def page1():
|
|
| 101 |
metadata={'session_id': st.session_state.session_id}
|
| 102 |
)
|
| 103 |
|
| 104 |
-
# User input and submission
|
| 105 |
prompt = st.text_input("Wie kann ich dir helfen?")
|
| 106 |
if st.button("Submit"):
|
| 107 |
-
# Display user's message immediately
|
| 108 |
-
with st.chat_message('user'):
|
| 109 |
-
st.markdown(prompt)
|
| 110 |
-
|
| 111 |
# Add message to the thread
|
| 112 |
st.session_state.messages = client.beta.threads.messages.create(
|
| 113 |
thread_id=st.session_state.thread.id,
|
|
@@ -121,13 +116,13 @@ def page1():
|
|
| 121 |
assistant_id=st.session_state.assistant.id,
|
| 122 |
)
|
| 123 |
|
| 124 |
-
# Check run status
|
| 125 |
if st.session_state.retry_error < 3:
|
| 126 |
-
time.sleep(1)
|
| 127 |
st.rerun()
|
| 128 |
|
| 129 |
-
#
|
| 130 |
if hasattr(st.session_state.run, 'status'):
|
|
|
|
| 131 |
if st.session_state.run.status == "running":
|
| 132 |
with st.chat_message('assistant'):
|
| 133 |
st.write("Thinking ......")
|
|
@@ -135,6 +130,7 @@ def page1():
|
|
| 135 |
time.sleep(1) # Short delay to prevent immediate rerun
|
| 136 |
st.rerun()
|
| 137 |
|
|
|
|
| 138 |
elif st.session_state.run.status == "failed":
|
| 139 |
st.session_state.retry_error += 1
|
| 140 |
with st.chat_message('assistant'):
|
|
@@ -145,6 +141,7 @@ def page1():
|
|
| 145 |
else:
|
| 146 |
st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
|
| 147 |
|
|
|
|
| 148 |
elif st.session_state.run.status != "completed":
|
| 149 |
st.session_state.run = client.beta.threads.runs.retrieve(
|
| 150 |
thread_id=st.session_state.thread.id,
|
|
@@ -154,26 +151,26 @@ def page1():
|
|
| 154 |
time.sleep(3)
|
| 155 |
st.rerun()
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
elif st.session_state.run.status == "completed":
|
| 159 |
# Retrieve the list of messages
|
| 160 |
st.session_state.messages = client.beta.threads.messages.list(
|
| 161 |
thread_id=st.session_state.thread.id
|
| 162 |
)
|
| 163 |
-
|
| 164 |
-
#
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
|
| 178 |
|
| 179 |
|
|
|
|
| 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
|
| 107 |
st.session_state.messages = client.beta.threads.messages.create(
|
| 108 |
thread_id=st.session_state.thread.id,
|
|
|
|
| 116 |
assistant_id=st.session_state.assistant.id,
|
| 117 |
)
|
| 118 |
|
|
|
|
| 119 |
if st.session_state.retry_error < 3:
|
| 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 |
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 |
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 |
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 |
|
| 176 |
|