chaojiemao commited on
Commit
969c1d2
·
verified ·
1 Parent(s): d5ccfc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -39
app.py CHANGED
@@ -15,12 +15,33 @@ dashscope.api_key = DASHSCOPE_API_KEY
15
  KEEP_SUCCESS_TASK = 3600 * 10
16
  KEEP_RUNING_TASK = 3600 * 1
17
  # the total running task number in 1800 seconds
18
- LIMIT_RUNING_TASK = 4
 
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,
@@ -31,13 +52,16 @@ def t2v_generation(prompt, resolution, watermark_wan, seed = -1):
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,
@@ -46,17 +70,18 @@ def t2v_generation_async(prompt, size, watermark_wan, seed = -1):
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
@@ -64,26 +89,29 @@ def i2v_generation(prompt, image, watermark_wan, seed = -1):
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
  rsp = VideoSynthesis.async_call(model="wanx2.1-i2v-plus", prompt=prompt, seed=seed,
74
- img_url= image, watermark_wanx=watermark_wan)
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}")
@@ -93,12 +121,17 @@ def get_result_with_task_id(task_id):
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
@@ -108,7 +141,25 @@ def allow_task_num():
108
  num += 1
109
  if not task_status[task_id]["status"]:
110
  total_num += 1
111
- return num < LIMIT_RUNING_TASK or num < 10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  def clean_task_status():
113
  # clean the task over 1800 seconds
114
  for task_id in copy.deepcopy(task_status):
@@ -122,9 +173,10 @@ def clean_task_status():
122
  if task_status[task_id]["time"] + KEEP_RUNING_TASK < time.time():
123
  task_status.pop(task_id)
124
 
125
- def cost_time(task_id):
 
126
  if task_id in task_status and not task_status[task_id]["status"]:
127
- et = time.time() - task_status[task_id]["time"]
128
  return f"{et:.2f}"
129
  else:
130
  return gr.Textbox()
@@ -141,7 +193,7 @@ def get_process_bar(task_id, status):
141
  }
142
  if not task_status[task_id]["status"]:
143
  # only when > 50% do check status
144
- if task_status[task_id]["value"] >= 10 and task_status[task_id]["value"] % 5 == 0:
145
  status, video_url = get_result_with_task_id(task_id)
146
  else:
147
  status, video_url = False, None
@@ -150,16 +202,15 @@ def get_process_bar(task_id, status):
150
  if task_status[task_id]["status"]:
151
  task_status[task_id]["value"] = 100
152
  else:
153
- task_status[task_id]["value"] += 1
154
  if task_status[task_id]["value"] >= 100 and not task_status[task_id]["status"]:
155
  task_status[task_id]["value"] = 95
156
  # print(task_id, task_status[task_id], task_status)
157
  value = task_status[task_id]["value"]
158
- return gr.Slider(label= f"({value}%)Generating" if value%2==1 else f"({value}%)Generating.....", value=value)
159
 
160
 
161
  with gr.Blocks() as demo:
162
-
163
  gr.HTML("""
164
  <div style="text-align: center; font-size: 32px; font-weight: bold; margin-bottom: 20px;">
165
  Wan2.1: Open and Advanced Large-Scale Video Generative Models
@@ -169,7 +220,7 @@ with gr.Blocks() as demo:
169
  <a href="https://huggingface.co/Wan-AI">Huggingface</a> |
170
  <a href="https://modelscope.cn/organization/Wan-AI">Modelscope</a>
171
  </div>
172
-
173
  """)
174
  task_id = gr.State(value="")
175
  status = gr.State(value=False)
@@ -194,6 +245,7 @@ with gr.Blocks() as demo:
194
  )
195
  with gr.Row():
196
  run_t2v_button = gr.Button("Generate Video")
 
197
  # Image to Video Tab
198
  with gr.TabItem("Image to Video") as i2v_tab:
199
  with gr.Row():
@@ -211,18 +263,21 @@ with gr.Blocks() as demo:
211
  )
212
  with gr.Row():
213
  run_i2v_button = gr.Button("Generate Video")
 
214
  with gr.Column():
215
  with gr.Row():
216
  result_gallery = gr.Video(label='Generated Video',
217
  interactive=False,
218
  height=500)
