Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,6 +27,10 @@ def handle_and_display_image(file_id):
|
|
| 27 |
print('Failed to download file.')
|
| 28 |
return None
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Initialize global session state
|
| 31 |
if "session_id" not in st.session_state:
|
| 32 |
st.session_state.session_id = str(uuid.uuid4())
|
|
@@ -83,63 +87,115 @@ def page2():
|
|
| 83 |
|
| 84 |
def page1():
|
| 85 |
st.sidebar.title("OpenAI - Assistant API🤖")
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
if "assistant" not in st.session_state:
|
| 90 |
openai.api_key = api_key
|
| 91 |
st.session_state.assistant = openai.beta.assistants.retrieve(assistant_id)
|
| 92 |
st.session_state.thread = client.beta.threads.create(metadata={'session_id': st.session_state.session_id})
|
| 93 |
|
| 94 |
-
|
| 95 |
-
st.session_state.messages = []
|
| 96 |
-
|
| 97 |
-
if "retry_error" not in st.session_state:
|
| 98 |
-
st.session_state.retry_error = 0
|
| 99 |
-
|
| 100 |
-
# User inputs a query
|
| 101 |
prompt = st.text_input("Wie kann ich dir helfen?")
|
| 102 |
|
|
|
|
| 103 |
if st.button("Submit"):
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
if hasattr(st.session_state.run, 'status'):
|
|
|
|
| 118 |
if st.session_state.run.status == "running":
|
| 119 |
-
st.
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
for message in st.session_state.messages:
|
| 127 |
-
st.text(f"{message.role}: {message.content.get('text', 'No text found')}")
|
| 128 |
elif st.session_state.run.status == "failed":
|
| 129 |
st.session_state.retry_error += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
if st.session_state.retry_error < 3:
|
| 131 |
-
st.text("Run failed, retrying ......")
|
| 132 |
time.sleep(3)
|
| 133 |
st.rerun()
|
| 134 |
-
else:
|
| 135 |
-
st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
|
| 136 |
-
# Handle other statuses if needed
|
| 137 |
-
|
| 138 |
-
# Reset retry_error if needed
|
| 139 |
-
if st.button("Reset"):
|
| 140 |
-
st.session_state.retry_error = 0
|
| 141 |
-
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
main()
|
| 145 |
-
|
|
|
|
| 27 |
print('Failed to download file.')
|
| 28 |
return None
|
| 29 |
|
| 30 |
+
|
| 31 |
+
if "api_call_status" not in st.session_state:
|
| 32 |
+
st.session_state.api_call_status = None
|
| 33 |
+
|
| 34 |
# Initialize global session state
|
| 35 |
if "session_id" not in st.session_state:
|
| 36 |
st.session_state.session_id = str(uuid.uuid4())
|
|
|
|
| 87 |
|
| 88 |
def page1():
|
| 89 |
st.sidebar.title("OpenAI - Assistant API🤖")
|
| 90 |
+
st.sidebar.markdown("**Model:** gpt-4-1106-preview API")
|
| 91 |
+
st.sidebar.markdown("**Version:** 1.1")
|
| 92 |
+
st.sidebar.markdown("**Session-ID:**")
|
| 93 |
+
st.sidebar.markdown(st.session_state.session_id)
|
| 94 |
+
st.sidebar.divider()
|
| 95 |
+
st.title("Assitent-Name")
|
| 96 |
+
st.text("Beschreibung")
|
| 97 |
+
|
| 98 |
|
| 99 |
+
if "assistant" not in st.session_state:
|
|
|
|
| 100 |
openai.api_key = api_key
|
| 101 |
st.session_state.assistant = openai.beta.assistants.retrieve(assistant_id)
|
| 102 |
st.session_state.thread = client.beta.threads.create(metadata={'session_id': st.session_state.session_id})
|
| 103 |
|
| 104 |
+
# User input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
prompt = st.text_input("Wie kann ich dir helfen?")
|
| 106 |
|
| 107 |
+
# Submit button
|
| 108 |
if st.button("Submit"):
|
| 109 |
+
st.session_state.api_call_status = "pending"
|
| 110 |
+
# Here, use the prompt to make your API call
|
| 111 |
+
# For example, adding the prompt to the thread and creating a run
|
| 112 |
+
response = "This is a simulated response" # Replace with actual response
|
| 113 |
+
st.session_state.messages.append(("User", prompt))
|
| 114 |
+
st.session_state.messages.append(("Assistant", response))
|
| 115 |
+
st.session_state.api_call_status = "completed"
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# Display messages
|
| 119 |
+
for role, message in st.session_state.messages:
|
| 120 |
+
st.text(f"{role}: {message}")
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# If the run is completed, display the messages
|
| 125 |
+
elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
| 126 |
+
# Retrieve the list of messages
|
| 127 |
+
st.session_state.messages = client.beta.threads.messages.list(
|
| 128 |
+
thread_id=st.session_state.thread.id
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# Display messages
|
| 133 |
+
for message in reversed(st.session_state.messages.data):
|
| 134 |
+
if message.role in ["user", "assistant"]:
|
| 135 |
+
with st.chat_message(message.role):
|
| 136 |
+
for content_part in message.content:
|
| 137 |
+
if content_part.type == 'text': # For text responses
|
| 138 |
+
message_text = content_part.text.value
|
| 139 |
+
st.markdown(message_text)
|
| 140 |
+
elif hasattr(content_part, 'image_file') and content_part.image_file:
|
| 141 |
+
image = handle_and_display_image(content_part.image_file.file_id)
|
| 142 |
+
if image:
|
| 143 |
+
st.image(image, caption="Generated Image")
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# Display chat history
|
| 148 |
+
for sender, message in st.session_state.chat_history:
|
| 149 |
+
st.text(f"{sender}: {message}")
|
| 150 |
+
|
| 151 |
|
| 152 |
+
# Add message to the thread
|
| 153 |
+
st.session_state.messages = client.beta.threads.messages.create(
|
| 154 |
+
thread_id=st.session_state.thread.id,
|
| 155 |
+
role="user",
|
| 156 |
+
content=prompt
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# Do a run to process the messages in the thread
|
| 160 |
+
st.session_state.run = client.beta.threads.runs.create(
|
| 161 |
+
thread_id=st.session_state.thread.id,
|
| 162 |
+
assistant_id=st.session_state.assistant.id,
|
| 163 |
+
)
|
| 164 |
+
if st.session_state.retry_error < 3:
|
| 165 |
+
time.sleep(1) # Wait 1 second before checking run status
|
| 166 |
+
st.rerun()
|
| 167 |
+
|
| 168 |
+
# Check if 'run' object has 'status' attribute
|
| 169 |
if hasattr(st.session_state.run, 'status'):
|
| 170 |
+
# Handle the 'running' status
|
| 171 |
if st.session_state.run.status == "running":
|
| 172 |
+
with st.chat_message('assistant'):
|
| 173 |
+
st.write("Thinking ......")
|
| 174 |
+
if st.session_state.retry_error < 3:
|
| 175 |
+
time.sleep(1) # Short delay to prevent immediate rerun
|
| 176 |
+
st.rerun()
|
| 177 |
+
|
| 178 |
+
# Handle the 'failed' status
|
|
|
|
|
|
|
| 179 |
elif st.session_state.run.status == "failed":
|
| 180 |
st.session_state.retry_error += 1
|
| 181 |
+
with st.chat_message('assistant'):
|
| 182 |
+
if st.session_state.retry_error < 3:
|
| 183 |
+
st.write("Run failed, retrying ......")
|
| 184 |
+
time.sleep(3) # Longer delay before retrying
|
| 185 |
+
st.rerun()
|
| 186 |
+
else:
|
| 187 |
+
st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
|
| 188 |
+
|
| 189 |
+
# Handle any status that is not 'completed'
|
| 190 |
+
elif st.session_state.run.status != "completed":
|
| 191 |
+
# Attempt to retrieve the run again
|
| 192 |
+
st.session_state.run = client.beta.threads.runs.retrieve(
|
| 193 |
+
thread_id=st.session_state.thread.id,
|
| 194 |
+
run_id=st.session_state.run.id,
|
| 195 |
+
)
|
| 196 |
if st.session_state.retry_error < 3:
|
|
|
|
| 197 |
time.sleep(3)
|
| 198 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
main()
|
|
|