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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
from datetime import datetime as Datetime
|
4 |
+
import gradio as gr
|
5 |
+
import json
|
6 |
+
import requests
|
7 |
+
import threading
|
8 |
+
import time
|
9 |
+
import hashlib
|
10 |
+
sys.path.append('./')
|
11 |
+
from oss_utils import *
|
12 |
+
from myconfigs import *
|
13 |
+
from cache_util import RedisCache
|
14 |
+
|
15 |
+
|
16 |
+
scale_depth = 0.7
|
17 |
+
scale_pose = 0.5
|
18 |
+
|
19 |
+
def md5_hash_file(filename):
|
20 |
+
"""Compute the MD5 hash of the contents of the given file."""
|
21 |
+
# Create a new MD5 hash object
|
22 |
+
md5_hash = hashlib.md5()
|
23 |
+
# Open the file in binary mode and read chunks
|
24 |
+
with open(filename, 'rb') as f:
|
25 |
+
# Read and update hash string value in blocks of 4K
|
26 |
+
for byte_block in iter(lambda: f.read(4096), b""):
|
27 |
+
md5_hash.update(byte_block)
|
28 |
+
# Return the hexadecimal digest of the hash
|
29 |
+
return md5_hash.hexdigest()
|
30 |
+
|
31 |
+
def get_dirnames(filePath='', tail=".mp4", reserve_num=-1):
|
32 |
+
if not os.path.isdir(filePath):
|
33 |
+
return []
|
34 |
+
if len(tail) <= 0:
|
35 |
+
return []
|
36 |
+
len_tail = len(tail)
|
37 |
+
lists = os.listdir(filePath)
|
38 |
+
file_list = []
|
39 |
+
for i in range(len(lists)):
|
40 |
+
cur_file = lists[i]
|
41 |
+
# if os.path.isfile(cur_file):
|
42 |
+
if len(cur_file) > 4 and cur_file[-len_tail:] == tail:
|
43 |
+
full_file_path = os.path.join(filePath, cur_file)
|
44 |
+
file_list.append(full_file_path)
|
45 |
+
# 按照时间排序
|
46 |
+
sorted_list = sorted(file_list, key=lambda x: os.path.getctime(x))
|
47 |
+
sorted_list = sorted_list[::-1] # 倒序
|
48 |
+
# 保留最近的100个视频
|
49 |
+
if reserve_num > 0:
|
50 |
+
for i in range(len(sorted_list)):
|
51 |
+
if i >= reserve_num:
|
52 |
+
os.remove(sorted_list[i])
|
53 |
+
return sorted_list
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
def sync_request_local(request_id, data):
|
58 |
+
headers = {
|
59 |
+
"Content-Type": "application/json",
|
60 |
+
"Accept": "application/json",
|
61 |
+
# "Authorization": APP_AUTH_TEXTURE,
|
62 |
+
# "X-DashScope-Async": "enable",
|
63 |
+
# "X-DashScope-DataInspection": "enable"
|
64 |
+
}
|
65 |
+
url_create_task = 'http://0.0.0.0:8000/api'
|
66 |
+
|
67 |
+
print(f"request_id: {request_id}, request type: video generation, json input: {data}")
|
68 |
+
res_ = requests.post(url_create_task, data=data, headers=headers)
|
69 |
+
res = json.loads(res_.content.decode())
|
70 |
+
|
71 |
+
result_video_url = ''
|
72 |
+
if res['header']['status_name'] == 'Success':
|
73 |
+
result_video_url = res['payload']['output']['res_video_path']
|
74 |
+
print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {result_video_url}")
|
75 |
+
else:
|
76 |
+
print(f"request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_video_url}")
|
77 |
+
return result_video_url
|
78 |
+
|
79 |
+
def sync_request_cartoon(request_id, data):
|
80 |
+
headers = {
|
81 |
+
"Content-Type": "application/json",
|
82 |
+
"Accept": "application/json",
|
83 |
+
"Authorization": EAS_AUTH_CARTOONRECOG,
|
84 |
+
# "X-DashScope-Async": "enable",
|
85 |
+
# "X-DashScope-DataInspection": "enable"
|
86 |
+
}
|
87 |
+
url_create_task = 'http://1096433202046721.cn-shanghai.pai-eas.aliyuncs.com/api/predict/videogene_supp/api'
|
88 |
+
|
89 |
+
print(f"request_id: {request_id}, request type: cartoon recognize, json input: {data}")
|
90 |
+
res_ = requests.post(url_create_task, data=data, headers=headers)
|
91 |
+
# print(res_)
|
92 |
+
# print(res_.content)
|
93 |
+
res = json.loads(res_.content.decode())
|
94 |
+
|
95 |
+
cartoon_recog = ''
|
96 |
+
if res['payload']['output']['error_message'] == 'Success':
|
97 |
+
cartoon_recog = res['payload']['output']['key']['label']
|
98 |
+
# print(f"request_id: {request_id} cartoon_recog: {cartoon_recog}")
|
99 |
+
print(f"request_id: {request_id}, request type: cartoon recognize, retuen message: Succees, result: {cartoon_recog}")
|
100 |
+
else:
|
101 |
+
print(f"request_id: {request_id}, request type: cartoon recognize, retuen message: Faild, result: {cartoon_recog}")
|
102 |
+
return cartoon_recog
|
103 |
+
|
104 |
+
|
105 |
+
def sync_request_translate_en2cn(request_id, data):
|
106 |
+
headers = {
|
107 |
+
"Content-Type": "application/json",
|
108 |
+
"Accept": "application/json",
|
109 |
+
"Authorization": EAS_AUTH_CARTOONRECOG,
|
110 |
+
# "X-DashScope-Async": "enable",
|
111 |
+
# "X-DashScope-DataInspection": "enable"
|
112 |
+
}
|
113 |
+
url_create_task = 'http://1096433202046721.cn-shanghai.pai-eas.aliyuncs.com/api/predict/videogene_supp/api'
|
114 |
+
|
115 |
+
print(f"[{request_id}], request type: translate en2cn, json input: {data}")
|
116 |
+
res_ = requests.post(url_create_task, data=data, headers=headers)
|
117 |
+
# print(res_)
|
118 |
+
# print(res_.content)
|
119 |
+
res = json.loads(res_.content.decode())
|
120 |
+
|
121 |
+
translate_cn = ''
|
122 |
+
if res['payload']['output']['error_message'] == 'Success':
|
123 |
+
translate_cn = res['payload']['output']['key']
|
124 |
+
# print(f"{request_id} translate_cn: {translate_cn}")
|
125 |
+
print(f"[{request_id}], request type: translate en2cn, retuen message: Succees, result: {translate_cn}")
|
126 |
+
else:
|
127 |
+
print(f"[{request_id}], request type: translate en2cn, retuen message: Faild, result: {translate_cn}")
|
128 |
+
return translate_cn
|
129 |
+
|
130 |
+
def async_request_video_generation(request_id, data):
|
131 |
+
headers = {
|
132 |
+
"Content-Type": "application/json",
|
133 |
+
"Accept": "application/json",
|
134 |
+
# "Authorization": APP_AUTH_TEXTURE,
|
135 |
+
# "X-DashScope-Async": "enable",
|
136 |
+
# "X-DashScope-DataInspection": "enable"
|
137 |
+
}
|
138 |
+
|
139 |
+
# 1.发起一个异步请求
|
140 |
+
url_create_task = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/submit'
|
141 |
+
print(f"request_id: {request_id}, request type: video generation, json input: {data}")
|
142 |
+
res_ = requests.post(url_create_task, data=data, headers=headers)
|
143 |
+
# print("res_=", res_)
|
144 |
+
# print(res_.content)
|
145 |
+
result_json = json.loads(res_.content.decode("utf-8"))
|
146 |
+
|
147 |
+
# # 2.异步查询结果
|
148 |
+
# is_running = True
|
149 |
+
# running_print_count = 0
|
150 |
+
# res_video_path = None
|
151 |
+
# while is_running:
|
152 |
+
# url_query = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/query-result'
|
153 |
+
# res_ = requests.post(url_query, data=data, headers=headers)
|
154 |
+
# respose_code = res_.status_code
|
155 |
+
# if 200 == respose_code:
|
156 |
+
# res = json.loads(res_.content.decode())
|
157 |
+
# if "SUCCESS" == res['header']['task_status']:
|
158 |
+
# if 200 == res['payload']['output']['error_code']:
|
159 |
+
# res_video_path = res['payload']['output']['res_video_path']
|
160 |
+
# print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {res_video_path}")
|
161 |
+
# break
|
162 |
+
# else:
|
163 |
+
# print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
|
164 |
+
# # raise gr.Error(f'algo error.')
|
165 |
+
# break
|
166 |
+
# elif "RUNNING" == res['header']['task_status']:
|
167 |
+
# if running_print_count == 0:
|
168 |
+
# print(f'request_id: {request_id}, request type: video generation, retuen message: running..., result: {result_json}')
|
169 |
+
# running_print_count += 1
|
170 |
+
# time.sleep(1)
|
171 |
+
# elif "FAILED" == res['header']['task_status']:
|
172 |
+
# print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
|
173 |
+
# # raise gr.Error(f'query result faild.')
|
174 |
+
# break
|
175 |
+
# else:
|
176 |
+
# print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
|
177 |
+
# # raise gr.Error(f'query result faild.')
|
178 |
+
# break
|
179 |
+
# else:
|
180 |
+
# print(f'request_id: {request_id}: Fail to query task result: {res_.content}')
|
181 |
+
# # raise gr.Error("Fail to query task result.")
|
182 |
+
# break
|
183 |
+
# return res_video_path
|
184 |
+
|
185 |
+
|
186 |
+
|
187 |
+
def async_query_video_generation(request_id, data):
|
188 |
+
headers = {
|
189 |
+
"Content-Type": "application/json",
|
190 |
+
"Accept": "application/json",
|
191 |
+
# "Authorization": APP_AUTH_TEXTURE,
|
192 |
+
# "X-DashScope-Async": "enable",
|
193 |
+
# "X-DashScope-DataInspection": "enable"
|
194 |
+
}
|
195 |
+
|
196 |
+
# # 1.发起一个异步请求
|
197 |
+
# url_create_task = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/submit'
|
198 |
+
# print(f"request_id: {request_id}, request type: video generation, json input: {data}")
|
199 |
+
# res_ = requests.post(url_create_task, data=data, headers=headers)
|
200 |
+
# # print("res_=", res_)
|
201 |
+
# # print(res_.content)
|
202 |
+
# result_json = json.loads(res_.content.decode("utf-8"))
|
203 |
+
|
204 |
+
# 2.异步查询结果
|
205 |
+
is_running = True
|
206 |
+
running_print_count = 0
|
207 |
+
res_video_path = None
|
208 |
+
while is_running:
|
209 |
+
url_query = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/query-result'
|
210 |
+
res_ = requests.post(url_query, data=data, headers=headers)
|
211 |
+
respose_code = res_.status_code
|
212 |
+
if 200 == respose_code:
|
213 |
+
res = json.loads(res_.content.decode())
|
214 |
+
if "SUCCESS" == res['header']['task_status']:
|
215 |
+
if 200 == res['payload']['output']['error_code']:
|
216 |
+
res_video_path = res['payload']['output']['res_video_path']
|
217 |
+
print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {res_video_path}")
|
218 |
+
break
|
219 |
+
else:
|
220 |
+
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
|
221 |
+
# raise gr.Error(f'algo error.')
|
222 |
+
break
|
223 |
+
elif "RUNNING" == res['header']['task_status']:
|
224 |
+
if running_print_count == 0:
|
225 |
+
print(f'request_id: {request_id}, request type: video generation, retuen message: running..., result: {res}')
|
226 |
+
running_print_count += 1
|
227 |
+
time.sleep(1)
|
228 |
+
elif "FAILED" == res['header']['task_status']:
|
229 |
+
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
|
230 |
+
break
|
231 |
+
else:
|
232 |
+
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
|
233 |
+
break
|
234 |
+
else:
|
235 |
+
print(f'request_id: {request_id}: Fail to query task result: {res_.content}')
|
236 |
+
# raise gr.Error("Fail to query task result.")
|
237 |
+
break
|
238 |
+
return res_video_path
|
239 |
+
|
240 |
+
|
241 |
+
def query_video_generation(request_id, data):
|
242 |
+
headers = {
|
243 |
+
"Content-Type": "application/json",
|
244 |
+
"Accept": "application/json",
|
245 |
+
# "Authorization": APP_AUTH_TEXTURE,
|
246 |
+
# "X-DashScope-Async": "enable",
|
247 |
+
# "X-DashScope-DataInspection": "enable"
|
248 |
+
}
|
249 |
+
|
250 |
+
# # 1.发起一个异步请求
|
251 |
+
# url_create_task = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/submit'
|
252 |
+
# print(f"request_id: {request_id}, request type: video generation, json input: {data}")
|
253 |
+
# res_ = requests.post(url_create_task, data=data, headers=headers)
|
254 |
+
# # print("res_=", res_)
|
255 |
+
# # print(res_.content)
|
256 |
+
# result_json = json.loads(res_.content.decode("utf-8"))
|
257 |
+
|
258 |
+
# 2.异步查询结果
|
259 |
+
is_running = True
|
260 |
+
running_print_count = 0
|
261 |
+
res_video_path = None
|
262 |
+
# while is_running:
|
263 |
+
url_query = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/query-result'
|
264 |
+
res_ = requests.post(url_query, data=data, headers=headers)
|
265 |
+
respose_code = res_.status_code
|
266 |
+
res = json.loads(res_.content.decode())
|
267 |
+
if 200 == respose_code:
|
268 |
+
if "SUCCESS" == res['header']['task_status']:
|
269 |
+
if 200 == res['payload']['output']['error_code']:
|
270 |
+
res_video_path = res['payload']['output']['res_video_path']
|
271 |
+
print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {res_video_path}")
|
272 |
+
return "SUCCESS", res
|
273 |
+
else:
|
274 |
+
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
|
275 |
+
return "FAILED", res
|
276 |
+
elif "RUNNING" == res['header']['task_status']:
|
277 |
+
print(f'request_id: {request_id}, request type: video generation, retuen message: running..., result: {res}')
|
278 |
+
return "RUNNING", res
|
279 |
+
elif "FAILED" == res['header']['task_status']:
|
280 |
+
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
|
281 |
+
return "FAILED", res
|
282 |
+
else:
|
283 |
+
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
|
284 |
+
return "FAILED", res
|
285 |
+
else:
|
286 |
+
print(f'request_id: {request_id}: Fail to query task result: {res_.content}')
|
287 |
+
return "FAILED", res
|
288 |
+
|
289 |
+
|
290 |
+
def sync_request_prompt_caption(request_id, data):
|
291 |
+
headers = {
|
292 |
+
"Content-Type": "application/json",
|
293 |
+
"Accept": "application/json",
|
294 |
+
"Authorization": EAS_AUTH_PROMPT,
|
295 |
+
# "X-DashScope-Async": "enable",
|
296 |
+
# "X-DashScope-DataInspection": "enable"
|
297 |
+
}
|
298 |
+
url_create_task = 'http://1096433202046721.cn-shanghai.pai-eas.aliyuncs.com/api/predict/videogene_supp_gu50/api'
|
299 |
+
|
300 |
+
print(f"request_id: {request_id}, request type: prompt_caption, json input: {data}")
|
301 |
+
res_ = requests.post(url_create_task, data=data, headers=headers)
|
302 |
+
# print(res_)
|
303 |
+
# print(res_.content)
|
304 |
+
res = json.loads(res_.content.decode())
|
305 |
+
|
306 |
+
prompt_caption = ''
|
307 |
+
gender = ''
|
308 |
+
if res['payload']['output']['error_message'] == 'Success':
|
309 |
+
key = res['payload']['output']['key'] # {"gender":"female","prompt":"Asian woman, 25-35, long black hair, dark brown eyes, average height","style":""}
|
310 |
+
gender = key['gender']
|
311 |
+
prompt_caption = 'a '+ key['gender'] + ', ' + key['prompt']
|
312 |
+
style = key['style']
|
313 |
+
if style != '':
|
314 |
+
prompt_caption = style + ' style, ' + prompt_caption
|
315 |
+
|
316 |
+
print(f"request_id: {request_id}, request type: prompt_caption, retuen message: Succees, result: {prompt_caption}")
|
317 |
+
else:
|
318 |
+
print(f"request_id: {request_id}, request type: prompt_caption, retuen message: Faild, result: {prompt_caption}")
|
319 |
+
return prompt_caption, gender
|
320 |
+
|
321 |
+
import re
|
322 |
+
def extract_mp4_filename(input_string):
|
323 |
+
# Regular expression pattern to match the file name
|
324 |
+
pattern = r"template_\d+\.mp4"
|
325 |
+
# Extract the MP4 file name
|
326 |
+
match = re.search(pattern, input_string)
|
327 |
+
if match:
|
328 |
+
return match.group()
|
329 |
+
else:
|
330 |
+
return None # or you could raise an exception or return an empty string
|
331 |
+
|
332 |
+
|
333 |
+
class HumanGenService:
|
334 |
+
def __init__(self):
|
335 |
+
self.oss_service = ossService()
|
336 |
+
self.all_user_requests = {}
|
337 |
+
self.all_requests = []
|
338 |
+
self.all_requests_time = {} # dict: request_id, time
|
339 |
+
self.lock = threading.Lock()
|
340 |
+
|
341 |
+
def translate_en2cn(self, request_id, input_prompt):
|
342 |
+
#--------------- translate service -----------------#
|
343 |
+
translate_data = {}
|
344 |
+
translate_data['header'] = {}
|
345 |
+
translate_data['header']['request_id'] = request_id
|
346 |
+
translate_data['header']['service_id'] = ''
|
347 |
+
translate_data['header']['task_id'] = request_id
|
348 |
+
translate_data['header']['attributes'] = {}
|
349 |
+
translate_data['header']['attributes']['user_id'] = ''
|
350 |
+
translate_data['payload'] = {}
|
351 |
+
translate_data['payload']['input'] = {}
|
352 |
+
translate_data['payload']['input']['work_type'] = 'translate_en2zh'
|
353 |
+
translate_data['payload']['input']['key'] = input_prompt
|
354 |
+
translate_data['payload']['parameters'] = {}
|
355 |
+
translate_data = json.dumps(translate_data) # to string
|
356 |
+
# serving api
|
357 |
+
# print("input_prompt: ", input_prompt)
|
358 |
+
translate_cn = sync_request_translate_en2cn(request_id=request_id, data=translate_data)
|
359 |
+
# print("translate_cn: ", translate_cn)
|
360 |
+
#--------------- translate service -----------------#
|
361 |
+
print(f'[{request_id}] - [HumanGen] - translate ok')
|
362 |
+
return translate_cn
|
363 |
+
|
364 |
+
def click_button_prompt(self, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt='', prompt_template='',model_id=False):
|
365 |
+
cartoon_recog = 'realhuman' # input infer mode id 'face' and 'clothes'
|
366 |
+
if model_id is False:
|
367 |
+
cartoon_recog = 'realhuman'
|
368 |
+
elif model_id is True:
|
369 |
+
cartoon_recog = 'cartoon'
|
370 |
+
|
371 |
+
print(f"request_id: {request_id}, input_mode: {input_mode}")
|
372 |
+
print(f"request_id: {request_id}, cartoon_recog: {cartoon_recog}")
|
373 |
+
print(f"request_id: {request_id}, ref_image_path: {ref_image_path}")
|
374 |
+
print(f"request_id: {request_id}, ref_video_path: {ref_video_path}")
|
375 |
+
|
376 |
+
if input_mode == 'prompt_mode' and (ref_image_path is None or ref_image_path == '' or (not os.path.exists(ref_image_path))):
|
377 |
+
print(f"request_id: {request_id}, 用户未输入图片,prompt caption结束")
|
378 |
+
raise gr.Error("请输入图片!")
|
379 |
+
# return "请输入图片!"
|
380 |
+
|
381 |
+
if input_mode == 'template_mode' and cartoon_recog == 'cartoon' and (ref_image_path is None or ref_image_path == '' or (not os.path.exists(ref_image_path))):
|
382 |
+
print(f"request_id: {request_id}, 用户未输入卡通图片,prompt caption结束")
|
383 |
+
raise gr.Error("请输入卡通图片!")
|
384 |
+
# return "请输入视频!"
|
385 |
+
|
386 |
+
if input_mode == 'template_mode' and cartoon_recog == 'realhuman' and (ref_video_path is None or ref_video_path == ''):
|
387 |
+
print(f"request_id: {request_id}, 用户未输入视频,prompt caption结束")
|
388 |
+
raise gr.Error("请输入视频!")
|
389 |
+
# return "请输入视频!"
|
390 |
+
|
391 |
+
# user_mode = -1
|
392 |
+
# if input_mode == 'template_mode':
|
393 |
+
# if (ref_video_name[:len('template_')] == 'template_'):
|
394 |
+
# user_mode = 0 # mode 0: image + template video
|
395 |
+
# else:
|
396 |
+
# user_mode = 1 # mode 1: image + upload video
|
397 |
+
# if input_mode == 'prompt_mode':
|
398 |
+
# user_mode = 2 # mode 2: image + prompt
|
399 |
+
|
400 |
+
date_string = datetime.datetime.now().strftime('%Y-%m-%d')
|
401 |
+
img1_oss_path = ''
|
402 |
+
vid1_oss_path = ''
|
403 |
+
try:
|
404 |
+
# ref image
|
405 |
+
if not (ref_image_path is None or ref_image_path == '' or (not os.path.exists(ref_image_path))):
|
406 |
+
img_file_name = os.path.basename(ref_image_path)
|
407 |
+
img_extension = os.path.splitext(img_file_name)[1] # 输出:.jpg
|
408 |
+
img1_oss_path = self.oss_service.ObjectName + '/Service/' + date_string + '/' + user_id + '/' + request_id + '/' + "ref_image" + img_extension
|
409 |
+
is_success0, sign_img_oss_path = self.oss_service.uploadOssFile(img1_oss_path, ref_image_path)
|
410 |
+
print(f"request_id: {request_id}, is_success0={is_success0}, sign_img_oss_path={sign_img_oss_path}")
|
411 |
+
|
412 |
+
if input_mode == 'template_mode' and cartoon_recog == 'realhuman':
|
413 |
+
vid_file_name = os.path.basename(ref_video_path)
|
414 |
+
vid_extension = os.path.splitext(vid_file_name)[1] # 输出:.mp4
|
415 |
+
vid1_oss_path = self.oss_service.ObjectName + '/Service/' + date_string + '/' + user_id + '/' + request_id + '/' + "ref_video" + vid_extension
|
416 |
+
is_success1, sign_vid_oss_path = self.oss_service.uploadOssFile(vid1_oss_path, ref_video_path)
|
417 |
+
print(f"request_id: {request_id}, is_success1={is_success1}, sign_vid_oss_path={sign_vid_oss_path}")
|
418 |
+
except Exception as e:
|
419 |
+
print(f"request_id: {request_id}, oss upload error for input local image or video. ")
|
420 |
+
raise gr.Error("oss upload error for input local image or video")
|
421 |
+
|
422 |
+
#-----------------------------prompt caption-----------------------------#
|
423 |
+
# print("sign_img_oss_path: ", sign_img_oss_path)
|
424 |
+
# print("sign_img_oss_path1: ", sign_img_oss_path)
|
425 |
+
prompt_caption_data = {}
|
426 |
+
prompt_caption_data['header'] = {}
|
427 |
+
prompt_caption_data['header']['request_id'] = request_id
|
428 |
+
prompt_caption_data['header']['service_id'] = 'test123'
|
429 |
+
prompt_caption_data['header']['task_id'] = request_id
|
430 |
+
prompt_caption_data['header']['attributes'] = {}
|
431 |
+
prompt_caption_data['header']['attributes']['user_id'] = 'wanx_lab'
|
432 |
+
prompt_caption_data['payload'] = {}
|
433 |
+
prompt_caption_data['payload']['input'] = {}
|
434 |
+
prompt_caption_data['payload']['input']['work_type'] = 'prompt_caption'
|
435 |
+
if input_mode == 'prompt_mode':
|
436 |
+
sign_img_oss_path = 'https://vigen-invi.oss-cn-shanghai-internal' + sign_img_oss_path[len('https://vigen-invi.oss-cn-shanghai'):]
|
437 |
+
prompt_caption_data['payload']['input']['key'] = sign_img_oss_path
|
438 |
+
else:
|
439 |
+
if cartoon_recog == 'cartoon':
|
440 |
+
sign_img_oss_path = 'https://vigen-invi.oss-cn-shanghai-internal' + sign_img_oss_path[len('https://vigen-invi.oss-cn-shanghai'):]
|
441 |
+
prompt_caption_data['payload']['input']['key'] = sign_img_oss_path
|
442 |
+
else:
|
443 |
+
sign_vid_oss_path = 'https://vigen-invi.oss-cn-shanghai-internal' + sign_vid_oss_path[len('https://vigen-invi.oss-cn-shanghai'):]
|
444 |
+
prompt_caption_data['payload']['input']['key'] = sign_vid_oss_path
|
445 |
+
prompt_caption_data['payload']['parameters'] = {}
|
446 |
+
if input_mode == 'prompt_mode':
|
447 |
+
if cartoon_recog == 'cartoon':
|
448 |
+
prompt_caption_data['payload']['parameters']['input_type'] = 'cartoon_id_image' # id_image,cartoon_id_image,reference_video
|
449 |
+
else:
|
450 |
+
prompt_caption_data['payload']['parameters']['input_type'] = 'id_image' # id_image,cartoon_id_image,reference_video
|
451 |
+
else:
|
452 |
+
if cartoon_recog == 'cartoon':
|
453 |
+
prompt_caption_data['payload']['parameters']['input_type'] = 'cartoon_id_image' # id_image,cartoon_id_image,reference_video
|
454 |
+
else:
|
455 |
+
prompt_caption_data['payload']['parameters']['input_type'] = 'reference_video' # id_image,cartoon_id_image,reference_video
|
456 |
+
prompt_caption_data['payload']['parameters']['input_format'] = 'url' # url,oss_path
|
457 |
+
prompt_caption_data = json.dumps(prompt_caption_data) # to string
|
458 |
+
|
459 |
+
# serving api
|
460 |
+
prompt_caption_en,__ = sync_request_prompt_caption(request_id=request_id, data=prompt_caption_data)
|
461 |
+
print(f"request_id: {request_id}, prompt_caption_en: {prompt_caption_en}")
|
462 |
+
|
463 |
+
prompt_caption_cn = self.translate_en2cn(request_id, prompt_caption_en)
|
464 |
+
print(f"request_id: {request_id}, prompt_caption_cn: {prompt_caption_cn}")
|
465 |
+
#-----------------------------prompt caption-----------------------------#
|
466 |
+
return prompt_caption_cn
|
467 |
+
|
468 |
+
# def template_video_2_prompt(self, ref_video_name):
|
469 |
+
# file_name = ref_video_name
|
470 |
+
# if file_name[:len('template_')] == 'template_':
|
471 |
+
# if file_name not in template_prompts:
|
472 |
+
# raise gr.Error("Please input video is not a template!")
|
473 |
+
# input_prompt = template_prompts[file_name]
|
474 |
+
# if file_name not in template_orign_videos:
|
475 |
+
# raise gr.Error("Please input video is not a template!")
|
476 |
+
# ref_ori_video_path = template_orign_videos[file_name]
|
477 |
+
# return ref_ori_video_path, input_prompt
|
478 |
+
|
479 |
+
def template_video_2_prompt(self, ref_video_name):
|
480 |
+
file_name = ref_video_name
|
481 |
+
ref_ori_video_path = ref_video_name
|
482 |
+
print("videl filename:%s" % ref_video_name)
|
483 |
+
input_prompt = ""
|
484 |
+
if file_name[:len('template_')] == 'template_':
|
485 |
+
file_name = extract_mp4_filename(file_name)
|
486 |
+
if file_name not in template_prompts:
|
487 |
+
raise gr.Error("The input video is not a template!")
|
488 |
+
input_prompt = template_prompts[file_name]
|
489 |
+
if file_name not in template_orign_videos:
|
490 |
+
raise gr.Error("The input video is not a template!")
|
491 |
+
ref_ori_video_path = template_orign_videos[file_name]
|
492 |
+
else:
|
493 |
+
print("video file not found:%s" % ref_video_name)
|
494 |
+
return ref_ori_video_path, input_prompt
|
495 |
+
|
496 |
+
def click_button_func_async(self, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt='', prompt_template='',model_id=False):
|
497 |
+
start_time = time.time()
|
498 |
+
if is_wanx_platform:
|
499 |
+
user_id = 'wanx_lab'
|
500 |
+
request_id = get_random_string()
|
501 |
+
print(f"request_id: {request_id}, generate user_id: {user_id} and request_id: {request_id}")
|
502 |
+
if user_id is None or user_id == '':
|
503 |
+
user_id = 'test_version_phone'
|
504 |
+
# key by: ref_video_name, digest(ref_image_path), prompt_template, input_prompt,
|
505 |
+
# scale_depth, scale_pose
|
506 |
+
#print("ref_image_path:%s ref_video_path:%s" % (ref_image_path, ref_video_path) )
|
507 |
+
cache_key = None
|
508 |
+
# if ref_image_path and os.path.exists(ref_image_path):
|
509 |
+
# digest_ref_image = md5_hash_file(ref_image_path)
|
510 |
+
# if ref_video_path and os.path.exists(ref_video_path):
|
511 |
+
# ref_video_name = os.path.basename(ref_video_path)
|
512 |
+
# cache_key = "%s_%s_%s_%s_%s_%s" % (ref_video_name, digest_ref_image, prompt_template, input_prompt, scale_depth, scale_pose)
|
513 |
+
# print("cache key:%s" % cache_key)
|
514 |
+
|
515 |
+
# relative_oss_path = self.generate_video(cache_key, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt, prompt_template, model_id=model_id)
|
516 |
+
self.generate_video(cache_key, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt, prompt_template, model_id=model_id)
|
517 |
+
|
518 |
+
# oss_path = "oss://vigen-invi/" + relative_oss_path
|
519 |
+
# style = "video/snapshot,t_1000,f_jpg,w_544,h_768,m_fast"
|
520 |
+
# params = {'x-oss-process': style}
|
521 |
+
# _, snapshot_image = self.oss_service.sign(oss_path, timeout=3600*100, params=params)
|
522 |
+
# _, video_url = self.oss_service.sign(oss_path, timeout=3600*100)
|
523 |
+
|
524 |
+
total_time_minutes = (time.time() -start_time) / 60 # minites
|
525 |
+
print(f"request_id: {request_id}, 请求耗时: {total_time_minutes:.1f} 分钟")
|
526 |
+
|
527 |
+
# return video_url, snapshot_image
|
528 |
+
|
529 |
+
#@RedisCache(expire=60*60*24*7) # 7 天有效期
|
530 |
+
def generate_video(self, cache_key, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt='', prompt_template='',model_id=False):
|
531 |
+
self.lock.acquire()
|
532 |
+
if user_id in self.all_user_requests and len(self.all_user_requests[user_id]) > 0:
|
533 |
+
print(f"request_id: {request_id}, 您还有未处理完的任务!")
|
534 |
+
self.lock.release()
|
535 |
+
raise gr.Error("视频生成任务请求失败,可能服务器连接问题,请重试")
|
536 |
+
# return None
|
537 |
+
self.all_requests.append(request_id)
|
538 |
+
if request_id not in self.all_requests_time:
|
539 |
+
self.all_requests_time[request_id] = time.time()
|
540 |
+
if user_id not in self.all_user_requests:
|
541 |
+
self.all_user_requests[user_id] = []
|
542 |
+
self.all_user_requests[user_id].append(request_id)
|
543 |
+
else:
|
544 |
+
self.all_user_requests[user_id].append(request_id)
|
545 |
+
self.lock.release()
|
546 |
+
|
547 |
+
print(f">>>request_id: {request_id}, user_id: {user_id}, 加入新任务!")
|
548 |
+
|
549 |
+
print(f"request_id: {request_id}, start process")
|
550 |
+
|
551 |
+
ref_video_name = ''
|
552 |
+
if input_mode == 'template_mode' and os.path.exists(ref_video_path):
|
553 |
+
ref_video_name = os.path.basename(ref_video_path)
|
554 |
+
|
555 |
+
user_mode = -1
|
556 |
+
if input_mode == 'template_mode':
|
557 |
+
if (ref_video_name[:len('template_')] == 'template_'):
|
558 |
+
user_mode = 0 # mode 0: image + template video
|
559 |
+
else:
|
560 |
+
user_mode = 1 # mode 1: image + upload video
|
561 |
+
if input_mode == 'prompt_mode':
|
562 |
+
user_mode = 2 # mode 2: image + prompt
|
563 |
+
|
564 |
+
cartoon_recog = 'realhuman' # input infer mode id 'face' and 'clothes'
|
565 |
+
if model_id is False:
|
566 |
+
cartoon_recog = 'realhuman'
|
567 |
+
elif model_id is True:
|
568 |
+
cartoon_recog = 'cartoon'
|
569 |
+
|
570 |
+
# try:
|
571 |
+
# import shutil
|
572 |
+
# dir_path = "/tmp/gradio"
|
573 |
+
# shutil.rmtree(dir_path)
|
574 |
+
# print(f"目录: {dir_path} 删除成功")
|
575 |
+
# except OSError as e:
|
576 |
+
# print(f"目录: {dir_path} 删除失败:", e)
|
577 |
+
|
578 |
+
date_string = datetime.datetime.now().strftime('%Y-%m-%d')
|
579 |
+
img1_oss_path = ''
|
580 |
+
vid1_oss_path = ''
|
581 |
+
try:
|
582 |
+
# ref image
|
583 |
+
img_file_name = os.path.basename(ref_image_path)
|
584 |
+
img_extension = os.path.splitext(img_file_name)[1] # 输出:.jpg
|
585 |
+
img1_oss_path = self.oss_service.ObjectName + '/Service/' + date_string + '/' + user_id + '/' + request_id + '/' + "ref_image" + img_extension
|
586 |
+
is_success0, sign_img_oss_path = self.oss_service.uploadOssFile(img1_oss_path, ref_image_path)
|
587 |
+
print(f"request_id: {request_id}, is_success0={is_success0}, sign_img_oss_path={sign_img_oss_path}")
|
588 |
+
|
589 |
+
if user_mode == 0 or user_mode == 1:
|
590 |
+
vid_file_name = os.path.basename(ref_video_path)
|
591 |
+
vid_extension = os.path.splitext(vid_file_name)[1] # 输出:.mp4
|
592 |
+
vid1_oss_path = self.oss_service.ObjectName + '/Service/' + date_string + '/' + user_id + '/' + request_id + '/' + "ref_video" + vid_extension
|
593 |
+
is_success1, sign_vid_oss_path = self.oss_service.uploadOssFile(vid1_oss_path, ref_video_path)
|
594 |
+
print(f"request_id: {request_id}, is_success1={is_success1}, sign_vid_oss_path={sign_vid_oss_path}")
|
595 |
+
except Exception as e:
|
596 |
+
print(f"request_id: {request_id}, 数据上传失败")
|
597 |
+
raise gr.Error("数据上传失败")
|
598 |
+
# return None
|
599 |
+
|
600 |
+
# #-----------------------------cartoon recog-----------------------------#
|
601 |
+
# # print("sign_img_oss_path: ", sign_img_oss_path)
|
602 |
+
# sign_img_oss_path = 'https://vigen-invi.oss-cn-shanghai-internal' + sign_img_oss_path[len('https://vigen-invi.oss-cn-shanghai'):]
|
603 |
+
# # print("sign_img_oss_path1: ", sign_img_oss_path)
|
604 |
+
# cartoon_data = {}
|
605 |
+
# cartoon_data['header'] = {}
|
606 |
+
# cartoon_data['header']['request_id'] = request_id
|
607 |
+
# cartoon_data['header']['service_id'] = ''
|
608 |
+
# cartoon_data['header']['task_id'] = request_id
|
609 |
+
# cartoon_data['header']['attributes'] = {}
|
610 |
+
# cartoon_data['header']['attributes']['user_id'] = ''
|
611 |
+
# cartoon_data['payload'] = {}
|
612 |
+
# cartoon_data['payload']['input'] = {}
|
613 |
+
# cartoon_data['payload']['input']['work_type'] = 'cartoonreg'
|
614 |
+
# cartoon_data['payload']['input']['key'] = sign_img_oss_path
|
615 |
+
# cartoon_data['payload']['parameters'] = {}
|
616 |
+
# cartoon_data = json.dumps(cartoon_data) # to string
|
617 |
+
# # serving api
|
618 |
+
# cartoon_recog = sync_request_cartoon(request_id=request_id, data=cartoon_data)
|
619 |
+
# print(f"request_id: {request_id}, cartoon_recog: {cartoon_recog}")
|
620 |
+
# #-----------------------------cartoon recog-----------------------------#
|
621 |
+
|
622 |
+
|
623 |
+
# gender_dif = False
|
624 |
+
# if input_mode == 'template_mode' and cartoon_recog == 'realhuman':
|
625 |
+
# try:
|
626 |
+
# #-----------------------------image prompt caption-----------------------------#
|
627 |
+
# prompt_caption_data = {}
|
628 |
+
# prompt_caption_data['header'] = {}
|
629 |
+
# prompt_caption_data['header']['request_id'] = request_id
|
630 |
+
# prompt_caption_data['header']['service_id'] = 'test123'
|
631 |
+
# prompt_caption_data['header']['task_id'] = request_id
|
632 |
+
# prompt_caption_data['header']['attributes'] = {}
|
633 |
+
# prompt_caption_data['header']['attributes']['user_id'] = 'wanx_lab'
|
634 |
+
# prompt_caption_data['payload'] = {}
|
635 |
+
# prompt_caption_data['payload']['input'] = {}
|
636 |
+
# prompt_caption_data['payload']['input']['work_type'] = 'prompt_caption'
|
637 |
+
# sign_img_oss_path_inter = 'https://vigen-invi.oss-cn-shanghai-internal' + sign_img_oss_path[len('https://vigen-invi.oss-cn-shanghai'):]
|
638 |
+
# prompt_caption_data['payload']['input']['key'] = sign_img_oss_path_inter
|
639 |
+
# prompt_caption_data['payload']['parameters'] = {}
|
640 |
+
# prompt_caption_data['payload']['parameters']['input_type'] = 'id_image' # id_image,cartoon_id_image,reference_video
|
641 |
+
# prompt_caption_data['payload']['parameters']['input_format'] = 'url' # url,oss_path
|
642 |
+
# prompt_caption_data = json.dumps(prompt_caption_data) # to string
|
643 |
+
# # serving api
|
644 |
+
# __, img_gender = sync_request_prompt_caption(request_id=request_id, data=prompt_caption_data)
|
645 |
+
# print(f"request_id: {request_id}, img_gender: {img_gender}")
|
646 |
+
# #-----------------------------image prompt caption-----------------------------#
|
647 |
+
|
648 |
+
# #-----------------------------video prompt caption-----------------------------#
|
649 |
+
# prompt_caption_data = {}
|
650 |
+
# prompt_caption_data['header'] = {}
|
651 |
+
# prompt_caption_data['header']['request_id'] = request_id
|
652 |
+
# prompt_caption_data['header']['service_id'] = 'test123'
|
653 |
+
# prompt_caption_data['header']['task_id'] = request_id
|
654 |
+
# prompt_caption_data['header']['attributes'] = {}
|
655 |
+
# prompt_caption_data['header']['attributes']['user_id'] = 'wanx_lab'
|
656 |
+
# prompt_caption_data['payload'] = {}
|
657 |
+
# prompt_caption_data['payload']['input'] = {}
|
658 |
+
# prompt_caption_data['payload']['input']['work_type'] = 'prompt_caption'
|
659 |
+
# sign_vid_oss_path_inter = 'https://vigen-invi.oss-cn-shanghai-internal' + sign_vid_oss_path[len('https://vigen-invi.oss-cn-shanghai'):]
|
660 |
+
# prompt_caption_data['payload']['input']['key'] = sign_vid_oss_path_inter
|
661 |
+
# prompt_caption_data['payload']['parameters'] = {}
|
662 |
+
# prompt_caption_data['payload']['parameters']['input_type'] = 'reference_video' # id_image,cartoon_id_image,reference_video
|
663 |
+
# prompt_caption_data['payload']['parameters']['input_format'] = 'url' # url,oss_path
|
664 |
+
# prompt_caption_data = json.dumps(prompt_caption_data) # to string
|
665 |
+
# # serving api
|
666 |
+
# __, vid_gender = sync_request_prompt_caption(request_id=request_id, data=prompt_caption_data)
|
667 |
+
# print(f"request_id: {request_id}, vid_gender: {vid_gender}")
|
668 |
+
# #-----------------------------video prompt caption-----------------------------#
|
669 |
+
|
670 |
+
# if img_gender != vid_gender:
|
671 |
+
# gender_dif = True
|
672 |
+
|
673 |
+
# except Exception as e:
|
674 |
+
# print(f"request_id: {request_id}, 视频生成任务请求失败,可能服务器连接问题,请重试")
|
675 |
+
# raise gr.Error("视频生成任务请求失败,可能服务器连接问题,请重试")
|
676 |
+
# # return None
|
677 |
+
|
678 |
+
|
679 |
+
#-----------------------------motion generation-----------------------------#
|
680 |
+
data = '{"header":{"request_id":"","service_id":"","task_id":""},"payload":{"input": {"ref_image_path": "", "ref_video_path": "", "ref_video_name": "", "input_prompt": "", "prompt_template": "", "scale_depth": 0.7, "scale_pose": 0.5},"parameters":{}}}'
|
681 |
+
data = json.loads(data) # string to dict
|
682 |
+
data['header']['service_id'] = DASHONE_SERVICE_ID
|
683 |
+
data['header']['request_id'] = request_id
|
684 |
+
data['header']['task_id'] = request_id
|
685 |
+
data['header']['attributes'] = {}
|
686 |
+
data['header']['attributes']['user_id'] = user_id
|
687 |
+
data['payload']['input']['user_id'] = user_id
|
688 |
+
data['payload']['input']['ref_image_path'] = img1_oss_path # sign_img_oss_path
|
689 |
+
data['payload']['input']['ref_video_path'] = vid1_oss_path # sign_vid_oss_path
|
690 |
+
data['payload']['input']['ref_video_name'] = ref_video_name
|
691 |
+
data['payload']['input']['input_prompt'] = input_prompt
|
692 |
+
data['payload']['input']['prompt_template'] = prompt_template
|
693 |
+
data['payload']['input']['scale_depth'] = scale_depth
|
694 |
+
data['payload']['input']['scale_pose'] = scale_pose
|
695 |
+
data['payload']['input']['cartoon_recog'] = cartoon_recog
|
696 |
+
data = json.dumps(data) # to string
|
697 |
+
|
698 |
+
# serving api
|
699 |
+
# sign_oss_path = sync_request_local(request_id=request_id, data=data) # sync
|
700 |
+
|
701 |
+
try:
|
702 |
+
async_request_video_generation(request_id=request_id, data=data) # async
|
703 |
+
print(f"request_id: {request_id}, async_request_video_generation")
|
704 |
+
except Exception as e:
|
705 |
+
print(f"request_id: {request_id}, 视频生成任务请求失败,可能服务器连接问题,请重试")
|
706 |
+
raise gr.Error("视频生成任务请求失败,可能服务器连接问题,请重试")
|
707 |
+
# return None
|
708 |
+
|
709 |
+
# sign_oss_path = ''
|
710 |
+
# try:
|
711 |
+
# sign_oss_path = async_query_video_generation(request_id=request_id, data=data) # async
|
712 |
+
# print(f"request_id: {request_id}, async_query_video_generation sign_oss_path: {sign_oss_path}")
|
713 |
+
# except Exception as e:
|
714 |
+
# print(f"request_id: {request_id}, 视频生成失败请求处理失败. ")
|
715 |
+
# # raise gr.Error("request process faild, sign_oss_path is empty")
|
716 |
+
# return None
|
717 |
+
|
718 |
+
# snapshot_image = ""
|
719 |
+
# if sign_oss_path == '' or sign_oss_path is None:
|
720 |
+
# print(f"request_id: {request_id}, 视频生成失败请求处理失败. ")
|
721 |
+
# # raise gr.Error("request process faild, sign_oss_path is empty")
|
722 |
+
# return None
|
723 |
+
# # else:
|
724 |
+
# # return sign_oss_path
|
725 |
+
# #-----------------------------motion generation-----------------------------#
|
726 |
+
|
727 |
+
# # try:
|
728 |
+
# # file_path = "data/output/"+user_id
|
729 |
+
# # if not os.path.exists(file_path):
|
730 |
+
# # os.makedirs(file_path)
|
731 |
+
|
732 |
+
# # local_res_video_path = "data/output/"+user_id+'/'+request_id+".mp4"
|
733 |
+
# # # self.oss_service.downloadFile(sign_oss_path, local_res_video_path)
|
734 |
+
# # self.oss_service.downloadOssFile(sign_oss_path, local_res_video_path)
|
735 |
+
|
736 |
+
# # print(f"request_id: {request_id}, finished sign_oss_path download. ")
|
737 |
+
|
738 |
+
# # except Exception as e:
|
739 |
+
# # print(f"request_id: {request_id}, result video download error. ")
|
740 |
+
# # # raise gr.Error("result video download error")
|
741 |
+
# # return None
|
742 |
+
|
743 |
+
# # # clear output folder
|
744 |
+
# # get_dirnames(filePath='data/output/'+user_id, tail=".mp4", reserve_num=20)
|
745 |
+
|
746 |
+
|
747 |
+
print(f"=================end request_id: {request_id}")
|
748 |
+
# return sign_oss_path
|
749 |
+
|
750 |
+
|
751 |
+
def valid_check(self, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt='', prompt_template='',model_id=False):
|
752 |
+
if is_wanx_platform:
|
753 |
+
user_id = 'wanx_lab'
|
754 |
+
if user_id is None or user_id == '':
|
755 |
+
user_id = 'test_version_phone'
|
756 |
+
print(f"-----------------request_id: {request_id}, user_id: {user_id}---------------")
|
757 |
+
|
758 |
+
self.lock.acquire()
|
759 |
+
if user_id in self.all_user_requests:
|
760 |
+
if len(self.all_user_requests[user_id]) > 0:
|
761 |
+
self.lock.release()
|
762 |
+
# raise gr.Error("您的视频正在生成过程中,请忽略错误信息,等待并点击刷新按钮,在用户生成区域查看结果。")
|
763 |
+
return "您的视频正在生成过程中,等处理完毕后再提交新的任务, 点击刷新获取最新生成进度!"
|
764 |
+
self.lock.release()
|
765 |
+
|
766 |
+
cartoon_recog = 'realhuman' # input infer mode id 'face' and 'clothes'
|
767 |
+
if model_id is False:
|
768 |
+
cartoon_recog = 'realhuman'
|
769 |
+
elif model_id is True:
|
770 |
+
cartoon_recog = 'cartoon'
|
771 |
+
|
772 |
+
print(f"request_id: {request_id}, input_mode: {input_mode}")
|
773 |
+
print(f"request_id: {request_id}, ref_image_path: {ref_image_path}")
|
774 |
+
print(f"request_id: {request_id}, ref_video_path: {ref_video_path}")
|
775 |
+
print(f"request_id: {request_id}, input_prompt = {input_prompt}")
|
776 |
+
print(f"request_id: {request_id}, prompt_template = {prompt_template}")
|
777 |
+
print(f"request_id: {request_id}, scale_depth = {scale_depth}")
|
778 |
+
print(f"request_id: {request_id}, scale_pose = {scale_pose}")
|
779 |
+
print(f"request_id: {request_id}, model_id = {model_id}")
|
780 |
+
print(f"request_id: {request_id}, style: {cartoon_recog}")
|
781 |
+
|
782 |
+
if ref_image_path is None or ref_image_path == '' or (not os.path.exists(ref_image_path)):
|
783 |
+
print(f"request_id: {request_id}, 用户未输入图片,任务结束")
|
784 |
+
# raise gr.Error("请输入图片!")
|
785 |
+
return "请输入图片!"
|
786 |
+
|
787 |
+
if input_mode == 'prompt_mode' and (input_prompt == '' or input_prompt == [] or input_prompt is None):
|
788 |
+
print(f"request_id: {request_id}, 用户未输入prompt,任务结束")
|
789 |
+
# raise gr.Error("请输入prompt!")
|
790 |
+
return "请输入prompt!"
|
791 |
+
|
792 |
+
if input_mode == 'template_mode' and (ref_video_path is None or ref_video_path == ''):
|
793 |
+
print(f"request_id: {request_id}, 用户未输入视频,任务结束")
|
794 |
+
# raise gr.Error("请输入视频!")
|
795 |
+
return "请输入视频!"
|
796 |
+
|
797 |
+
ref_video_name = ''
|
798 |
+
if input_mode == 'template_mode' and os.path.exists(ref_video_path):
|
799 |
+
ref_video_name = os.path.basename(ref_video_path)
|
800 |
+
print(f"request_id: {request_id}, ref_video_name = {ref_video_name}")
|
801 |
+
if (prompt_template == '' or prompt_template == None):
|
802 |
+
print(f"request_id: {request_id}, 用户未输入prompt,任务结束")
|
803 |
+
# raise gr.Error("请输入prompt!")
|
804 |
+
return "请输入prompt!"
|
805 |
+
|
806 |
+
user_mode = -1
|
807 |
+
if input_mode == 'template_mode':
|
808 |
+
if (ref_video_name[:len('template_')] == 'template_'):
|
809 |
+
user_mode = 0 # mode 0: image + template video
|
810 |
+
else:
|
811 |
+
user_mode = 1 # mode 1: image + upload video
|
812 |
+
if input_mode == 'prompt_mode':
|
813 |
+
user_mode = 2 # mode 2: image + prompt
|
814 |
+
print(f"request_id: {request_id}, user_mode = {user_mode} (0: image + template video, 1: image + upload video, 2: image + prompt)")
|
815 |
+
|
816 |
+
return ''
|
817 |
+
|
818 |
+
def delete_request_id(self, user_id, request_id, lock=True):
|
819 |
+
if lock:
|
820 |
+
self.lock.acquire()
|
821 |
+
if request_id in self.all_requests:
|
822 |
+
self.all_requests.remove(request_id)
|
823 |
+
if user_id in self.all_user_requests and request_id in self.all_user_requests[user_id]:
|
824 |
+
self.all_user_requests[user_id].remove(request_id)
|
825 |
+
if request_id in self.all_requests_time:
|
826 |
+
del self.all_requests_time[request_id]
|
827 |
+
if lock:
|
828 |
+
self.lock.release()
|
829 |
+
|
830 |
+
def get_ranking_location(self, user_id):
|
831 |
+
if is_wanx_platform:
|
832 |
+
user_id = 'wanx_lab'
|
833 |
+
if user_id is None or user_id == '':
|
834 |
+
user_id = 'test_version_phone'
|
835 |
+
process_status = ''
|
836 |
+
|
837 |
+
if len(self.all_requests) > 0:
|
838 |
+
for i in range(min(num_instance_dashone, len(self.all_requests))):
|
839 |
+
req = self.all_requests[i]
|
840 |
+
waste_time = 10000
|
841 |
+
if req in self.all_requests_time:
|
842 |
+
endt = time.time()
|
843 |
+
startt = self.all_requests_time[req]
|
844 |
+
waste_time = (endt - startt)/60
|
845 |
+
if waste_time > avg_process_time + 1:
|
846 |
+
data = '{"header":{"request_id":"","service_id":"","task_id":""},"payload":{"input": {"ref_image_path": "", "ref_video_path": "", "ref_video_name": "", "input_prompt": "", "prompt_template": "", "scale_depth": 0.7, "scale_pose": 0.5},"parameters":{}}}'
|
847 |
+
data = json.loads(data) # string to dict
|
848 |
+
data['header']['service_id'] = DASHONE_SERVICE_ID
|
849 |
+
data['header']['request_id'] = req
|
850 |
+
data['header']['task_id'] = req
|
851 |
+
data['header']['attributes'] = {}
|
852 |
+
data['header']['attributes']['user_id'] = user_id
|
853 |
+
data['payload']['input']['user_id'] = user_id
|
854 |
+
data = json.dumps(data) # to string
|
855 |
+
ret_status, ret_json = query_video_generation(request_id=req, data=data)
|
856 |
+
# print(f'ret_json = {ret_json}')
|
857 |
+
if ret_status == "SUCCESS" or ret_status == "FAILED":
|
858 |
+
if req in self.all_requests:
|
859 |
+
self.all_requests.remove(req) # delete request_id
|
860 |
+
if req in self.all_requests_time:
|
861 |
+
del self.all_requests_time[req]
|
862 |
+
else:
|
863 |
+
break
|
864 |
+
else:
|
865 |
+
break
|
866 |
+
else:
|
867 |
+
print(f'size of all_requests is empty.')
|
868 |
+
|
869 |
+
if user_id not in self.all_user_requests:
|
870 |
+
return f'您没有发起视频生成任务。', ''
|
871 |
+
|
872 |
+
if len(self.all_user_requests[user_id]) == 0:
|
873 |
+
return f'您没有正在处理中的任务,排队人数:{len(self.all_requests)}', ''
|
874 |
+
else:
|
875 |
+
self.lock.acquire()
|
876 |
+
lenn = len(self.all_user_requests[user_id])
|
877 |
+
if lenn > 1:
|
878 |
+
for j in range(lenn - 1):
|
879 |
+
req = self.all_user_requests[user_id][j]
|
880 |
+
self.delete_request_id(user_id, req, lock=False) # delete request_id
|
881 |
+
request_id = self.all_user_requests[user_id][0]
|
882 |
+
self.lock.release()
|
883 |
+
|
884 |
+
data = '{"header":{"request_id":"","service_id":"","task_id":""},"payload":{"input": {"ref_image_path": "", "ref_video_path": "", "ref_video_name": "", "input_prompt": "", "prompt_template": "", "scale_depth": 0.7, "scale_pose": 0.5},"parameters":{}}}'
|
885 |
+
data = json.loads(data) # string to dict
|
886 |
+
data['header']['service_id'] = DASHONE_SERVICE_ID
|
887 |
+
data['header']['request_id'] = request_id
|
888 |
+
data['header']['task_id'] = request_id
|
889 |
+
data['header']['attributes'] = {}
|
890 |
+
data['header']['attributes']['user_id'] = user_id
|
891 |
+
data['payload']['input']['user_id'] = user_id
|
892 |
+
data = json.dumps(data) # to string
|
893 |
+
ret_status, ret_json = query_video_generation(request_id=request_id, data=data)
|
894 |
+
print(f'ret_json = {ret_json}')
|
895 |
+
if ret_status == "SUCCESS":
|
896 |
+
req = self.all_user_requests[user_id][0]
|
897 |
+
self.delete_request_id(user_id, req) # delete request_id
|
898 |
+
return '您的视频已生成完毕。', ''
|
899 |
+
elif ret_status == "FAILED":
|
900 |
+
req = self.all_user_requests[user_id][0]
|
901 |
+
self.delete_request_id(user_id, req) # delete request_id
|
902 |
+
# if ret_json['header']['status_code'] == 200:
|
903 |
+
# if ret_json['header']['status_code'][]
|
904 |
+
return '您的视频生成失败,可以尝试打开“卡通视频生成”选项。', ''
|
905 |
+
else:
|
906 |
+
process_status = 'runing'
|
907 |
+
self.lock.acquire()
|
908 |
+
tmp_all_requests = self.all_requests.copy()
|
909 |
+
tmp_all_requests_time = self.all_requests_time.copy()
|
910 |
+
self.lock.release()
|
911 |
+
for i in range(len(tmp_all_requests)):
|
912 |
+
if tmp_all_requests[i] == request_id:
|
913 |
+
index = i + 1
|
914 |
+
# 计算剩余时间
|
915 |
+
endt = time.time()
|
916 |
+
rest_time_list = []
|
917 |
+
for k in range(i+1):
|
918 |
+
reqestid = tmp_all_requests[k]
|
919 |
+
startt = tmp_all_requests_time[reqestid] # 开始时间
|
920 |
+
wast_time = endt-startt # 已经用时
|
921 |
+
rest_time = max(0, avg_process_time * 60 - wast_time) # 处理完毕的剩余时间
|
922 |
+
rest_time_list.append(rest_time)
|
923 |
+
# max_ = max(rest_time) # 最大的一个剩余时间
|
924 |
+
sorted_time = sorted(rest_time_list, reverse=False) # 剩余时间从小到大排序
|
925 |
+
|
926 |
+
print(f'rest_time_list: {rest_time_list}')
|
927 |
+
print(f'sorted_time: {sorted_time}')
|
928 |
+
|
929 |
+
process_time = 0
|
930 |
+
if index <= num_instance_dashone:
|
931 |
+
process_time = rest_time_list[i]/60
|
932 |
+
return f'正在处理您的视频生成任务,需等待{process_time:.1f}分钟左右,点击刷新获取最新生成进度。', process_status
|
933 |
+
else:
|
934 |
+
rounds_to_wait = index // num_instance_dashone
|
935 |
+
rounds_to_rest = index % num_instance_dashone - 1
|
936 |
+
process_time = rounds_to_wait * avg_process_time + sorted_time[rounds_to_rest]/60
|
937 |
+
return f'您前面有{index-1}个任务在排队,需等待{process_time:.1f}分钟左右,点击刷新获取最新生成进度。', process_status
|
938 |
+
return f'您没有正在处理中的任务。', process_status
|
939 |
+
|
940 |
+
|
941 |
+
def click_button_mock_test(self, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt='', prompt_template='',model_id=False):
|
942 |
+
oss_file_path = "video_generation/Service/20231210/20231210-150105-570573-UHKVWH/result.mp4"
|
943 |
+
oss_path = "oss://vigen-invi/" + oss_file_path
|
944 |
+
style = "video/snapshot,t_1000,f_jpg,w_544,h_768,m_fast"
|
945 |
+
params = {'x-oss-process': style}
|
946 |
+
_, snapshot_image = self.oss_service.sign(oss_path, timeout=3600*100, params=params)
|
947 |
+
_, video_url = self.oss_service.sign(oss_path, timeout=3600*100)
|
948 |
+
return video_url, snapshot_image
|
949 |
+
|