guanwei1225 commited on
Commit
595f19f
·
verified ·
1 Parent(s): a6db245

Upload 7 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ assets/images/5.jpeg filter=lfs diff=lfs merge=lfs -text
37
+ assets/images/802f12c980c54aa18e6c3d75d3d6bafe1734588260122.png filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Wan2.1
3
+ emoji: 💻
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 5.17.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ short_description: 'Wan: Open and Advanced Large-Scale Video Generative Models'
12
+ ---
13
+
14
+ Online Demo: https://imagetovideoaifree.com/wanx21
15
+
app.py ADDED
@@ -0,0 +1,307 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_wan, 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_wan, 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_wan, 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_wan)
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_wan, 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_wan
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_wan, 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
+ # 检查image是否为URL字符串
74
+ if isinstance(image, str) and (image.startswith('http://') or image.startswith('https://')):
75
+ img_url = image
76
+ else:
77
+ img_url = image # 上传的文件路径
78
+
79
+ rsp = VideoSynthesis.async_call(model="wanx2.1-i2v-plus", prompt=prompt, seed=seed,
80
+ img_url=img_url, watermark_wanx=watermark_wan)
81
+ print(rsp)
82
+ task_id = rsp.output.task_id
83
+ status = False
84
+ return task_id, status, gr.Button(visible=False)
85
+ except Exception as e:
86
+ gr.Warning(f"Warning: {e}")
87
+ return "", None, gr.Button()
88
+
89
+ def get_result_with_task_id(task_id):
90
+ if task_id == "": return True, None
91
+ try:
92
+ rsp = VideoSynthesis.fetch(task = task_id)
93
+ print(rsp)
94
+ if rsp.output.task_status == "FAILED":
95
+ gr.Info(f"Warning: task running {rsp.output.task_status}")
96
+ status = True
97
+ video_url = None
98
+ else:
99
+ video_url = rsp.output.video_url
100
+ video_url = video_url if video_url != "" else None
101
+ status = video_url is not None
102
+ except:
103
+ video_url = None
104
+ status = False
105
+ return status, None if video_url=="" else video_url
106
+ # 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"
107
+ task_status = {}
108
+
109
+ def allow_task_num():
110
+ num = 0
111
+ for task_id in task_status:
112
+ if not task_status[task_id]["status"] and task_status[task_id]["time"] + 1800 > time.time():
113
+ num += 1
114
+ return num < LIMIT_RUNING_TASK
115
+ def clean_task_status():
116
+ # clean the task over 1800 seconds
117
+ for task_id in copy.deepcopy(task_status):
118
+ if task_id == "": continue
119
+ # finished task, keep 3600 seconds
120
+ if task_status[task_id]["status"]:
121
+ if task_status[task_id]["time"] + KEEP_SUCCESS_TASK < time.time():
122
+ task_status.pop(task_id)
123
+ else:
124
+ # clean the task over 3600 * 2 seconds
125
+ if task_status[task_id]["time"] + KEEP_RUNING_TASK < time.time():
126
+ task_status.pop(task_id)
127
+
128
+ def cost_time(task_id):
129
+ if task_id in task_status and not task_status[task_id]["status"]:
130
+ et = time.time() - task_status[task_id]["time"]
131
+ return f"{et:.2f}"
132
+ else:
133
+ return gr.Textbox()
134
+ def get_process_bar(task_id, status):
135
+ clean_task_status()
136
+ if task_id not in task_status:
137
+ task_status[task_id] = {
138
+ "value": 0 if not task_id == "" else 100,
139
+ "status": status if not task_id == "" else True,
140
+ "time": time.time(),
141
+ "url": None
142
+ }
143
+ if not task_status[task_id]["status"]:
144
+ # only when > 50% do check status
145
+ if task_status[task_id]["value"] >= 10 and task_status[task_id]["value"] % 5 == 0:
146
+ status, video_url = get_result_with_task_id(task_id)
147
+ else:
148
+ status, video_url = False, None
149
+ task_status[task_id]["status"] = status
150
+ task_status[task_id]["url"] = video_url
151
+ if task_status[task_id]["status"]:
152
+ task_status[task_id]["value"] = 100
153
+ else:
154
+ task_status[task_id]["value"] += 1
155
+ if task_status[task_id]["value"] >= 100 and not task_status[task_id]["status"]:
156
+ task_status[task_id]["value"] = 95
157
+ # print(task_id, task_status[task_id], task_status)
158
+ value = task_status[task_id]["value"]
159
+ return gr.Slider(label= f"({value}%)Generating" if value%2==1 else f"({value}%)Generating.....", value=value)
160
+
161
+
162
+ with gr.Blocks() as demo:
163
+
164
+ gr.HTML("""
165
+ <div style="text-align: center; font-size: 32px; font-weight: bold; margin-bottom: 20px;">
166
+ Wan2.1: Open and Advanced Large-Scale Video Generative Models
167
+ </div>
168
+ <div style="text-align: center;">
169
+ <a href="https://github.com/Wan-Video/Wan2.1">Code</a> |
170
+ <a href="https://huggingface.co/Wan-AI">Huggingface</a> |
171
+ <a href="https://modelscope.cn/organization/Wan-AI">Modelscope</a>
172
+ </div>
173
+
174
+ """)
175
+ task_id = gr.State(value="")
176
+ status = gr.State(value=False)
177
+ task = gr.State(value="t2v")
178
+ with gr.Row():
179
+ with gr.Column():
180
+ with gr.Row():
181
+ with gr.Tabs():
182
+ # Text to Video Tab
183
+ with gr.TabItem("Text to Video") as t2v_tab:
184
+ with gr.Row():
185
+ txt2vid_prompt = gr.Textbox(
186
+ label="Prompt",
187
+ placeholder="Describe the video you want to generate",
188
+ lines=19,
189
+ )
190
+ with gr.Row():
191
+ resolution = gr.Dropdown(
192
+ label="Resolution",
193
+ choices=["1280*720", "960*960", "720*1280", "1088*832", "832*1088"],
194
+ value="1280*720",
195
+ )
196
+ with gr.Row():
197
+ run_t2v_button = gr.Button("Generate Video")
198
+ # Image to Video Tab
199
+ with gr.TabItem("Image to Video") as i2v_tab:
200
+ with gr.Row():
201
+ with gr.Column():
202
+ img_input_type = gr.Radio(
203
+ ["上传图片", "图片URL"],
204
+ label="输入方式",
205
+ value="上传图片"
206
+ )
207
+
208
+ with gr.Group(visible=True) as upload_image_group:
209
+ img2vid_image = gr.Image(
210
+ type="filepath",
211
+ label="上传输入图片",
212
+ elem_id="image_upload",
213
+ )
214
+
215
+ with gr.Group(visible=False) as url_image_group:
216
+ img2vid_url = gr.Textbox(
217
+ label="图片URL",
218
+ placeholder="输入图片的URL地址",
219
+ value="",
220
+ )
221
+
222
+ img2vid_prompt = gr.Textbox(
223
+ label="Prompt",
224
+ placeholder="Describe the video you want to generate",
225
+ value="",
226
+ lines=5,
227
+ )
228
+ with gr.Row():
229
+ run_i2v_button = gr.Button("生成视频")
230
+ with gr.Column():
231
+ with gr.Row():
232
+ result_gallery = gr.Video(label='Generated Video',
233
+ interactive=False,
234
+ height=500)
235
+ with gr.Row():
236
+ watermark_wan = gr.Checkbox(label="Watermark", value=True, visible=True, container=False)
237
+ seed = gr.Number(label="Seed", value=-1, container=True)
238
+ cost_time = gr.Number(label="Cost Time(secs)", value=cost_time, interactive=False,
239
+ every=10, inputs=[task_id], container=True)
240
+ process_bar = gr.Slider(show_label=True, label="", value=get_process_bar, maximum=100,
241
+ interactive=True, every=10, inputs=[task_id, status], container=True)
242
+
243
+ fake_video = gr.Video(label='Examples', visible=False, interactive=False)
244
+ with gr.Row(visible=True) as t2v_eg:
245
+ gr.Examples(t2v_examples,
246
+ inputs=[txt2vid_prompt, result_gallery],
247
+ outputs=[result_gallery])
248
+
249
+ with gr.Row(visible=False) as i2v_eg:
250
+ gr.Examples(i2v_examples,
251
+ inputs=[img2vid_prompt, img2vid_image, result_gallery],
252
+ outputs=[result_gallery])
253
+
254
+ def process_change(task_id, task):
255
+ status = task_status[task_id]["status"]
256
+ if status:
257
+ video_url = task_status[task_id]["url"]
258
+ ret_t2v_btn = gr.Button(visible=True) if task == 't2v' else gr.Button()
259
+ ret_i2v_btn = gr.Button(visible=True) if task == 'i2v' else gr.Button()
260
+ return gr.Video(value=video_url), ret_t2v_btn, ret_i2v_btn
261
+ return gr.Video(value=None), gr.Button(), gr.Button()
262
+
263
+ process_bar.change(process_change, inputs=[task_id, task],
264
+ outputs=[result_gallery, run_t2v_button, run_i2v_button])
265
+
266
+
267
+ def switch_i2v_tab():
268
+ return gr.Row(visible=False), gr.Row(visible=True), "i2v"
269
+
270
+ def switch_t2v_tab():
271
+ return gr.Row(visible=True), gr.Row(visible=False), "t2v"
272
+
273
+ i2v_tab.select(switch_i2v_tab, outputs=[t2v_eg, i2v_eg, task])
274
+ t2v_tab.select(switch_t2v_tab, outputs=[t2v_eg, i2v_eg, task])
275
+
276
+ run_t2v_button.click(
277
+ fn=t2v_generation_async,
278
+ inputs=[txt2vid_prompt, resolution, watermark_wan, seed],
279
+ outputs=[task_id, status, run_t2v_button],
280
+ )
281
+
282
+ def switch_input_type(input_type):
283
+ if input_type == "上传图片":
284
+ return gr.Group(visible=True), gr.Group(visible=False)
285
+ else:
286
+ return gr.Group(visible=False), gr.Group(visible=True)
287
+
288
+ img_input_type.change(
289
+ fn=switch_input_type,
290
+ inputs=[img_input_type],
291
+ outputs=[upload_image_group, url_image_group],
292
+ )
293
+
294
+ def i2v_generation_async_wrapper(prompt, image, image_url, input_type, watermark_wan, seed=-1):
295
+ if input_type == "上传图片":
296
+ return i2v_generation_async(prompt, image, watermark_wan, seed)
297
+ else:
298
+ return i2v_generation_async(prompt, image_url, watermark_wan, seed)
299
+
300
+ run_i2v_button.click(
301
+ fn=i2v_generation_async_wrapper,
302
+ inputs=[img2vid_prompt, img2vid_image, img2vid_url, img_input_type, watermark_wan, seed],
303
+ outputs=[task_id, status, run_i2v_button],
304
+ )
305
+
306
+ demo.queue(max_size=10)
307
+ demo.launch()
assets/images/5.jpeg ADDED