219
  with gr.Row():
220
- watermark_wan = gr.Checkbox(label="Watermark", value=True, visible=True, container=False)
221
- seed = gr.Number(label="Seed", value=-1, container=True)
222
- cost_time = gr.Number(label="Cost Time(secs)", value=cost_time, interactive=False,
223
- every=10, inputs=[task_id], container=True)
224
- process_bar = gr.Slider(show_label=True, label="", value=get_process_bar, maximum=100,
225
- interactive=True, every=10, inputs=[task_id, status], container=True)
 
 
226
 
227
  fake_video = gr.Video(label='Examples', visible=False, interactive=False)
228
  with gr.Row(visible=True) as t2v_eg:
@@ -235,38 +290,77 @@ with gr.Blocks() as demo:
235
  inputs=[img2vid_prompt, img2vid_image, result_gallery],
236
  outputs=[result_gallery])
237
 
 
238
  def process_change(task_id, task):
239
  status = task_status[task_id]["status"]
240
  if status:
241
  video_url = task_status[task_id]["url"]
242
  ret_t2v_btn = gr.Button(visible=True) if task == 't2v' else gr.Button()
 
243
  ret_i2v_btn = gr.Button(visible=True) if task == 'i2v' else gr.Button()
244
- return gr.Video(value=video_url), ret_t2v_btn, ret_i2v_btn
245
- return gr.Video(value=None), gr.Button(), gr.Button()
 
 
246
 
247
  process_bar.change(process_change, inputs=[task_id, task],
248
- outputs=[result_gallery, run_t2v_button, run_i2v_button])
 
249
 
250
 
251
  def switch_i2v_tab():
252
  return gr.Row(visible=False), gr.Row(visible=True), "i2v"
253
 
 
254
  def switch_t2v_tab():
255
  return gr.Row(visible=True), gr.Row(visible=False), "t2v"
256
 
 
257
  i2v_tab.select(switch_i2v_tab, outputs=[t2v_eg, i2v_eg, task])
258
  t2v_tab.select(switch_t2v_tab, outputs=[t2v_eg, i2v_eg, task])
259
 
260
  run_t2v_button.click(
261
  fn=t2v_generation_async,
262
  inputs=[txt2vid_prompt, resolution, watermark_wan, seed],
263
- outputs=[task_id, status, run_t2v_button],
264
  )
265
 
266
  run_i2v_button.click(
267
  fn=i2v_generation_async,
268
  inputs=[img2vid_prompt, img2vid_image, watermark_wan, seed],
269
- outputs=[task_id, status, run_i2v_button],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  )
271
 
272
  demo.queue(max_size=10)
 
15
  KEEP_SUCCESS_TASK = 3600 * 10
16
  KEEP_RUNING_TASK = 3600 * 1
17
  # the total running task number in 1800 seconds
18
+ LIMIT_RUNING_TASK = 2
19
+ LIMIT_HISTORY_RUNING_TASK = 4
20
 
21
+ task_status = {}
22
+
23
+ total_task_info = {
24
+ "total_process_cost": 0,
25
+ "total_complete_task": 0,
26
+ "total_submit": 0,
27
+ "latest_1h_submit_status": {
28
+
29
+ }
30
+ }
31
+
32
+ def get_submit_code():
33
+ submit_code = random.randint(0, 2147483647)
34
+ #
35
+ for sub_c, sub_info in copy.deepcopy(total_task_info["latest_1h_submit_status"]).items():
36
+ if time.time() - sub_info > 3600:
37
+ total_task_info["latest_1h_submit_status"].pop(sub_c)
38
+ return submit_code
39
+ def t2v_generation(prompt, resolution, watermark_wan, seed=-1):
40
  seed = seed if seed >= 0 else random.randint(0, 2147483647)
41
+ total_task_info["latest_1h_submit_status"][get_submit_code()] = time.time()
42
+ total_task_info["total_submit"] += 1
43
  if not allow_task_num():
44
+ gr.Info(f"Warning: The number of running tasks is too large, the estimate waiting time is {get_waiting_time('-1')} s.")
45
  return None, gr.Button(visible=True)
46
  try:
