Spaces:
Running
Running
File size: 13,344 Bytes
0b909d1 55bc200 0b909d1 55bc200 0b909d1 55bc200 0b909d1 55bc200 0b909d1 55bc200 0b909d1 55bc200 0b909d1 55bc200 0b909d1 55bc200 3547d18 55bc200 658070f 55bc200 1bbd1d4 7b7aba3 1bbd1d4 55bc200 658070f 55bc200 0b909d1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# app.py
import os
import oss2
import sys
import uuid
import shutil
import time
import gradio as gr
import requests
import dashscope
from dashscope.utils.oss_utils import check_and_upload_local
DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY")
dashscope.api_key = DASHSCOPE_API_KEY
class WanAnimateApp:
def __init__(self, url, get_url):
self.url = url
self.get_url = get_url
def predict(
self,
ref_img,
video,
model_id,
model,
):
# Upload files to OSS if needed and get URLs
_, image_url = check_and_upload_local(model_id, ref_img, DASHSCOPE_API_KEY)
_, video_url = check_and_upload_local(model_id, video, DASHSCOPE_API_KEY)
# Prepare the request payload
payload = {
"model": model_id,
"input": {
"image_url": image_url,
"video_url": video_url
},
"parameters": {
"check_image": True,
"mode": model,
}
}
# Set up headers
headers = {
"X-DashScope-Async": "enable",
"X-DashScope-OssResourceResolve": "enable",
"Authorization": f"Bearer {DASHSCOPE_API_KEY}",
"Content-Type": "application/json"
}
# Make the initial API request
url = self.url
response = requests.post(url, json=payload, headers=headers)
# Check if request was successful
if response.status_code != 200:
raise Exception(f"Initial request failed with status code {response.status_code}: {response.text}")
# Get the task ID from response
result = response.json()
task_id = result.get("output", {}).get("task_id")
if not task_id:
raise Exception("Failed to get task ID from response")
# Poll for results
get_url = f"{self.get_url}/{task_id}"
headers = {
"Authorization": f"Bearer {DASHSCOPE_API_KEY}",
"Content-Type": "application/json"
}
while True:
response = requests.get(get_url, headers=headers)
if response.status_code != 200:
raise Exception(f"Failed to get task status: {response.status_code}: {response.text}")
result = response.json()
print(result)
task_status = result.get("output", {}).get("task_status")
if task_status == "SUCCEEDED":
# Task completed successfully, return video URL
video_url = result["output"]["results"]["video_url"]
return video_url, "SUCCEEDED"
elif task_status == "FAILED":
# Task failed, raise an exception with error message
error_msg = result.get("output", {}).get("message", "Unknown error")
code_msg = result.get("output", {}).get("code", "Unknown code")
print(f"\n\nTask failed: {error_msg} Code: {code_msg} TaskId: {task_id}\n\n")
return None, f"Task failed: {error_msg} Code: {code_msg} TaskId: {task_id}"
# raise Exception(f"Task failed: {error_msg} TaskId: {task_id}")
else:
# Task is still running, wait and retry
time.sleep(5) # Wait 5 seconds before polling again
def start_app():
import argparse
parser = argparse.ArgumentParser(description="Wan2.2-Animate 视频生成工具")
args = parser.parse_args()
url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis/"
# url = "https://poc-dashscope.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis"
get_url = f"https://dashscope.aliyuncs.com/api/v1/tasks/"
# get_url = f"https://poc-dashscope.aliyuncs.com/api/v1/tasks"
app = WanAnimateApp(url=url, get_url=get_url)
with gr.Blocks(title="Wan2.2-Animate 视频生成") as demo:
gr.HTML("""
<div style="padding: 2rem; text-align: center; max-width: 1200px; margin: 0 auto; font-family: Arial, sans-serif;">
<h1 style="font-size: 2.5rem; font-weight: bold; margin-bottom: 0.5rem; color: #333;">
Wan2.2-Animate: Unified Character Animation and Replacement with Holistic Replication
</h1>
<h3 style="font-size: 2.5rem; font-weight: bold; margin-bottom: 0.5rem; color: #333;">
Wan2.2-Animate: 统一的角色动画和视频人物替换模型
</h3>
<div style="font-size: 1.25rem; margin-bottom: 1.5rem; color: #555;">
Tongyi Lab, Alibaba
</div>
<div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 1rem; margin-bottom: 1rem;">
<!-- 第一行按钮 -->
<a href="https://arxiv.org/abs/2509.14055" target="_blank"
style="display: inline-flex; align-items: center; padding: 0.5rem 1rem; background-color: #f0f0f0; /* 浅灰色背景 */ color: #333; /* 深色文字 */ text-decoration: none; border-radius: 9999px; font-weight: 500; transition: background-color 0.3s;">
<span style="margin-right: 0.5rem;">📄</span> <!-- 使用文档图标 -->
<span>Paper</span>
</a>
<a href="https://github.com/Wan-Video/Wan2.2" target="_blank"
style="display: inline-flex; align-items: center; padding: 0.5rem 1rem; background-color: #f0f0f0; color: #333; text-decoration: none; border-radius: 9999px; font-weight: 500; transition: background-color 0.3s;">
<span style="margin-right: 0.5rem;">💻</span> <!-- 使用电脑图标 -->
<span>GitHub</span>
</a>
<a href="https://huggingface.co/Wan-AI/Wan2.2-Animate-14B" target="_blank"
style="display: inline-flex; align-items: center; padding: 0.5rem 1rem; background-color: #f0f0f0; color: #333; text-decoration: none; border-radius: 9999px; font-weight: 500; transition: background-color 0.3s;">
<span style="margin-right: 0.5rem;">🤗</span>
<span>HF Model</span>
</a>
<a href="https://www.modelscope.cn/models/Wan-AI/Wan2.2-Animate-14B" target="_blank"
style="display: inline-flex; align-items: center; padding: 0.5rem 1rem; background-color: #f0f0f0; color: #333; text-decoration: none; border-radius: 9999px; font-weight: 500; transition: background-color 0.3s;">
<span style="margin-right: 0.5rem;">🤖</span>
<span>MS Model</span>
</a>
</div>
<div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 1rem;">
<!-- 第二行按钮 -->
<a href="https://huggingface.co/spaces/Wan-AI/Wan2.2-Animate" target="_blank"
style="display: inline-flex; align-items: center; padding: 0.5rem 1rem; background-color: #f0f0f0; color: #333; text-decoration: none; border-radius: 9999px; font-weight: 500; transition: background-color 0.3s;">
<span style="margin-right: 0.5rem;">🤗</span>
<span>HF Space</span>
</a>
<a href="https://www.modelscope.cn/studios/Wan-AI/Wan2.2-Animate" target="_blank"
style="display: inline-flex; align-items: center; padding: 0.5rem 1rem; background-color: #f0f0f0; color: #333; text-decoration: none; border-radius: 9999px; font-weight: 500; transition: background-color 0.3s;">
<span style="margin-right: 0.5rem;">🤖</span>
<span>MS Studio</span>
</a>
</div>
</div>
""")
gr.HTML("""
<details>
<summary>‼️Usage (使用说明)</summary>
Wan-Animate supports two mode:
<ul>
<li>Move Mode: animate the character in input image with movements from the input video</li>
<li>Mix Mode: replace the character in input video with the character in input image</li>
</ul>
Wan-Animate 支持两种模式:
<ul>
<li>Move模式: 用输入视频中提取的动作,驱动输入图片中的角色</li>
<li>Mix模式: 用输入图片中的角色,替换输入视频中的角色</li>
</ul>
Currently, the following restrictions apply to inputs:
<ul> <li>Video file size: Less than 200MB</li>
<li>Video resolution: The shorter side must be greater than 200, and the longer side must be less than 2048</li>
<li>Video duration: 2s to 30s</li>
<li>Video aspect ratio: 1:3 to 3:1</li>
<li>Video formats: mp4, avi, mov</li>
<li>Image file size: Less than 5MB</li>
<li>Image resolution: The shorter side must be greater than 200, and the longer side must be less than 4096</li>
<li>Image formats: jpg, png, jpeg, webp, bmp</li> </ul>
当前,对于输入有以下的限制
<ul>
<li>视频文件大小: 小于 200MB</li>
<li>视频分辨率: 最小边大于 200, 最大边小于2048</li>
<li>视频时长: 2s ~ 30s </li>
<li>视频比例:1:3 ~ 3:1 </li>
<li>视频格式: mp4, avi, mov </li>
<li>图片文件大小: 小于5MB </li>
<li>图片分辨率:最小边大于200,最大边小于4096 </li>
<li>图片格式: jpg, png, jpeg, webp, bmp </li>
</ul>
<p> Currently, the inference quality has two variants. You can use our open-source code for more flexible configuration. </p>
<p>当前,推理质量有两个变种。 您可以使用我们的开源代码,来进行更灵活的设置。</p>
<ul>
<li> wan-pro: 25fps, 720p </li>
<li> wan-std: 15fps, 720p </li>
</ul>
</details>
""")
with gr.Row():
with gr.Column():
ref_img = gr.Image(
label="Reference Image(参考图像)",
type="filepath",
sources=["upload"],
)
video = gr.Video(
label="Template Video(模版视频)",
sources=["upload"],
)
with gr.Row():
model_id = gr.Dropdown(
label="Mode(模式)",
choices=["wan2.2-animate-move", "wan2.2-animate-mix"],
value="wan2.2-animate-move",
info=""
)
model = gr.Dropdown(
label="推理质量(Inference Quality)",
choices=["wan-pro", "wan-std"],
value="wan-pro",
)
run_button = gr.Button("Generate Video(生成视频)")
with gr.Column():
output_video = gr.Video(label="Output Video(输出视频)")
output_status = gr.Textbox(label="Status(状态)")
run_button.click(
fn=app.predict,
inputs=[
ref_img,
video,
model_id,
model,
],
outputs=[output_video, output_status],
)
example_data = [
['./examples/mov/1/1.jpeg', './examples/mov/1/1.mp4', 'wan2.2-animate-move', 'wan-pro'],
['./examples/mov/2/2.jpeg', './examples/mov/2/2.mp4', 'wan2.2-animate-move', 'wan-pro'],
['./examples/mix/1/1.jpeg', './examples/mix/1/1.mp4', 'wan2.2-animate-mix', 'wan-pro'],
['./examples/mix/2/2.jpeg', './examples/mix/2/2.mp4', 'wan2.2-animate-mix', 'wan-pro']
]
if example_data:
gr.Examples(
examples=example_data,
inputs=[ref_img, video, model_id, model],
outputs=[output_video, output_status],
fn=app.predict,
cache_examples="lazy",
)
demo.queue(default_concurrency_limit=100)
demo.launch(
server_name="0.0.0.0",
server_port=7860
)
if __name__ == "__main__":
start_app() |