Spaces:
Sleeping
Sleeping
response_text = "抱歉,助手有點累,好像睡著了,請稍後再試。"
Browse files
app.py
CHANGED
@@ -1044,27 +1044,14 @@ def chat_with_youtube_transcript(youtube_id, thread_id, user_message, chat_histo
|
|
1044 |
)
|
1045 |
|
1046 |
# 等待助手响应,设定最大等待时间为 30 秒
|
1047 |
-
|
1048 |
-
start_time = time.time()
|
1049 |
-
while time.time() - start_time < max_wait_time:
|
1050 |
-
print("=== 等待助手响应 ===")
|
1051 |
-
print(f"run.status: {run.status}")
|
1052 |
-
print(f"time.time() - start_time: {time.time() - start_time}")
|
1053 |
-
print("=== 等待助手响应 ===")
|
1054 |
-
run_status = client.beta.threads.runs.retrieve(
|
1055 |
-
thread_id=thread_id,
|
1056 |
-
run_id=run.id
|
1057 |
-
)
|
1058 |
-
if run_status.status != "running":
|
1059 |
-
break
|
1060 |
-
time.sleep(3) # 每3秒检查一次状态
|
1061 |
-
|
1062 |
# 获取助手的响应消息
|
1063 |
-
|
1064 |
-
|
1065 |
-
|
1066 |
-
|
1067 |
-
|
|
|
1068 |
|
1069 |
# 更新聊天历史
|
1070 |
new_chat_history = (user_message, response_text)
|
@@ -1076,6 +1063,54 @@ def chat_with_youtube_transcript(youtube_id, thread_id, user_message, chat_histo
|
|
1076 |
# 返回聊天历史和空字符串清空输入框
|
1077 |
return "", chat_history, thread.id
|
1078 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1079 |
|
1080 |
def update_slide(direction):
|
1081 |
global TRANSCRIPTS
|
|
|
1044 |
)
|
1045 |
|
1046 |
# 等待助手响应,设定最大等待时间为 30 秒
|
1047 |
+
run_status = poll_run_status(run.id, thread.id, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1048 |
# 获取助手的响应消息
|
1049 |
+
if run_status == "completed":
|
1050 |
+
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
1051 |
+
assistant_responses = [msg for msg in messages.data if msg.role == "assistant"]
|
1052 |
+
response_text = assistant_responses[-1].content if assistant_responses
|
1053 |
+
else:
|
1054 |
+
response_text = "抱歉,助手有點累,好像睡著了,請稍後再試。"
|
1055 |
|
1056 |
# 更新聊天历史
|
1057 |
new_chat_history = (user_message, response_text)
|
|
|
1063 |
# 返回聊天历史和空字符串清空输入框
|
1064 |
return "", chat_history, thread.id
|
1065 |
|
1066 |
+
def poll_run_status(run_id, thread_id, timeout=600, poll_interval=5):
|
1067 |
+
"""
|
1068 |
+
Polls the status of a Run and handles different statuses appropriately.
|
1069 |
+
|
1070 |
+
:param run_id: The ID of the Run to poll.
|
1071 |
+
:param thread_id: The ID of the Thread associated with the Run.
|
1072 |
+
:param timeout: Maximum time to wait for the Run to complete, in seconds.
|
1073 |
+
:param poll_interval: Time to wait between each poll, in seconds.
|
1074 |
+
"""
|
1075 |
+
client = OPEN_AI_CLIENT
|
1076 |
+
start_time = time.time()
|
1077 |
+
while time.time() - start_time < timeout:
|
1078 |
+
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
|
1079 |
+
|
1080 |
+
if run.status in ["completed", "cancelled", "failed"]:
|
1081 |
+
print(f"Run completed with status: {run.status}")
|
1082 |
+
break
|
1083 |
+
elif run.status == "requires_action":
|
1084 |
+
print("Run requires action. Performing required action...")
|
1085 |
+
# Here, you would perform the required action, e.g., running functions
|
1086 |
+
# and then submitting the outputs. This is simplified for this example.
|
1087 |
+
# After performing the required action, you'd complete the action:
|
1088 |
+
# OPEN_AI_CLIENT.beta.threads.runs.complete_required_action(...)
|
1089 |
+
elif run.status == "expired":
|
1090 |
+
print("Run expired. Exiting...")
|
1091 |
+
break
|
1092 |
+
else:
|
1093 |
+
print(f"Run status is {run.status}. Waiting for updates...")
|
1094 |
+
|
1095 |
+
time.sleep(poll_interval)
|
1096 |
+
else:
|
1097 |
+
print("Timeout reached. Run did not complete in the expected time.")
|
1098 |
+
|
1099 |
+
# Once the Run is completed, handle the result accordingly
|
1100 |
+
if run.status == "completed":
|
1101 |
+
# Retrieve and handle messages or run steps as needed
|
1102 |
+
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
1103 |
+
for message in messages.data:
|
1104 |
+
if message.role == "assistant":
|
1105 |
+
print(f"Assistant response: {message.content}")
|
1106 |
+
elif run.status in ["cancelled", "failed"]:
|
1107 |
+
# Handle cancellation or failure
|
1108 |
+
print(f"Run ended with status: {run.status}")
|
1109 |
+
elif run.status == "expired":
|
1110 |
+
# Handle expired run
|
1111 |
+
print("Run expired without completion.")
|
1112 |
+
|
1113 |
+
return run.status
|
1114 |
|
1115 |
def update_slide(direction):
|
1116 |
global TRANSCRIPTS
|