import copy import os import random os.system('pip install dashscope') import gradio as gr import dashscope from dashscope import VideoSynthesis from examples import t2v_examples, i2v_examples import time DASHSCOPE_API_KEY = os.getenv('DASHSCOPE_API_KEY') dashscope.api_key = DASHSCOPE_API_KEY KEEP_SUCCESS_TASK = 3600 * 10 KEEP_RUNING_TASK = 3600 * 2 # the total running task number in 1800 seconds LIMIT_RUNING_TASK = 10 def t2v_generation(prompt, resolution, watermark_wanx, seed = -1): seed = seed if seed >= 0 else random.randint(0, 2147483647) if not allow_task_num(): gr.Info(f"Warning: The number of running tasks is too large, please wait for a while.") return None, gr.Button(visible=True) try: rsp = VideoSynthesis.call(model="wanx2.1-t2v-plus", prompt=prompt, seed=seed, watermark_wanx=watermark_wanx, size=resolution) video_url = rsp.output.video_url return video_url, gr.Button(visible=True) except Exception as e: gr.Warning(f"Warning: {e}") return None, gr.Button(visible=True) def t2v_generation_async(prompt, size, watermark_wanx, seed = -1): print(seed) seed = seed if seed >= 0 else random.randint(0, 2147483647) print(seed) if not allow_task_num(): gr.Info(f"Warning: The number of running tasks is too large, please wait for a while.") return None, False, gr.Button(visible=True) try: rsp = VideoSynthesis.async_call(model="wanx2.1-t2v-plus", prompt=prompt, size=size, seed=seed, watermark_wanx=watermark_wanx) task_id = rsp.output.task_id status = False return task_id, status, gr.Button(visible=False) except Exception as e: gr.Warning(f"Warning: {e}") return None, True, gr.Button() def i2v_generation(prompt, image, watermark_wanx, seed = -1): seed = seed if seed >= 0 else random.randint(0, 2147483647) video_url = None try: rsp = VideoSynthesis.call(model="wanx2.1-i2v-plus", prompt=prompt, img_url= image, seed = seed, watermark_wanx=watermark_wanx ) video_url = rsp.output.video_url except Exception as e: gr.Warning(f"Warning: {e}") return video_url def i2v_generation_async(prompt, image, watermark_wanx, seed = -1): seed = seed if seed >= 0 else random.randint(0, 2147483647) if not allow_task_num(): gr.Info(f"Warning: The number of running tasks is too large, please wait for a while.") return "", None, gr.Button(visible=True) try: rsp = VideoSynthesis.async_call(model="wanx2.1-i2v-plus", prompt=prompt, seed=seed, img_url= image, watermark_wanx=watermark_wanx) print(rsp) task_id = rsp.output.task_id status = False return task_id, status, gr.Button(visible=False) except Exception as e: gr.Warning(f"Warning: {e}") return "", None, gr.Button() def get_result_with_task_id(task_id): if task_id == "": return True, None try: rsp = VideoSynthesis.fetch(task = task_id) print(rsp) if rsp.output.task_status == "FAILED": gr.Info(f"Warning: task running {rsp.output.task_status}") status = True video_url = None else: video_url = rsp.output.video_url video_url = video_url if video_url != "" else None status = video_url is not None except: video_url = None status = False return status, None if video_url=="" else video_url # return True, "https://dashscope-result-wlcb.oss-cn-wulanchabu.aliyuncs.com/1d/f8/20250220/e7d3f375/ccc590a2-7e90-4d92-84bc-22668db42979.mp4?Expires=1740137152&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=i3S3jA5FY6XYfvzZNHnvQiPzZSw%3D" task_status = {} def allow_task_num(): num = 0 for task_id in task_status: if not task_status[task_id]["status"] and task_status[task_id]["time"] + 1800 > time.time(): num += 1 return num < LIMIT_RUNING_TASK def clean_task_status(): # clean the task over 1800 seconds for task_id in copy.deepcopy(task_status): if task_id == "": continue # finished task, keep 3600 seconds if task_status[task_id]["status"]: if task_status[task_id]["time"] + KEEP_SUCCESS_TASK < time.time(): task_status.pop(task_id) else: # clean the task over 3600 * 2 seconds if task_status[task_id]["time"] + KEEP_RUNING_TASK < time.time(): task_status.pop(task_id) def cost_time(task_id): if task_id in task_status and not task_status[task_id]["status"]: et = time.time() - task_status[task_id]["time"] return f"{et:.2f}" else: return gr.Textbox() def get_process_bar(task_id, status): clean_task_status() if task_id not in task_status: task_status[task_id] = { "value": 0 if not task_id == "" else 100, "status": status if not task_id == "" else True, "time": time.time(), "url": None } if not task_status[task_id]["status"]: # only when > 50% do check status if task_status[task_id]["value"] >= 10 and task_status[task_id]["value"] % 5 == 0: status, video_url = get_result_with_task_id(task_id) else: status, video_url = False, None task_status[task_id]["status"] = status task_status[task_id]["url"] = video_url if task_status[task_id]["status"]: task_status[task_id]["value"] = 100 else: task_status[task_id]["value"] += 1 if task_status[task_id]["value"] >= 100 and not task_status[task_id]["status"]: task_status[task_id]["value"] = 95 # print(task_id, task_status[task_id], task_status) value = task_status[task_id]["value"] return gr.Slider(label= f"({value}%)Generating" if value%2==1 else f"({value}%)Generating.....", value=value) with gr.Blocks() as demo: gr.HTML("""