chaojiemao commited on
Commit
8419428
·
verified ·
1 Parent(s): 21c2b29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -33
app.py CHANGED
@@ -15,7 +15,8 @@ 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
  task_status = {}
21
 
@@ -32,7 +33,7 @@ def get_submit_code():
32
  submit_code = random.randint(0, 2147483647)
33
  #
34
  for sub_c, sub_info in copy.deepcopy(total_task_info["latest_1h_submit_status"]).items():
35
- if time.time() - sub_info["time"] > 3600:
36
  total_task_info["latest_1h_submit_status"].pop(sub_c)
37
  return submit_code
38
  def t2v_generation(prompt, resolution, watermark_wan, seed=-1):
@@ -40,7 +41,7 @@ def t2v_generation(prompt, resolution, watermark_wan, seed=-1):
40
  total_task_info["latest_1h_submit_status"][get_submit_code()] = time.time()
41
  total_task_info["total_submit"] += 1
42
  if not allow_task_num():
43
- gr.Info(f"Warning: The number of running tasks is too large, please wait for a while.")
44
  return None, gr.Button(visible=True)
45
  try:
46
  rsp = VideoSynthesis.call(model="wanx2.1-t2v-plus", prompt=prompt, seed=seed,
@@ -55,11 +56,12 @@ def t2v_generation(prompt, resolution, watermark_wan, seed=-1):
55
  def t2v_generation_async(prompt, size, watermark_wan, seed=-1):
56
  print(seed)
57
  seed = seed if seed >= 0 else random.randint(0, 2147483647)
 
58
  total_task_info["total_submit"] += 1
59
  print(seed)
60
  if not allow_task_num():
61
- gr.Info(f"Warning: The number of running tasks is too large, the estimate waiting time is {waiting_time('-1')} s.")
62
- return None, False, gr.Button(visible=True), gr.Button(visible=False)
63
  try:
64
  rsp = VideoSynthesis.async_call(model="wanx2.1-t2v-plus",
65
  prompt=prompt,
@@ -68,10 +70,10 @@ def t2v_generation_async(prompt, size, watermark_wan, seed=-1):
68
  watermark_wanx=watermark_wan)
69
  task_id = rsp.output.task_id
70
  status = False
71
- return task_id, status, gr.Button(visible=False), gr.Button(visible=True)
72
  except Exception as e:
73
  gr.Warning(f"Warning: {e}")
74
- return None, True, gr.Button(), gr.Button()
75
 
76
 
77
  def i2v_generation(prompt, image, watermark_wan, seed=-1):
@@ -90,21 +92,21 @@ def i2v_generation(prompt, image, watermark_wan, seed=-1):
90
 
91
  def i2v_generation_async(prompt, image, watermark_wan, seed=-1):
92
  seed = seed if seed >= 0 else random.randint(0, 2147483647)
 
93
  total_task_info["total_submit"] += 1
94
  if not allow_task_num():
95
- gr.Info(f"Warning: The number of running tasks is too large, the estimate waiting time is {waiting_time('-1')} s.")
96
- return "", None, gr.Button(visible=True), gr.Button(visible=False)
97
  try:
98
  rsp = VideoSynthesis.async_call(model="wanx2.1-i2v-plus", prompt=prompt, seed=seed,
99
  img_url=image, watermark_wanx=watermark_wan)
100
  print(rsp)
101
  task_id = rsp.output.task_id
102
  status = False
103
- return task_id, status, gr.Button(visible=False), gr.Button(visible=True)
104
  except Exception as e:
105
  gr.Warning(f"Warning: {e}")
106
- return "", None, gr.Button(), gr.Button()
107
-
108
 
109
  def get_result_with_task_id(task_id):
110
  if task_id == "": return True, None
@@ -122,7 +124,7 @@ def get_result_with_task_id(task_id):
122
  if status:
123
  total_task_info["total_complete_task"] += 1
124
  total_task_info["total_process_cost"] += time.time() - task_status[task_id]["time"]
125
- print(total_task_info)
126
  except:
127
  video_url = None
128
  status = False
@@ -139,7 +141,7 @@ def allow_task_num():
139
  num += 1
140
  if not task_status[task_id]["status"]:
141
  total_num += 1
142
- return num < LIMIT_RUNING_TASK or num < 10
143
 
144
  def get_waiting_time(task_id):
145
  # if the num of running task < Limit
@@ -154,9 +156,13 @@ def get_waiting_time(task_id):
154
  latest_submit_tasks = len(total_task_info["latest_1h_submit_status"])
155
  print("latest submit tasks", latest_submit_tasks)
156
  if task_id in task_status:
157
- return int(num * 480 - (time.time() - task_status[task_id]["time"]))
158
  else:
159
- return int(latest_submit_tasks * (total_task_info["total_process_cost"]/total_task_info["total_complete_task"]))
 
 
 
 
160
 
161
  def clean_task_status():
162
  # clean the task over 1800 seconds
@@ -174,13 +180,18 @@ def clean_task_status():
174
 
175
  def get_cost_time(task_id):
176
  if task_id in task_status and not task_status[task_id]["status"]:
177
- et = time.time() - task_status[task_id]["time"]
178
  return f"{et:.2f}"
179
  else:
180
  return gr.Textbox()
181
 
 
 
 
182
 
183
- def get_process_bar(task_id, status):
 
 
184
  clean_task_status()
185
  if task_id not in task_status:
186
  task_status[task_id] = {
@@ -200,7 +211,7 @@ def get_process_bar(task_id, status):
200
  if task_status[task_id]["status"]:
201
  task_status[task_id]["value"] = 100
202
  else:
203
- task_status[task_id]["value"] += 1
204
  if task_status[task_id]["value"] >= 100 and not task_status[task_id]["status"]:
205
  task_status[task_id]["value"] = 95
206
  # print(task_id, task_status[task_id], task_status)
@@ -220,7 +231,8 @@ with gr.Blocks() as demo:
220
  </div>
221
 
222
  """)
223
- task_id = gr.State(value="")
 
224
  status = gr.State(value=False)
225
  task = gr.State(value="t2v")
226
  with gr.Row():
@@ -270,12 +282,12 @@ with gr.Blocks() as demo:
270
  with gr.Row():
271
  watermark_wan = gr.Checkbox(label="Watermark", value=True, visible=True, container=False)
272
  seed = gr.Number(label="Seed", value=-1, container=True)
273
- cost_time = gr.Number(label="Cost Time(secs)", value=get_cost_time, interactive=False,
274
- every=120, inputs=[task_id], container=True)
275
- waiting_time = gr.Number(label="Estimated Waiting Time(secs)", value=get_waiting_time, interactive=False,
276
- every=120, inputs=[task_id], container=True)
277
  process_bar = gr.Slider(show_label=True, label="", value=get_process_bar, maximum=100,
278
- interactive=True, every=120, inputs=[task_id, status], container=True)
279
 
280
  fake_video = gr.Video(label='Examples', visible=False, interactive=False)
281
  with gr.Row(visible=True) as t2v_eg:
@@ -300,8 +312,11 @@ with gr.Blocks() as demo:
300
  return gr.Video(value=video_url), ret_t2v_btn, ret_i2v_btn, ret_t2v_status_btn, ret_i2v_status_btn
301
  return gr.Video(value=None), gr.Button(), gr.Button(), gr.Button(), gr.Button()
302
 
 
 
 
303
 
304
- process_bar.change(process_change, inputs=[task_id, task],
305
  outputs=[result_gallery, run_t2v_button, run_i2v_button,
306
  t2v_refresh_status, i2v_refresh_status])
307
 
@@ -320,26 +335,34 @@ with gr.Blocks() as demo:
320
  run_t2v_button.click(
321
  fn=t2v_generation_async,
322
  inputs=[txt2vid_prompt, resolution, watermark_wan, seed],
323
- outputs=[task_id, status, run_t2v_button, t2v_refresh_status],
324
  )
325
 
326
  run_i2v_button.click(
327
  fn=i2v_generation_async,
328
  inputs=[img2vid_prompt, img2vid_image, watermark_wan, seed],
329
- outputs=[task_id, status, run_i2v_button, i2v_refresh_status],
330
  )
331
 
332
  def status_refresh(task_id, task, status):
333
- cost_time = get_cost_time(task_id)
 
 
 
 
 
 
334
  waiting_time = get_waiting_time(task_id)
335
- process_bar = get_process_bar(task_id, status)
 
 
336
  process_change_ret = process_change(task_id, task)
337
- return process_change_ret + [cost_time, waiting_time, process_bar]
338
 
339
 
340
  t2v_refresh_status.click(
341
  fn=status_refresh,
342
- inputs=[task_id, task, status],
343
  outputs=[result_gallery, run_t2v_button, run_i2v_button,
344
  t2v_refresh_status, i2v_refresh_status,
345
  cost_time, waiting_time, process_bar]
@@ -347,7 +370,7 @@ with gr.Blocks() as demo:
347
 
348
  i2v_refresh_status.click(
349
  fn=status_refresh,
350
- inputs=[task_id, task, status],
351
  outputs=[result_gallery, run_t2v_button, run_i2v_button,
352
  t2v_refresh_status, i2v_refresh_status,
353
  cost_time, waiting_time, process_bar]
 
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
 
 
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):
 
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,
 
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):
 
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
 
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
 
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
 
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 online_get_waiting_time(task, t2v_task_id, i2v_task_id):
164
+ task_id = t2v_task_id if task == "t2v" else i2v_task_id
165
+ return get_waiting_time(task_id)
166
 
167
  def clean_task_status():
168
  # clean the task over 1800 seconds
 
180
 
181
  def get_cost_time(task_id):
182
  if task_id in task_status and not task_status[task_id]["status"]:
183
+ et = int(time.time() - task_status[task_id]["time"])
184
  return f"{et:.2f}"
185
  else:
186
  return gr.Textbox()
187
 
188
+ def online_get_cost_time(task, t2v_task_id, i2v_task_id):
189
+ task_id = t2v_task_id if task == "t2v" else i2v_task_id
190
+ return get_cost_time(task_id)
191
 
192
+
193
+ def get_process_bar(task, t2v_task_id, i2v_task_id, status):
194
+ task_id = t2v_task_id if task == "t2v" else i2v_task_id
195
  clean_task_status()
196
  if task_id not in task_status:
197
  task_status[task_id] = {
 
211
  if task_status[task_id]["status"]:
212
  task_status[task_id]["value"] = 100
213
  else:
214
+ task_status[task_id]["value"] += 5
215
  if task_status[task_id]["value"] >= 100 and not task_status[task_id]["status"]:
216
  task_status[task_id]["value"] = 95
217
  # print(task_id, task_status[task_id], task_status)
 
231
  </div>
232
 
233
  """)
234
+ t2v_task_id = gr.State(value="")
235
+ i2v_task_id = gr.State(value="")
236
  status = gr.State(value=False)
237
  task = gr.State(value="t2v")
238
  with gr.Row():
 
282
  with gr.Row():
283
  watermark_wan = gr.Checkbox(label="Watermark", value=True, visible=True, container=False)
284
  seed = gr.Number(label="Seed", value=-1, container=True)
285
+ cost_time = gr.Number(label="Cost Time(secs)", value=online_get_cost_time, interactive=False,
286
+ every=120, inputs=[task, t2v_task_id, i2v_task_id], container=True)
287
+ waiting_time = gr.Number(label="Estimated Waiting Time(secs)", value=online_get_waiting_time, interactive=False,
288
+ every=120, inputs=[task, t2v_task_id, i2v_task_id], container=True)
289
  process_bar = gr.Slider(show_label=True, label="", value=get_process_bar, maximum=100,
290
+ interactive=True, every=120, inputs=[task, t2v_task_id, i2v_task_id, status], container=True)
291
 
292
  fake_video = gr.Video(label='Examples', visible=False, interactive=False)
293
  with gr.Row(visible=True) as t2v_eg:
 
312
  return gr.Video(value=video_url), ret_t2v_btn, ret_i2v_btn, ret_t2v_status_btn, ret_i2v_status_btn
313
  return gr.Video(value=None), gr.Button(), gr.Button(), gr.Button(), gr.Button()
314
 
315
+ def online_process_change(task, t2v_task_id, i2v_task_id):
316
+ task_id = t2v_task_id if task == 't2v' else i2v_task_id
317
+ return process_change(task_id, task)
318
 
319
+ process_bar.change(online_process_change, inputs=[task, t2v_task_id, i2v_task_id],
320
  outputs=[result_gallery, run_t2v_button, run_i2v_button,
321
  t2v_refresh_status, i2v_refresh_status])
322
 
 
335
  run_t2v_button.click(
336
  fn=t2v_generation_async,
337
  inputs=[txt2vid_prompt, resolution, watermark_wan, seed],
338
+ outputs=[t2v_task_id, status, run_t2v_button, t2v_refresh_status],
339
  )
340
 
341
  run_i2v_button.click(
342
  fn=i2v_generation_async,
343
  inputs=[img2vid_prompt, img2vid_image, watermark_wan, seed],
344
+ outputs=[i2v_task_id, status, run_i2v_button, i2v_refresh_status],
345
  )
346
 
347
  def status_refresh(task_id, task, status):
348
+ if task_id in task_status and not task_status[task_id]["status"]:
349
+ cost_time = int(time.time() - task_status[task_id]["time"])
350
+ else:
351
+ cost_time = 0
352
+ status, video_url = get_result_with_task_id(task_id)
353
+ task_status[task_id]["status"] = status
354
+ task_status[task_id]["url"] = video_url
355
  waiting_time = get_waiting_time(task_id)
356
+ value = task_status.get(task_id, {"value": 100})["value"]
357
+ value = min(value, int(cost_time*100/waiting_time))
358
+ process_bar = gr.Slider(label=f"({value}%)Generating" if value % 2 == 1 else f"({value}%)Generating.....", value=value)
359
  process_change_ret = process_change(task_id, task)
360
+ return *process_change_ret, cost_time, waiting_time, process_bar
361
 
362
 
363
  t2v_refresh_status.click(
364
  fn=status_refresh,
365
+ inputs=[t2v_task_id, task, status],
366
  outputs=[result_gallery, run_t2v_button, run_i2v_button,
367
  t2v_refresh_status, i2v_refresh_status,
368
  cost_time, waiting_time, process_bar]
 
370
 
371
  i2v_refresh_status.click(
372
  fn=status_refresh,
373
+ inputs=[i2v_task_id, task, status],
374
  outputs=[result_gallery, run_t2v_button, run_i2v_button,
375
  t2v_refresh_status, i2v_refresh_status,
376
  cost_time, waiting_time, process_bar]