47
  rsp = VideoSynthesis.call(model="wanx2.1-t2v-plus", prompt=prompt, seed=seed,
 
52
  gr.Warning(f"Warning: {e}")
53
  return None, gr.Button(visible=True)
54
 
55
+
56
+ def t2v_generation_async(prompt, size, watermark_wan, seed=-1):
57
  print(seed)
58
  seed = seed if seed >= 0 else random.randint(0, 2147483647)
59
+ total_task_info["latest_1h_submit_status"][get_submit_code()] = time.time()
60
+ total_task_info["total_submit"] += 1
61
  print(seed)
62
  if not allow_task_num():
63
+ gr.Info(f"Warning: The number of running tasks is too large, the estimate waiting time is {get_waiting_time('-1')} s.")
64
+ return None, False, gr.Button(visible=True), gr.Button(visible=False), gr.Slider(), gr.Slider()
65
  try:
66
  rsp = VideoSynthesis.async_call(model="wanx2.1-t2v-plus",
67
  prompt=prompt,
 
70
  watermark_wanx=watermark_wan)
71
  task_id = rsp.output.task_id
72
  status = False
73
+ return task_id, status, gr.Button(visible=False), gr.Button(visible=True), get_cost_time(task_id), get_waiting_time(task_id)
74
  except Exception as e:
75
  gr.Warning(f"Warning: {e}")
76
+ return None, True, gr.Button(), gr.Button(), gr.Slider(), gr.Slider()
77
 
78
+
79
+ def i2v_generation(prompt, image, watermark_wan, seed=-1):
80
  seed = seed if seed >= 0 else random.randint(0, 2147483647)
81
  video_url = None
82
  try:
83
+ rsp = VideoSynthesis.call(model="wanx2.1-i2v-plus", prompt=prompt, img_url=image,
84
+ seed=seed,
85
  watermark_wanx=watermark_wan
86
  )
87
  video_url = rsp.output.video_url
 
89
  gr.Warning(f"Warning: {e}")
90
  return video_url
91
 
92
+
93
+ def i2v_generation_async(prompt, image, watermark_wan, seed=-1):
94
  seed = seed if seed >= 0 else random.randint(0, 2147483647)
95
+ total_task_info["latest_1h_submit_status"][get_submit_code()] = time.time()
96
+ total_task_info["total_submit"] += 1
97
  if not allow_task_num():
98
+ gr.Info(f"Warning: The number of running tasks is too large, the estimate waiting time is {get_waiting_time('-1')} s.")
99
+ return "", None, gr.Button(visible=True), gr.Button(visible=False), gr.Slider(), gr.Slider()
100
  try:
101
  rsp = VideoSynthesis.async_call(model="wanx2.1-i2v-plus", prompt=prompt, seed=seed,
102
+ img_url=image, watermark_wanx=watermark_wan)
103
  print(rsp)
104
  task_id = rsp.output.task_id
105
  status = False
106
+ return task_id, status, gr.Button(visible=False), gr.Button(visible=True), gr.Button(visible=True), get_cost_time(task_id), get_waiting_time(task_id)
107
  except Exception as e:
108
  gr.Warning(f"Warning: {e}")
109
+ return "", None, gr.Button(), gr.Button(), gr.Slider(), gr.Slider()
110
 
111
  def get_result_with_task_id(task_id):
112
  if task_id == "": return True, None
113
  try:
114
+ rsp = VideoSynthesis.fetch(task=task_id)
115
  print(rsp)
116
  if rsp.output.task_status == "FAILED":
117
  gr.Info(f"Warning: task running {rsp.output.task_status}")
 
121
  video_url = rsp.output.video_url
122
  video_url = video_url if video_url != "" else None
123
  status = video_url is not None
124
+ if status:
125
+ total_task_info["total_complete_task"] += 1
126
+ total_task_info["total_process_cost"] += time.time() - task_status[task_id]["time"]
127
+ print(total_task_info["total_complete_task"], total_task_info["total_process_cost"])
128
  except:
129
  video_url = None
130
  status = False
131
+ return status, None if video_url == "" else video_url
132
  # 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"
133
+
134
+
135
 
136
  def allow_task_num():
137
  num = 0
 
141
  num += 1
142
  if not task_status[task_id]["status"]:
143
  total_num += 1
144
+ return num < LIMIT_RUNING_TASK or total_num < LIMIT_HISTORY_RUNING_TASK
145
+
146
+ def get_waiting_time(task_id):
147
+ # if the num of running task < Limit
148
+ # waiting time = num * 480s
149
+ # task_id not in task_status, return a large number
150
+ # prediction the waiting time
151
+ # avg_cost * latest submit time
152
+ num = 0
153
+ for task_id in task_status:
154
+ if not task_status[task_id]["status"]:
155
+ num += 1
156
+ latest_submit_tasks = len(total_task_info["latest_1h_submit_status"])
157
+ print("latest submit tasks", latest_submit_tasks)
158
+ if task_id in task_status:
159
+ return int(640 - (time.time() - task_status[task_id]["time"]))
160
+ else:
161
+ return int(latest_submit_tasks * (total_task_info["total_process_cost"]/(total_task_info["total_complete_task"]+1)))
162
+
163
  def clean_task_status():
164
  # clean the task over 1800 seconds
165
  for task_id in copy.deepcopy(task_status):
 
173
  if task_status[task_id]["time"] + KEEP_RUNING_TASK < time.time():
174
  task_status.pop(task_id)
175
 
176
+
177
+ def get_cost_time(task_id):
178
  if task_id in task_status and not task_status[task_id]["status"]:
179
+ et = int(time.time() - task_status[task_id]["time"])
180
  return f"{et:.2f}"
181
  else:
182
  return gr.Textbox()
 
193
  }