Git LFS Details

  • SHA256: 2440662df7ddaf09483168e80327b522a02990dfa4d5461cd12ce5775722bc05
  • Pointer size: 131 Bytes
  • Size of remote file: 252 kB
assets/images/802f12c980c54aa18e6c3d75d3d6bafe1734588260122.png ADDED

Git LFS Details

  • SHA256: 068a94e4d18c1286d964d8a86feb286ae93d47527341f3ead96ab723e226527b
  • Pointer size: 132 Bytes
  • Size of remote file: 1.05 MB
examples.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ t2v_examples = [
2
+ [
3
+ "在台风肆虐的街头,一只身着亮黄雨衣、背负巨大天使翅膀的橙色小猫,驾驶踏板摩托车勇敢穿行。8K画质下,小猫眼神灵动,毛发细腻,雨衣与头盔色彩鲜明,与灰暗背景形成强烈对比。城市灯光映照在路面积水上,增添一抹柔和,小猫的微笑和闪烁的大眼睛仿佛能驱散所有阴霾,营造出温馨奇幻的氛围,让人仿佛进入了一个充满魔力的梦境。",
4
+ "./assets/videos/2.webm"
5
+ ],
6
+ [
7
+ "A little dog wearing headphones is riding a colorful scooter across a lush green field, with a basket filled with various fresh vegetables, resembling a moving pastoral landscape painting. Uniquely, a relaxed little cat is perched on the dog's back, and the two of them work together harmoniously, enjoying the thrill of the speedy ride. In the background, rolling mountains and a blue sky decorated with fluffy white clouds create a soothing and delightful scene. The dog's focused expression and the cat's comfortable posture together illustrate this extraordinary joy of riding.",
8
+ "./assets/videos/1.webm"
9
+ ],
10
+ ]
11
+
12
+ i2v_examples = [
13
+ [
14
+ "一只水母缓缓漂浮在海洋中。水母的伞形体态展现出柔和的蓝色和紫色,中央散发出温暖的金黄色光芒,仿佛在闪烁着神秘的光辉。长长的触手如丝带般轻盈地摇曳",
15
+ "./assets/images/5.jpeg",
16
+ "./assets/videos/3.webm"
17
+ ],
18
+ [
19
+ "The cat is moving around, and the flag in the background is waving along with the clouds.",
20
+ "./assets/images/802f12c980c54aa18e6c3d75d3d6bafe1734588260122.png",
21
+ "./assets/videos/4.webm"
22
+ ]
23
+ ]
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ dashscope>=0.10.0
2
+ gradio>=3.50.2
3
+ pillow>=9.0.0
4
+ numpy>=1.22.0