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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
|
5 |
+
os.system('pip install dashscope')
|
6 |
+
import gradio as gr
|
7 |
+
import dashscope
|
8 |
+
from dashscope import VideoSynthesis
|
9 |
+
from examples import t2v_examples, i2v_examples
|
10 |
+
import time
|
11 |
+
|
12 |
+
DASHSCOPE_API_KEY = os.getenv('DASHSCOPE_API_KEY')
|
13 |
+
dashscope.api_key = DASHSCOPE_API_KEY
|
14 |
+
|
15 |
+
KEEP_SUCCESS_TASK = 3600 * 10
|
16 |
+
KEEP_RUNING_TASK = 3600 * 2
|
17 |
+
# the total running task number in 1800 seconds
|
18 |
+
LIMIT_RUNING_TASK = 10
|
19 |
+
|
20 |
+
def t2v_generation(prompt, resolution, watermark_wanx, seed = -1):
|
21 |
+
seed = seed if seed >= 0 else random.randint(0, 2147483647)
|
22 |
+
if not allow_task_num():
|
23 |
+
gr.Info(f"Warning: The number of running tasks is too large, please wait for a while.")
|
24 |
+
return None, gr.Button(visible=True)
|
25 |
+
try:
|
26 |
+
rsp = VideoSynthesis.call(model="wanx2.1-t2v-plus", prompt=prompt, seed=seed,
|
27 |
+
watermark_wanx=watermark_wanx, size=resolution)
|
28 |
+
video_url = rsp.output.video_url
|
29 |
+
return video_url, gr.Button(visible=True)
|
30 |
+
except Exception as e:
|
31 |
+
gr.Warning(f"Warning: {e}")
|
32 |
+
return None, gr.Button(visible=True)
|
33 |
+
|
34 |
+
def t2v_generation_async(prompt, size, watermark_wanx, seed = -1):
|
35 |
+
print(seed)
|
36 |
+
seed = seed if seed >= 0 else random.randint(0, 2147483647)
|
37 |
+
print(seed)
|
38 |
+
if not allow_task_num():
|
39 |
+
gr.Info(f"Warning: The number of running tasks is too large, please wait for a while.")
|
40 |
+
return None, False, gr.Button(visible=True)
|
41 |
+
try:
|
42 |
+
rsp = VideoSynthesis.async_call(model="wanx2.1-t2v-plus",
|
43 |
+
prompt=prompt,
|
44 |
+
size=size,
|
45 |
+
seed=seed,
|
46 |
+
watermark_wanx=watermark_wanx)
|
47 |
+
task_id = rsp.output.task_id
|
48 |
+
status = False
|
49 |
+
return task_id, status, gr.Button(visible=False)
|
50 |
+
except Exception as e:
|
51 |
+
gr.Warning(f"Warning: {e}")
|
52 |
+
return None, True, gr.Button()
|
53 |
+
|
54 |
+
def i2v_generation(prompt, image, watermark_wanx, seed = -1):
|
55 |
+
seed = seed if seed >= 0 else random.randint(0, 2147483647)
|
56 |
+
video_url = None
|
57 |
+
try:
|
58 |
+
rsp = VideoSynthesis.call(model="wanx2.1-i2v-plus", prompt=prompt, img_url= image,
|
59 |
+
seed = seed,
|
60 |
+
watermark_wanx=watermark_wanx
|
61 |
+
)
|
62 |
+
video_url = rsp.output.video_url
|
63 |
+
except Exception as e:
|
64 |
+
gr.Warning(f"Warning: {e}")
|
65 |
+
return video_url
|
66 |
+
|
67 |
+
def i2v_generation_async(prompt, image, watermark_wanx, seed = -1):
|
68 |
+
seed = seed if seed >= 0 else random.randint(0, 2147483647)
|
69 |
+
if not allow_task_num():
|
70 |
+
gr.Info(f"Warning: The number of running tasks is too large, please wait for a while.")
|
71 |
+
return "", None, gr.Button(visible=True)
|
72 |
+
try:
|
73 |
+
rsp = VideoSynthesis.async_call(model="wanx2.1-i2v-plus", prompt=prompt, seed=seed,
|
74 |
+
img_url= image, watermark_wanx=watermark_wanx)
|
75 |
+
print(rsp)
|
76 |
+
task_id = rsp.output.task_id
|
77 |
+
status = False
|
78 |
+
return task_id, status, gr.Button(visible=False)
|
79 |
+
except Exception as e:
|
80 |
+
gr.Warning(f"Warning: {e}")
|
81 |
+
return "", None, gr.Button()
|
82 |
+
|
83 |
+
def get_result_with_task_id(task_id):
|
84 |
+
if task_id == "": return True, None
|
85 |
+
try:
|
86 |
+
rsp = VideoSynthesis.fetch(task = task_id)
|
87 |
+
print(rsp)
|
88 |
+
if rsp.output.task_status == "FAILED":
|
89 |
+
gr.Info(f"Warning: task running {rsp.output.task_status}")
|
90 |
+
status = True
|
91 |
+
video_url = None
|
92 |
+
else:
|
93 |
+
video_url = rsp.output.video_url
|
94 |
+
video_url = video_url if video_url != "" else None
|
95 |
+
status = video_url is not None
|
96 |
+
except:
|
97 |
+
video_url = None
|
98 |
+
status = False
|
99 |
+
return status, None if video_url=="" else video_url
|
100 |
+
# 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"
|
101 |
+
task_status = {}
|
102 |
+
|
103 |
+
def allow_task_num():
|
104 |
+
num = 0
|
105 |
+
for task_id in task_status:
|
106 |
+
if not task_status[task_id]["status"] and task_status[task_id]["time"] + 1800 > time.time():
|
107 |
+
num += 1
|
108 |
+
return num < LIMIT_RUNING_TASK
|
109 |
+
def clean_task_status():
|
110 |
+
# clean the task over 1800 seconds
|
111 |
+
for task_id in copy.deepcopy(task_status):
|
112 |
+
if task_id == "": continue
|
113 |
+
# finished task, keep 3600 seconds
|
114 |
+
if task_status[task_id]["status"]:
|
115 |
+
if task_status[task_id]["time"] + KEEP_SUCCESS_TASK < time.time():
|
116 |
+
task_status.pop(task_id)
|
117 |
+
else:
|
118 |
+
# clean the task over 3600 * 2 seconds
|
119 |
+
if task_status[task_id]["time"] + KEEP_RUNING_TASK < time.time():
|
120 |
+
task_status.pop(task_id)
|
121 |
+
|
122 |
+
def cost_time(task_id):
|
123 |
+
if task_id in task_status and not task_status[task_id]["status"]:
|
124 |
+
et = time.time() - task_status[task_id]["time"]
|
125 |
+
return f"{et:.2f}"
|
126 |
+
else:
|
127 |
+
return gr.Textbox()
|
128 |
+
def get_process_bar(task_id, status):
|
129 |
+
clean_task_status()
|
130 |
+
if task_id not in task_status:
|
131 |
+
task_status[task_id] = {
|
132 |
+
"value": 0 if not task_id == "" else 100,
|
133 |
+
"status": status if not task_id == "" else True,
|
134 |
+
"time": time.time(),
|
135 |
+
"url": None
|
136 |
+
}
|
137 |
+
if not task_status[task_id]["status"]:
|
138 |
+
# only when > 50% do check status
|
139 |
+
if task_status[task_id]["value"] >= 10 and task_status[task_id]["value"] % 5 == 0:
|
140 |
+
status, video_url = get_result_with_task_id(task_id)
|
141 |
+
else:
|
142 |
+
status, video_url = False, None
|
143 |
+
task_status[task_id]["status"] = status
|
144 |
+
task_status[task_id]["url"] = video_url
|
145 |
+
if task_status[task_id]["status"]:
|
146 |
+
task_status[task_id]["value"] = 100
|
147 |
+
else:
|
148 |
+
task_status[task_id]["value"] += 1
|
149 |
+
if task_status[task_id]["value"] >= 100 and not task_status[task_id]["status"]:
|
150 |
+
task_status[task_id]["value"] = 95
|
151 |
+
# print(task_id, task_status[task_id], task_status)
|
152 |
+
value = task_status[task_id]["value"]
|
153 |
+
return gr.Slider(label= f"({value}%)Generating" if value%2==1 else f"({value}%)Generating.....", value=value)
|
154 |
+
|
155 |
+
|
156 |
+
with gr.Blocks() as demo:
|
157 |
+
gr.Markdown("""
|
158 |
+
<div style="text-align: center; font-size: 32px; font-weight: bold; margin-bottom: 20px;">
|
159 |
+
WanX (Tongyi Wanxiang)
|
160 |
+
</div>
|
161 |
+
<div style="text-align: center; font-size: 16px; font-weight: normal; margin-bottom: 20px;">
|
162 |
+
The latest powerful text to video generation model developed by the WanX Team from Tongyi Lab, Alibaba Group.
|
163 |
+
</div>
|
164 |
+
<div style="text-align: center;">
|
165 |
+
<a href="https://www.youtube.com/watch?v=bq4nAVbYQQU">YouTube Videos</a> |
|
166 |
+
<a href="https://tongyi.aliyun.com/wanxiang/videoCreation">WanX WEB</a>
|
167 |
+
</div>
|
168 |
+
""")
|
169 |
+
task_id = gr.State(value="")
|
170 |
+
status = gr.State(value=False)
|
171 |
+
task = gr.State(value="t2v")
|
172 |
+
with gr.Row():
|
173 |
+
with gr.Column():
|
174 |
+
with gr.Row():
|
175 |
+
with gr.Tabs():
|
176 |
+
# Text to Video Tab
|
177 |
+
with gr.TabItem("Text to Video") as t2v_tab:
|
178 |
+
with gr.Row():
|
179 |
+
txt2vid_prompt = gr.Textbox(
|
180 |
+
label="Prompt",
|
181 |
+
placeholder="Describe the video you want to generate",
|
182 |
+
lines=19,
|
183 |
+
)
|
184 |
+
with gr.Row():
|
185 |
+
resolution = gr.Dropdown(
|
186 |
+
label="Resolution",
|
187 |
+
choices=["1280*720", "960*960", "720*1280", "1088*832", "832*1088"],
|
188 |
+
value="1280*720",
|
189 |
+
)
|
190 |
+
with gr.Row():
|
191 |
+
run_t2v_button = gr.Button("Generate Video")
|
192 |
+
# Image to Video Tab
|
193 |
+
with gr.TabItem("Image to Video") as i2v_tab:
|
194 |
+
with gr.Row():
|
195 |
+
with gr.Column():
|
196 |
+
img2vid_image = gr.Image(
|
197 |
+
type="filepath",
|
198 |
+
label="Upload Input Image",
|
199 |
+
elem_id="image_upload",
|
200 |
+
)
|
201 |
+
img2vid_prompt = gr.Textbox(
|
202 |
+
label="Prompt",
|
203 |
+
placeholder="Describe the video you want to generate",
|
204 |
+
value="",
|
205 |
+
lines=5,
|
206 |
+
)
|
207 |
+
with gr.Row():
|
208 |
+
run_i2v_button = gr.Button("Generate Video")
|
209 |
+
with gr.Column():
|
210 |
+
with gr.Row():
|
211 |
+
result_gallery = gr.Video(label='WanX Generated Video',
|
212 |
+
interactive=False,
|
213 |
+
height=500)
|
214 |
+
with gr.Row():
|
215 |
+
watermark_wanx = gr.Checkbox(label="Watermark", value=True, container=False)
|
216 |
+
seed = gr.Number(label="Seed", value=-1, container=True)
|
217 |
+
cost_time = gr.Number(label="Cost Time(secs)", value=cost_time, interactive=False,
|
218 |
+
every=2, inputs=[task_id], container=True)
|
219 |
+
process_bar = gr.Slider(show_label=True, label="", value=get_process_bar, maximum=100,
|
220 |
+
interactive=True, every=3, inputs=[task_id, status], container=True)
|
221 |
+
|
222 |
+
fake_video = gr.Video(label='WanX Examples', visible=False, interactive=False)
|
223 |
+
with gr.Row(visible=True) as t2v_eg:
|
224 |
+
gr.Examples(t2v_examples,
|
225 |
+
inputs=[txt2vid_prompt, result_gallery],
|
226 |
+
outputs=[result_gallery])
|
227 |
+
|
228 |
+
with gr.Row(visible=False) as i2v_eg:
|
229 |
+
gr.Examples(i2v_examples,
|
230 |
+
inputs=[img2vid_prompt, img2vid_image, result_gallery],
|
231 |
+
outputs=[result_gallery])
|
232 |
+
|
233 |
+
def process_change(task_id, task):
|
234 |
+
status = task_status[task_id]["status"]
|
235 |
+
if status:
|
236 |
+
video_url = task_status[task_id]["url"]
|
237 |
+
ret_t2v_btn = gr.Button(visible=True) if task == 't2v' else gr.Button()
|
238 |
+
ret_i2v_btn = gr.Button(visible=True) if task == 'i2v' else gr.Button()
|
239 |
+
return gr.Video(value=video_url), ret_t2v_btn, ret_i2v_btn
|
240 |
+
return gr.Video(value=None), gr.Button(), gr.Button()
|
241 |
+
|
242 |
+
process_bar.change(process_change, inputs=[task_id, task],
|
243 |
+
outputs=[result_gallery, run_t2v_button, run_i2v_button])
|
244 |
+
|
245 |
+
|
246 |
+
def switch_i2v_tab():
|
247 |
+
return gr.Row(visible=False), gr.Row(visible=True), "i2v"
|
248 |
+
|
249 |
+
def switch_t2v_tab():
|
250 |
+
return gr.Row(visible=True), gr.Row(visible=False), "t2v"
|
251 |
+
|
252 |
+
i2v_tab.select(switch_i2v_tab, outputs=[t2v_eg, i2v_eg, task])
|
253 |
+
t2v_tab.select(switch_t2v_tab, outputs=[t2v_eg, i2v_eg, task])
|
254 |
+
|
255 |
+
run_t2v_button.click(
|
256 |
+
fn=t2v_generation_async,
|
257 |
+
inputs=[txt2vid_prompt, resolution, watermark_wanx, seed],
|
258 |
+
outputs=[task_id, status, run_t2v_button],
|
259 |
+
)
|
260 |
+
|
261 |
+
run_i2v_button.click(
|
262 |
+
fn=i2v_generation_async,
|
263 |
+
inputs=[img2vid_prompt, img2vid_image, watermark_wanx, seed],
|
264 |
+
outputs=[task_id, status, run_i2v_button],
|
265 |
+
)
|
266 |
+
|
267 |
+
demo.queue(max_size=10)
|
268 |
+
demo.launch()
|