194
  if not task_status[task_id]["status"]:
195
  # only when > 50% do check status
196
+ if task_status[task_id]["value"] >= 5 and task_status[task_id]["value"] % 5 == 0:
197
  status, video_url = get_result_with_task_id(task_id)
198
  else:
199
  status, video_url = False, None
 
202
  if task_status[task_id]["status"]:
203
  task_status[task_id]["value"] = 100
204
  else:
205
+ task_status[task_id]["value"] += 5
206
  if task_status[task_id]["value"] >= 100 and not task_status[task_id]["status"]:
207
  task_status[task_id]["value"] = 95
208
  # print(task_id, task_status[task_id], task_status)
209
  value = task_status[task_id]["value"]
210
+ return gr.Slider(label=f"({value}%)Generating" if value % 2 == 1 else f"({value}%)Generating.....", value=value)
211
 
212
 
213
  with gr.Blocks() as demo:
 
214
  gr.HTML("""
215
  <div style="text-align: center; font-size: 32px; font-weight: bold; margin-bottom: 20px;">
216
  Wan2.1: Open and Advanced Large-Scale Video Generative Models
 
220
  <a href="https://huggingface.co/Wan-AI">Huggingface</a> |
221
  <a href="https://modelscope.cn/organization/Wan-AI">Modelscope</a>
222
  </div>
223
+
224
  """)
225
  task_id = gr.State(value="")
226
  status = gr.State(value=False)
 
245
  )
246
  with gr.Row():
247
  run_t2v_button = gr.Button("Generate Video")
248
+ t2v_refresh_status = gr.Button("Refresh Generating Status", visible=False)
249
  # Image to Video Tab
250
  with gr.TabItem("Image to Video") as i2v_tab:
251
  with gr.Row():
 
263
  )
264
  with gr.Row():
265
  run_i2v_button = gr.Button("Generate Video")
266
+ i2v_refresh_status = gr.Button("Refresh Generating Status", visible=False)
267
  with gr.Column():
268
  with gr.Row():
269
  result_gallery = gr.Video(label='Generated Video',
270
  interactive=False,
271
  height=500)
272
  with gr.Row():
273
+ watermark_wan = gr.Checkbox(label="Watermark", value=True, visible=True, container=False)
274
+ seed = gr.Number(label="Seed", value=-1, container=True)
275
+ cost_time = gr.Number(label="Cost Time(secs)", value=get_cost_time, interactive=False,
276
+ every=120, inputs=[task_id], container=True)
277
+ waiting_time = gr.Number(label="Estimated Waiting Time(secs)", value=get_waiting_time, interactive=False,
278
+ every=120, inputs=[task_id], container=True)
279
+ process_bar = gr.Slider(show_label=True, label="", value=get_process_bar, maximum=100,
280
+ interactive=True, every=120, inputs=[task_id, status], container=True)
281
 
