Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
examples/callback/business/callbacks.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding: utf-8
|
2 |
+
# Copyright (c) 2025 inclusionAI.
|
3 |
+
|
4 |
+
"""
|
5 |
+
Callback function registration module, used for centralized management and registration of all callback functions.
|
6 |
+
"""
|
7 |
+
|
8 |
+
from aworld.runners.callback.decorator import reg_callback, CallbackRegistry
|
9 |
+
|
10 |
+
|
11 |
+
# Register a simple callback function
|
12 |
+
@reg_callback("print_content")
|
13 |
+
def simple_callback(content):
|
14 |
+
"""Simple callback function that prints content and returns it
|
15 |
+
|
16 |
+
Args:
|
17 |
+
content: Content to print
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
The input content
|
21 |
+
"""
|
22 |
+
print(f"Callback function received content: {content}")
|
23 |
+
return content
|
24 |
+
|
25 |
+
|
26 |
+
# You can register more callback functions here
|
27 |
+
@reg_callback("uppercase_content")
|
28 |
+
def uppercase_callback(content):
|
29 |
+
"""Callback function that converts content to uppercase
|
30 |
+
|
31 |
+
Args:
|
32 |
+
content: Content to process
|
33 |
+
|
34 |
+
Returns:
|
35 |
+
Content converted to uppercase
|
36 |
+
"""
|
37 |
+
if isinstance(content, str):
|
38 |
+
result = content.upper()
|
39 |
+
print(f"Callback function converted content to uppercase: {result}")
|
40 |
+
return result
|
41 |
+
return content
|
42 |
+
|
43 |
+
|
44 |
+
# Provide a function to check all registered callback functions
|
45 |
+
def list_all_callbacks():
|
46 |
+
"""List all registered callback functions"""
|
47 |
+
callbacks = CallbackRegistry.list()
|
48 |
+
print("Registered callback functions:")
|
49 |
+
for key, func_name in callbacks.items():
|
50 |
+
print(f" - {key}: {func_name}")
|
51 |
+
return callbacks
|
examples/callback/business/video_callback.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
|
5 |
+
import requests
|
6 |
+
from aworld.core.common import Observation, ActionResult
|
7 |
+
from typing_extensions import Any
|
8 |
+
|
9 |
+
from aworld.runners.callback.decorator import reg_callback
|
10 |
+
|
11 |
+
from aworld.logs.util import logger
|
12 |
+
|
13 |
+
@reg_callback("gen_video_server__video_tasks")
|
14 |
+
def gen_video(actionResult:ActionResult) -> ActionResult:
|
15 |
+
try:
|
16 |
+
if not actionResult or not actionResult.content:
|
17 |
+
return actionResult
|
18 |
+
content = json.loads(actionResult.content)
|
19 |
+
task_id = content.get("video_id")
|
20 |
+
if not task_id:
|
21 |
+
return actionResult
|
22 |
+
gen_video_item(task_id)
|
23 |
+
|
24 |
+
return actionResult
|
25 |
+
|
26 |
+
except Exception as e:
|
27 |
+
logger.warning(f"Exception gen_video occurred: {e}")
|
28 |
+
return actionResult
|
29 |
+
|
30 |
+
def gen_video_item(task_id:str) -> Any:
|
31 |
+
if not task_id:
|
32 |
+
return None
|
33 |
+
try:
|
34 |
+
from dotenv import load_dotenv
|
35 |
+
load_dotenv()
|
36 |
+
api_key = os.getenv('DASHSCOPE_API_KEY')
|
37 |
+
query_base_url = os.getenv('DASHSCOPE_QUERY_BASE_URL', '')
|
38 |
+
# Step 2: Poll for results
|
39 |
+
max_attempts = int(os.getenv('DASHSCOPE_VIDEO_RETRY_TIMES', 10)) # Increased default retries for video
|
40 |
+
wait_time = int(os.getenv('DASHSCOPE_VIDEO_SLEEP_TIME', 5)) # Increased default wait time for video
|
41 |
+
query_url = f"{query_base_url}{task_id}"
|
42 |
+
|
43 |
+
for attempt in range(max_attempts):
|
44 |
+
# Wait before polling
|
45 |
+
time.sleep(wait_time)
|
46 |
+
logger.info(f"Polling attempt {attempt + 1}/{max_attempts}...")
|
47 |
+
|
48 |
+
# Poll for results
|
49 |
+
query_response = requests.get(query_url, headers={'Authorization': f'Bearer {api_key}'})
|
50 |
+
|
51 |
+
if query_response.status_code != 200:
|
52 |
+
logger.info(f"Poll request failed with status code {query_response.status_code}")
|
53 |
+
continue
|
54 |
+
|
55 |
+
try:
|
56 |
+
query_result = query_response.json()
|
57 |
+
except json.JSONDecodeError as e:
|
58 |
+
logger.warning(f"Failed to parse response as JSON: {e}")
|
59 |
+
continue
|
60 |
+
|
61 |
+
# Check task status
|
62 |
+
task_status = query_result.get("output", {}).get("task_status")
|
63 |
+
|
64 |
+
if task_status == "SUCCEEDED":
|
65 |
+
# Extract video URL
|
66 |
+
video_url = query_result.get("output", {}).get("video_url")
|
67 |
+
|
68 |
+
if video_url:
|
69 |
+
# Return as array of objects with video_url for consistency with image API
|
70 |
+
return json.dumps({"video_url": video_url})
|
71 |
+
else:
|
72 |
+
logger.info("Video URL not found in the response")
|
73 |
+
return None
|
74 |
+
elif task_status in ["PENDING", "RUNNING"]:
|
75 |
+
# If still running, continue to next polling attempt
|
76 |
+
logger.info(f"gen_video_item Task status: {task_status}, continuing to next poll...")
|
77 |
+
continue
|
78 |
+
elif task_status == "FAILED":
|
79 |
+
logger.warning("Task failed")
|
80 |
+
return None
|
81 |
+
else:
|
82 |
+
# Any other status, return None
|
83 |
+
logger.warning(f"Unexpected status: {task_status}")
|
84 |
+
return None
|
85 |
+
|
86 |
+
# If we get here, polling timed out
|
87 |
+
logger.warning("Polling timed out after maximum attempts")
|
88 |
+
return None
|
89 |
+
except Exception as e:
|
90 |
+
logger.warning(f"Exception gen_video_item occurred: {e}")
|
91 |
+
return None
|