Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,8 +15,7 @@ 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 =
|
19 |
-
LIMIT_HISTORY_RUNING_TASK = 4
|
20 |
|
21 |
task_status = {}
|
22 |
|
@@ -33,7 +32,7 @@ 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):
|
@@ -41,7 +40,7 @@ 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,
|
45 |
return None, gr.Button(visible=True)
|
46 |
try:
|
47 |
rsp = VideoSynthesis.call(model="wanx2.1-t2v-plus", prompt=prompt, seed=seed,
|
@@ -56,12 +55,11 @@ def t2v_generation(prompt, resolution, watermark_wan, seed=-1):
|
|
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 {
|
64 |
-
return None, False, gr.Button(visible=True), gr.Button(visible=False)
|
65 |
try:
|
66 |
rsp = VideoSynthesis.async_call(model="wanx2.1-t2v-plus",
|
67 |
prompt=prompt,
|
@@ -70,10 +68,10 @@ def t2v_generation_async(prompt, size, watermark_wan, seed=-1):
|
|
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)
|
74 |
except Exception as e:
|
75 |
gr.Warning(f"Warning: {e}")
|
76 |
-
return None, True, gr.Button(), gr.Button()
|
77 |
|
78 |
|
79 |
def i2v_generation(prompt, image, watermark_wan, seed=-1):
|
@@ -92,21 +90,21 @@ 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 {
|
99 |
-
return "", None, gr.Button(visible=True), gr.Button(visible=False)
|
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)
|
107 |
except Exception as e:
|
108 |
gr.Warning(f"Warning: {e}")
|
109 |
-
return "", None, gr.Button(), gr.Button()
|
|
|
110 |
|
111 |
def get_result_with_task_id(task_id):
|
112 |
if task_id == "": return True, None
|
@@ -124,7 +122,7 @@ def get_result_with_task_id(task_id):
|
|
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
|
128 |
except:
|
129 |
video_url = None
|
130 |
status = False
|
@@ -141,7 +139,7 @@ def allow_task_num():
|
|
141 |
num += 1
|
142 |
if not task_status[task_id]["status"]:
|
143 |
total_num += 1
|
144 |
-
return num < LIMIT_RUNING_TASK or
|
145 |
|
146 |
def get_waiting_time(task_id):
|
147 |
# if the num of running task < Limit
|
@@ -156,9 +154,9 @@ def get_waiting_time(task_id):
|
|
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(
|
160 |
else:
|
161 |
-
return int(latest_submit_tasks * (total_task_info["total_process_cost"]/
|
162 |
|
163 |
def clean_task_status():
|
164 |
# clean the task over 1800 seconds
|
@@ -176,7 +174,7 @@ def clean_task_status():
|
|
176 |
|
177 |
def get_cost_time(task_id):
|
178 |
if task_id in task_status and not task_status[task_id]["status"]:
|
179 |
-
et =
|
180 |
return f"{et:.2f}"
|
181 |
else:
|
182 |
return gr.Textbox()
|
@@ -202,7 +200,7 @@ def get_process_bar(task_id, status):
|
|
202 |
if task_status[task_id]["status"]:
|
203 |
task_status[task_id]["value"] = 100
|
204 |
else:
|
205 |
-
task_status[task_id]["value"] +=
|
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)
|
@@ -332,19 +330,11 @@ with gr.Blocks() as demo:
|
|
332 |
)
|
333 |
|
334 |
def status_refresh(task_id, task, status):
|
335 |
-
|
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 |
-
|
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
|
348 |
|
349 |
|
350 |
t2v_refresh_status.click(
|
|
|
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 |
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 |
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 |
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 |
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 |
|
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 |
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 |
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 |
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 |
|
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()
|
|
|
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)
|
|
|
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(
|