|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from typing import Dict |
| 15 | +from google.adk.tools import ToolContext |
| 16 | +from volcenginesdkarkruntime import Ark |
| 17 | +from veadk.config import getenv |
| 18 | +import time |
| 19 | +import traceback |
| 20 | + |
| 21 | +from veadk.utils.logger import get_logger |
| 22 | + |
| 23 | +logger = get_logger(__name__) |
| 24 | + |
| 25 | +client = Ark( |
| 26 | + api_key=getenv("MODEL_VIDEO_API_KEY"), |
| 27 | + base_url=getenv("MODEL_VIDEO_API_BASE"), |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +async def generate(tool_context, prompt, first_frame_image=None, last_frame_image=None): |
| 32 | + try: |
| 33 | + if first_frame_image is None: |
| 34 | + logger.debug("text generation") |
| 35 | + response = client.content_generation.tasks.create( |
| 36 | + model=getenv("MODEL_VIDEO_NAME"), |
| 37 | + content=[ |
| 38 | + {"type": "text", "text": prompt}, |
| 39 | + ], |
| 40 | + ) |
| 41 | + elif last_frame_image is None: |
| 42 | + logger.debug("first frame generation") |
| 43 | + response = client.content_generation.tasks.create( |
| 44 | + model=getenv("MODEL_VIDEO_NAME"), |
| 45 | + content=[ |
| 46 | + {"type": "text", "text": prompt}, |
| 47 | + { |
| 48 | + "type": "image_url", |
| 49 | + "image_url": {"url": first_frame_image}, |
| 50 | + }, |
| 51 | + ], |
| 52 | + ) |
| 53 | + else: |
| 54 | + logger.debug("last frame generation") |
| 55 | + response = client.content_generation.tasks.create( |
| 56 | + model=getenv("MODEL_VIDEO_NAME"), |
| 57 | + content=[ |
| 58 | + {"type": "text", "text": prompt}, |
| 59 | + { |
| 60 | + "type": "image_url", |
| 61 | + "image_url": {"url": first_frame_image}, |
| 62 | + "role": "first_frame", |
| 63 | + }, |
| 64 | + { |
| 65 | + "type": "image_url", |
| 66 | + "image_url": {"url": last_frame_image}, |
| 67 | + "role": "last_frame", |
| 68 | + }, |
| 69 | + ], |
| 70 | + ) |
| 71 | + except: |
| 72 | + traceback.print_exc() |
| 73 | + raise |
| 74 | + return response |
| 75 | + |
| 76 | + |
| 77 | +async def video_generate(params: list, tool_context: ToolContext) -> Dict: |
| 78 | + """Generate video in batch according to the prompt. |
| 79 | +
|
| 80 | + Args: |
| 81 | + params: |
| 82 | + video_name: The name of the generated video. |
| 83 | + first_frame: The first frame of the video, url or base64 string, or None. |
| 84 | + last_frame:The last frame of the video, url or base64 string, or None. |
| 85 | + prompt:The prompt of the video. |
| 86 | + """ |
| 87 | + batch_size = 10 |
| 88 | + success_list = [] |
| 89 | + error_list = [] |
| 90 | + for start_idx in range(0, len(params), batch_size): |
| 91 | + batch = params[start_idx : start_idx + batch_size] |
| 92 | + task_dict = {} |
| 93 | + for item in batch: |
| 94 | + video_name = item["video_name"] |
| 95 | + first_frame = item["first_frame"] |
| 96 | + last_frame = item["last_frame"] |
| 97 | + prompt = item["prompt"] |
| 98 | + try: |
| 99 | + if not first_frame: |
| 100 | + response = await generate(tool_context, prompt) |
| 101 | + elif not last_frame: |
| 102 | + response = await generate(tool_context, prompt, first_frame) |
| 103 | + else: |
| 104 | + response = await generate( |
| 105 | + tool_context, prompt, first_frame, last_frame |
| 106 | + ) |
| 107 | + task_dict[response.id] = video_name |
| 108 | + except Exception: |
| 109 | + traceback.print_exc() |
| 110 | + while True: |
| 111 | + task_list = list(task_dict.keys()) |
| 112 | + if len(task_list) == 0: |
| 113 | + break |
| 114 | + for task_id in task_list: |
| 115 | + result = client.content_generation.tasks.get(task_id=task_id) |
| 116 | + status = result.status |
| 117 | + if status == "succeeded": |
| 118 | + logger.debug("----- task succeeded -----") |
| 119 | + tool_context.state[f"{task_dict[task_id]}_video_url"] = ( |
| 120 | + result.content.video_url |
| 121 | + ) |
| 122 | + success_list.append({task_dict[task_id]: result.content.video_url}) |
| 123 | + task_dict.pop(task_id, None) |
| 124 | + elif status == "failed": |
| 125 | + logger.debug("----- task failed -----") |
| 126 | + logger.debug(f"Error: {result.error}") |
| 127 | + error_list.append(task_dict[task_id]) |
| 128 | + task_dict.pop(task_id, None) |
| 129 | + else: |
| 130 | + logger.debug( |
| 131 | + f"Current status: {status}, Retrying after 10 seconds..." |
| 132 | + ) |
| 133 | + time.sleep(10) |
| 134 | + |
| 135 | + if len(success_list) == 0: |
| 136 | + return {"status": "error", "message": f"Following videos failed: {error_list}"} |
| 137 | + else: |
| 138 | + return { |
| 139 | + "status": "success", |
| 140 | + "message": f"Following videos generated: {success_list}\nFollowing videos failed: {error_list}", |
| 141 | + } |
0 commit comments