282
  fake_video = gr.Video(label='Examples', visible=False, interactive=False)
283
  with gr.Row(visible=True) as t2v_eg:
 
290
  inputs=[img2vid_prompt, img2vid_image, result_gallery],
291
  outputs=[result_gallery])
292
 
293
+
294
  def process_change(task_id, task):
295
  status = task_status[task_id]["status"]
296
  if status:
297
  video_url = task_status[task_id]["url"]
298
  ret_t2v_btn = gr.Button(visible=True) if task == 't2v' else gr.Button()
299
+ ret_t2v_status_btn = gr.Button(visible=False) if task == 't2v' else gr.Button()
300
  ret_i2v_btn = gr.Button(visible=True) if task == 'i2v' else gr.Button()
301
+ ret_i2v_status_btn = gr.Button(visible=False) if task == 'i2v' else gr.Button()
302
+ return gr.Video(value=video_url), ret_t2v_btn, ret_i2v_btn, ret_t2v_status_btn, ret_i2v_status_btn
303
+ return gr.Video(value=None), gr.Button(), gr.Button(), gr.Button(), gr.Button()
304
+
305
 
306
  process_bar.change(process_change, inputs=[task_id, task],
307
+ outputs=[result_gallery, run_t2v_button, run_i2v_button,
308
+ t2v_refresh_status, i2v_refresh_status])
309
 
310
 
311
  def switch_i2v_tab():
312
  return gr.Row(visible=False), gr.Row(visible=True), "i2v"
313
 
314
+
315
  def switch_t2v_tab():
316
  return gr.Row(visible=True), gr.Row(visible=False), "t2v"
317
 
318
+
319
  i2v_tab.select(switch_i2v_tab, outputs=[t2v_eg, i2v_eg, task])
320
  t2v_tab.select(switch_t2v_tab, outputs=[t2v_eg, i2v_eg, task])
321
 
322
  run_t2v_button.click(
323
  fn=t2v_generation_async,
324
  inputs=[txt2vid_prompt, resolution, watermark_wan, seed],
325
+ outputs=[task_id, status, run_t2v_button, t2v_refresh_status],
326
  )
327
 
328
  run_i2v_button.click(
329
  fn=i2v_generation_async,
330
  inputs=[img2vid_prompt, img2vid_image, watermark_wan, seed],
331
+ outputs=[task_id, status, run_i2v_button, i2v_refresh_status],
332
+ )
333
+
334
+ def status_refresh(task_id, task, status):
335
+ if task_id in task_status and not task_status[task_id]["status"]:
336
+ cost_time = int(time.time() - task_status[task_id]["time"])
337
+ else:
338
+ cost_time = 0
339
+ status, video_url = get_result_with_task_id(task_id)
340
+ task_status[task_id]["status"] = status
341
+ task_status[task_id]["url"] = video_url
342
+ waiting_time = get_waiting_time(task_id)
343
+ value = task_status.get(task_id, {"value": 100})["value"]
344
+ value = min(value, int(cost_time*100/waiting_time))
345
+ process_bar = gr.Slider(label=f"({value}%)Generating" if value % 2 == 1 else f"({value}%)Generating.....", value=value)
346
+ process_change_ret = process_change(task_id, task)
347
+ return *process_change_ret, cost_time, waiting_time, process_bar
348
+
349
+
350
+ t2v_refresh_status.click(
351
+ fn=status_refresh,
352
+ inputs=[task_id, task, status],
353
+ outputs=[result_gallery, run_t2v_button, run_i2v_button,
354
+ t2v_refresh_status, i2v_refresh_status,
355
+ cost_time, waiting_time, process_bar]
356
+ )
357
+
358
+ i2v_refresh_status.click(
359
+ fn=status_refresh,
360
+ inputs=[task_id, task, status],
361
+ outputs=[result_gallery, run_t2v_button, run_i2v_button,
362
+ t2v_refresh_status, i2v_refresh_status,
363
+ cost_time, waiting_time, process_bar]
364
  )
365
 
366
  demo.queue(max_size=10)