# app.py import os import oss2 import sys import uuid import shutil import time import gradio as gr import requests os.system("pip install dashscope") 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 WanS2VApp: def __init__(self): pass def predict( self, ref_img, audio, resolution="480P", style="speech", ): # Upload files to OSS if needed and get URLs _, image_url = check_and_upload_local("wan2.2-s2v", ref_img, DASHSCOPE_API_KEY) _, audio_url = check_and_upload_local("wan2.2-s2v", audio, DASHSCOPE_API_KEY) # Prepare the request payload payload = { "model": "wan2.2-s2v", "input": { "image_url": image_url, "audio_url": audio_url }, "parameters": { "style": style, "resolution": resolution, } } # 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 = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis/" 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"https://dashscope.aliyuncs.com/api/v1/tasks/{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 elif task_status == "FAILED": # Task failed, raise an exception with error message error_msg = result.get("output", {}).get("message", "Unknown error") raise Exception(f"Task failed: {error_msg}") 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-S2V 视频生成工具") args = parser.parse_args() app = WanS2VApp() with gr.Blocks(title="Wan2.2-S2V 视频生成") as demo: # gr.Markdown("# Wan2.2-S2V 视频生成工具") gr.HTML("""