Spaces:
Runtime error
Runtime error
File size: 39,776 Bytes
005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c 9d30ad2 005028c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
import os
import sys
from datetime import datetime as Datetime
import gradio as gr
import json
import requests
import threading
import time
import hashlib
sys.path.append('./')
from oss_utils import *
from myconfigs import *
from cache_util import RedisCache
scale_depth = 0.7
scale_pose = 0.5
def md5_hash_file(filename):
"""Compute the MD5 hash of the contents of the given file."""
# Create a new MD5 hash object
md5_hash = hashlib.md5()
# Open the file in binary mode and read chunks
with open(filename, 'rb') as f:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: f.read(4096), b""):
md5_hash.update(byte_block)
# Return the hexadecimal digest of the hash
return md5_hash.hexdigest()
def get_dirnames(filePath='', tail=".mp4", reserve_num=-1):
if not os.path.isdir(filePath):
return []
if len(tail) <= 0:
return []
len_tail = len(tail)
lists = os.listdir(filePath)
file_list = []
for i in range(len(lists)):
cur_file = lists[i]
# if os.path.isfile(cur_file):
if len(cur_file) > 4 and cur_file[-len_tail:] == tail:
full_file_path = os.path.join(filePath, cur_file)
file_list.append(full_file_path)
sorted_list = sorted(file_list, key=lambda x: os.path.getctime(x))
sorted_list = sorted_list[::-1]
if reserve_num > 0:
for i in range(len(sorted_list)):
if i >= reserve_num:
os.remove(sorted_list[i])
return sorted_list
def sync_request_local(request_id, data):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
# "Authorization": APP_AUTH_TEXTURE,
# "X-DashScope-Async": "enable",
# "X-DashScope-DataInspection": "enable"
}
url_create_task = 'http://0.0.0.0:8000/api'
print(f"request_id: {request_id}, request type: video generation, json input: {data}")
res_ = requests.post(url_create_task, data=data, headers=headers)
res = json.loads(res_.content.decode())
result_video_url = ''
if res['header']['status_name'] == 'Success':
result_video_url = res['payload']['output']['res_video_path']
print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {result_video_url}")
else:
print(f"request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_video_url}")
return result_video_url
def sync_request_cartoon(request_id, data):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": EAS_AUTH_CARTOONRECOG,
# "X-DashScope-Async": "enable",
# "X-DashScope-DataInspection": "enable"
}
url_create_task = 'http://1096433202046721.cn-shanghai.pai-eas.aliyuncs.com/api/predict/videogene_supp/api'
print(f"request_id: {request_id}, request type: cartoon recognize, json input: {data}")
res_ = requests.post(url_create_task, data=data, headers=headers)
# print(res_)
# print(res_.content)
res = json.loads(res_.content.decode())
cartoon_recog = ''
if res['payload']['output']['error_message'] == 'Success':
cartoon_recog = res['payload']['output']['key']['label']
# print(f"request_id: {request_id} cartoon_recog: {cartoon_recog}")
print(f"request_id: {request_id}, request type: cartoon recognize, retuen message: Succees, result: {cartoon_recog}")
else:
print(f"request_id: {request_id}, request type: cartoon recognize, retuen message: Faild, result: {cartoon_recog}")
return cartoon_recog
def sync_request_translate_en2cn(request_id, data):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": EAS_AUTH_CARTOONRECOG,
# "X-DashScope-Async": "enable",
# "X-DashScope-DataInspection": "enable"
}
url_create_task = 'http://1096433202046721.cn-shanghai.pai-eas.aliyuncs.com/api/predict/videogene_supp/api'
print(f"[{request_id}], request type: translate en2cn, json input: {data}")
res_ = requests.post(url_create_task, data=data, headers=headers)
# print(res_)
# print(res_.content)
res = json.loads(res_.content.decode())
translate_cn = ''
if res['payload']['output']['error_message'] == 'Success':
translate_cn = res['payload']['output']['key']
# print(f"{request_id} translate_cn: {translate_cn}")
print(f"[{request_id}], request type: translate en2cn, retuen message: Succees, result: {translate_cn}")
else:
print(f"[{request_id}], request type: translate en2cn, retuen message: Faild, result: {translate_cn}")
return translate_cn
def async_request_and_query(request_id, data):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
# "Authorization": APP_AUTH_TEXTURE,
# "X-DashScope-Async": "enable",
# "X-DashScope-DataInspection": "enable"
}
# 1. async request
url_create_task = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/submit'
print(f"request_id: {request_id}, request type: video generation, json input: {data}")
res_ = requests.post(url_create_task, data=data, headers=headers)
# print("res_=", res_)
# print(res_.content)
result_json = json.loads(res_.content.decode("utf-8"))
# 2.async query
is_running = True
running_print_count = 0
res_video_path = None
while is_running:
url_query = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/query-result'
res_ = requests.post(url_query, data=data, headers=headers)
respose_code = res_.status_code
if 200 == respose_code:
res = json.loads(res_.content.decode())
if "SUCCESS" == res['header']['task_status']:
if 200 == res['payload']['output']['error_code']:
res_video_path = res['payload']['output']['res_video_path']
print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {res_video_path}")
break
else:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
# raise gr.Error(f'algo error.')
break
elif "RUNNING" == res['header']['task_status']:
if running_print_count == 0:
print(f'request_id: {request_id}, request type: video generation, retuen message: running..., result: {result_json}')
running_print_count += 1
time.sleep(1)
elif "FAILED" == res['header']['task_status']:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
# raise gr.Error(f'query result faild.')
break
else:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
# raise gr.Error(f'query result faild.')
break
else:
print(f'request_id: {request_id}: Fail to query task result: {res_.content}')
# raise gr.Error("Fail to query task result.")
break
return res_video_path
def async_request_video_generation(request_id, data):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
# "Authorization": APP_AUTH_TEXTURE,
# "X-DashScope-Async": "enable",
# "X-DashScope-DataInspection": "enable"
}
# async request
url_create_task = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/submit'
print(f"request_id: {request_id}, request type: video generation, json input: {data}")
res_ = requests.post(url_create_task, data=data, headers=headers)
# print("res_=", res_)
# print(res_.content)
# result_json = json.loads(res_.content.decode("utf-8"))
def async_query_video_generation(request_id, data):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
# "Authorization": APP_AUTH_TEXTURE,
# "X-DashScope-Async": "enable",
# "X-DashScope-DataInspection": "enable"
}
# async query
is_running = True
running_print_count = 0
res_video_path = None
while is_running:
url_query = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/query-result'
res_ = requests.post(url_query, data=data, headers=headers)
respose_code = res_.status_code
if 200 == respose_code:
res = json.loads(res_.content.decode())
if "SUCCESS" == res['header']['task_status']:
if 200 == res['payload']['output']['error_code']:
res_video_path = res['payload']['output']['res_video_path']
print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {res_video_path}")
break
else:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
# raise gr.Error(f'algo error.')
break
elif "RUNNING" == res['header']['task_status']:
if running_print_count == 0:
print(f'request_id: {request_id}, request type: video generation, retuen message: running..., result: {res}')
running_print_count += 1
time.sleep(1)
elif "FAILED" == res['header']['task_status']:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
break
else:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
break
else:
print(f'request_id: {request_id}: Fail to query task result: {res_.content}')
# raise gr.Error("Fail to query task result.")
break
return res_video_path
def query_video_generation(request_id, data):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
# "Authorization": APP_AUTH_TEXTURE,
# "X-DashScope-Async": "enable",
# "X-DashScope-DataInspection": "enable"
}
# async query
is_running = True
running_print_count = 0
res_video_path = None
# while is_running:
url_query = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/query-result'
res_ = requests.post(url_query, data=data, headers=headers)
respose_code = res_.status_code
res = json.loads(res_.content.decode())
if 200 == respose_code:
if "SUCCESS" == res['header']['task_status']:
if 200 == res['payload']['output']['error_code']:
res_video_path = res['payload']['output']['res_video_path']
print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {res_video_path}")
return "SUCCESS", res
else:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
return "FAILED", res
elif "RUNNING" == res['header']['task_status']:
print(f'request_id: {request_id}, request type: video generation, retuen message: running..., result: {res}')
return "RUNNING", res
elif "FAILED" == res['header']['task_status']:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
return "FAILED", res
else:
print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {res}')
return "FAILED", res
else:
print(f'request_id: {request_id}: Fail to query task result: {res_.content}')
return "FAILED", res
def sync_request_prompt_caption(request_id, data):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": EAS_AUTH_PROMPT,
# "X-DashScope-Async": "enable",
# "X-DashScope-DataInspection": "enable"
}
url_create_task = 'http://1096433202046721.cn-shanghai.pai-eas.aliyuncs.com/api/predict/videogene_supp_gu50/api'
print(f"request_id: {request_id}, request type: prompt_caption, json input: {data}")
res_ = requests.post(url_create_task, data=data, headers=headers)
# print(res_)
# print(res_.content)
res = json.loads(res_.content.decode())
prompt_caption = ''
gender = ''
if res['payload']['output']['error_message'] == 'Success':
key = res['payload']['output']['key'] # {"gender":"female","prompt":"Asian woman, 25-35, long black hair, dark brown eyes, average height","style":""}
gender = key['gender']
prompt_caption = key['prompt']
style = key['style']
print(f"request_id: {request_id}, request type: prompt_caption, retuen message: Succees, result: {prompt_caption}")
else:
print(f"request_id: {request_id}, request type: prompt_caption, retuen message: Faild, result: {prompt_caption}")
return prompt_caption, gender, style
import re
def extract_mp4_filename(input_string):
# Regular expression pattern to match the file name
pattern = r"template_\d+\.mp4"
# Extract the MP4 file name
match = re.search(pattern, input_string)
if match:
return match.group()
else:
return None # or you could raise an exception or return an empty string
class HumanGenService:
def __init__(self):
self.oss_service = ossService()
self.all_user_requests = {}
self.all_requests = []
self.all_requests_time = {} # dict: request_id, time
self.lock = threading.Lock()
def translate_en2cn(self, request_id, input_prompt):
#--------------- translate service -----------------#
translate_data = {}
translate_data['header'] = {}
translate_data['header']['request_id'] = request_id
translate_data['header']['service_id'] = ''
translate_data['header']['task_id'] = request_id
translate_data['header']['attributes'] = {}
translate_data['header']['attributes']['user_id'] = ''
translate_data['payload'] = {}
translate_data['payload']['input'] = {}
translate_data['payload']['input']['work_type'] = 'translate_en2zh'
translate_data['payload']['input']['key'] = input_prompt
translate_data['payload']['parameters'] = {}
translate_data = json.dumps(translate_data) # to string
# serving api
# print("input_prompt: ", input_prompt)
translate_cn = sync_request_translate_en2cn(request_id=request_id, data=translate_data)
# print("translate_cn: ", translate_cn)
#--------------- translate service -----------------#
print(f'[{request_id}] - [HumanGen] - translate ok')
return translate_cn
def click_button_prompt(self, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt='', prompt_template='',model_id=False):
cartoon_recog = 'realhuman' # input infer mode id 'face' and 'clothes'
if model_id is False:
cartoon_recog = 'realhuman'
elif model_id is True:
cartoon_recog = 'cartoon'
if input_mode == 'prompt_mode':
return input_prompt
if cartoon_recog == 'cartoon':
return prompt_template
print(f"request_id: {request_id}, input_mode: {input_mode}")
print(f"request_id: {request_id}, cartoon_recog: {cartoon_recog}")
print(f"request_id: {request_id}, ref_image_path: {ref_image_path}")
print(f"request_id: {request_id}, ref_video_path: {ref_video_path}")
if input_mode == 'template_mode' and cartoon_recog == 'realhuman' and (ref_video_path is None or ref_video_path == ''):
print(f"request_id: {request_id}, No video input, prompt caption over")
raise gr.Error("No video input!")
# return "请输入视频!"
ref_video_name = ''
user_mode = -1
if input_mode == 'template_mode':
import re
# pattern = r"template_\d+\.mp4"
pattern = r"template_.*\.mp4"
# Extract the MP4 file name
match = re.search(pattern, ref_video_path)
if match:
ref_video_name = match.group()
user_mode = 0 # mode 0: image + template video
else:
user_mode = 1 # mode 1: image + upload video
if input_mode == 'prompt_mode':
user_mode = 2 # mode 2: image + prompt
if user_mode == 0 and cartoon_recog == 'realhuman':
return prompt_template
date_string = datetime.datetime.now().strftime('%Y-%m-%d')
img1_oss_path = ''
vid1_oss_path = ''
try:
if input_mode == 'template_mode' and cartoon_recog == 'realhuman':
vid_file_name = os.path.basename(ref_video_path)
vid_extension = os.path.splitext(vid_file_name)[1] # .mp4
vid1_oss_path = self.oss_service.ObjectName + '/Service/' + date_string + '/' + user_id + '/' + request_id + '/' + "ref_video" + vid_extension
is_success1, sign_vid_oss_path = self.oss_service.uploadOssFile(vid1_oss_path, ref_video_path)
print(f"request_id: {request_id}, is_success1={is_success1}, sign_vid_oss_path={sign_vid_oss_path}")
except Exception as e:
print(f"request_id: {request_id}, oss upload error for input local image or video. ")
raise gr.Error("oss upload error for input local image or video")
#-----------------------------prompt caption-----------------------------#
# print("sign_img_oss_path: ", sign_img_oss_path)
# print("sign_img_oss_path1: ", sign_img_oss_path)
prompt_caption_data = {}
prompt_caption_data['header'] = {}
prompt_caption_data['header']['request_id'] = request_id
prompt_caption_data['header']['service_id'] = 'test123'
prompt_caption_data['header']['task_id'] = request_id
prompt_caption_data['header']['attributes'] = {}
prompt_caption_data['header']['attributes']['user_id'] = 'wanx_lab'
prompt_caption_data['payload'] = {}
prompt_caption_data['payload']['input'] = {}
prompt_caption_data['payload']['input']['work_type'] = 'prompt_caption'
sign_vid_oss_path = 'https://vigen-invi.oss-cn-shanghai-internal' + sign_vid_oss_path[len('https://vigen-invi.oss-cn-shanghai'):]
prompt_caption_data['payload']['input']['key'] = sign_vid_oss_path
prompt_caption_data['payload']['parameters'] = {}
prompt_caption_data['payload']['parameters']['input_type'] = 'reference_video' # id_image,cartoon_id_image,reference_video
prompt_caption_data['payload']['parameters']['input_format'] = 'url' # url,oss_path
prompt_caption_data = json.dumps(prompt_caption_data) # to string
# serving api
prompt_caption_en,__,__ = sync_request_prompt_caption(request_id=request_id, data=prompt_caption_data)
print(f"request_id: {request_id}, prompt caption: {prompt_caption_en}")
#-----------------------------prompt caption-----------------------------#
return prompt_caption_en # prompt_caption_cn
def template_video_2_prompt(self, ref_video_name):
file_name = ref_video_name
ref_ori_video_path = ref_video_name
print("videl filename:%s" % ref_video_name)
input_prompt = ""
if file_name[:len('template_')] == 'template_':
file_name = extract_mp4_filename(file_name)
if file_name not in template_prompts:
raise gr.Error("The input video is not a template!")
input_prompt = template_prompts[file_name]
if file_name not in template_orign_videos:
raise gr.Error("The input video is not a template!")
ref_ori_video_path = template_orign_videos[file_name]
else:
print("video file not found:%s" % ref_video_name)
return ref_ori_video_path, input_prompt
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):
start_time = time.time()
if is_wanx_platform:
user_id = 'wanx_lab'
request_id = get_random_string()
print(f"request_id: {request_id}, generate user_id: {user_id} and request_id: {request_id}")
if user_id is None or user_id == '':
user_id = 'test_version_phone'
if request_id is None or request_id == '':
request_id = get_random_string()
# key by: ref_video_name, digest(ref_image_path), prompt_template, input_prompt,
# scale_depth, scale_pose
#print("ref_image_path:%s ref_video_path:%s" % (ref_image_path, ref_video_path) )
cache_key = None
# if ref_image_path and os.path.exists(ref_image_path):
# digest_ref_image = md5_hash_file(ref_image_path)
# if ref_video_path and os.path.exists(ref_video_path):
# ref_video_name = os.path.basename(ref_video_path)
# cache_key = "%s_%s_%s_%s_%s_%s" % (ref_video_name, digest_ref_image, prompt_template, input_prompt, scale_depth, scale_pose)
# print("cache key:%s" % cache_key)
# 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)
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)
# oss_path = "oss://vigen-invi/" + relative_oss_path
# style = "video/snapshot,t_1000,f_jpg,w_544,h_768,m_fast"
# params = {'x-oss-process': style}
# _, snapshot_image = self.oss_service.sign(oss_path, timeout=3600*100, params=params)
# _, video_url = self.oss_service.sign(oss_path, timeout=3600*100)
total_time_minutes = (time.time() -start_time) / 60 # minites
print(f"request_id: {request_id}, Total process time: {total_time_minutes:.1f} minutes")
# return video_url, snapshot_image
#@RedisCache(expire=60*60*24*7) # 7 days
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):
self.lock.acquire()
if user_id in self.all_user_requests and len(self.all_user_requests[user_id]) > 0:
print(f"request_id: {request_id}, You still have unfinished tasks!")
self.lock.release()
raise gr.Error("You still have unfinished tasks!")
# return None
self.all_requests.append(request_id)
if request_id not in self.all_requests_time:
self.all_requests_time[request_id] = time.time()
if user_id not in self.all_user_requests:
self.all_user_requests[user_id] = []
self.all_user_requests[user_id].append(request_id)
else:
self.all_user_requests[user_id].append(request_id)
self.lock.release()
print(f">>>request_id: {request_id}, user_id: {user_id}, add a new task.")
print(f"request_id: {request_id}, start process")
ref_video_name = ''
user_mode = -1
if input_mode == 'template_mode':
import re
# pattern = r"template_\d+\.mp4"
pattern = r"template_.*\.mp4"
# Extract the MP4 file name
match = re.search(pattern, ref_video_path)
if match:
ref_video_name = match.group()
user_mode = 0 # mode 0: image + template video
else:
user_mode = 1 # mode 1: image + upload video
if input_mode == 'prompt_mode':
user_mode = 2 # mode 2: image + prompt
cartoon_recog = 'realhuman' # input infer mode id 'face' and 'clothes'
if model_id is False:
cartoon_recog = 'realhuman'
elif model_id is True:
cartoon_recog = 'cartoon'
# try:
# import shutil
# dir_path = "/tmp/gradio"
# shutil.rmtree(dir_path)
# print(f"目录: {dir_path} 删除成功")
# except OSError as e:
# print(f"目录: {dir_path} 删除失败:", e)
date_string = datetime.datetime.now().strftime('%Y-%m-%d')
img1_oss_path = ''
vid1_oss_path = ''
try:
# ref image
img_file_name = os.path.basename(ref_image_path)
img_extension = os.path.splitext(img_file_name)[1] # 输出:.jpg
img1_oss_path = self.oss_service.ObjectName + '/Service/' + date_string + '/' + user_id + '/' + request_id + '/' + "ref_image" + img_extension
is_success0, sign_img_oss_path = self.oss_service.uploadOssFile(img1_oss_path, ref_image_path)
print(f"request_id: {request_id}, is_success0={is_success0}, sign_img_oss_path={sign_img_oss_path}")
if user_mode == 0 or user_mode == 1:
vid_file_name = os.path.basename(ref_video_path)
vid_extension = os.path.splitext(vid_file_name)[1] # 输出:.mp4
vid1_oss_path = self.oss_service.ObjectName + '/Service/' + date_string + '/' + user_id + '/' + request_id + '/' + "ref_video" + vid_extension
is_success1, sign_vid_oss_path = self.oss_service.uploadOssFile(vid1_oss_path, ref_video_path)
print(f"request_id: {request_id}, is_success1={is_success1}, sign_vid_oss_path={sign_vid_oss_path}")
except Exception as e:
print(f"request_id: {request_id}, Data upload faild!")
raise gr.Error("Data upload faild!")
# return None
#-----------------------------motion generation-----------------------------#
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":{}}}'
data = json.loads(data) # string to dict
data['header']['service_id'] = DASHONE_SERVICE_ID
data['header']['request_id'] = request_id
data['header']['task_id'] = request_id
data['header']['attributes'] = {}
data['header']['attributes']['user_id'] = user_id
data['payload']['input']['user_id'] = user_id
data['payload']['input']['ref_image_path'] = img1_oss_path # sign_img_oss_path
data['payload']['input']['ref_video_path'] = vid1_oss_path # sign_vid_oss_path
data['payload']['input']['ref_video_name'] = ref_video_name
data['payload']['input']['input_prompt'] = input_prompt
data['payload']['input']['prompt_template'] = prompt_template
data['payload']['input']['scale_depth'] = scale_depth
data['payload']['input']['scale_pose'] = scale_pose
data['payload']['input']['cartoon_recog'] = cartoon_recog
data = json.dumps(data) # to string
# serving api
# sign_oss_path = sync_request_local(request_id=request_id, data=data) # sync
try:
async_request_video_generation(request_id=request_id, data=data) # async
print(f"request_id: {request_id}, async_request_video_generation")
except Exception as e:
print(f"request_id: {request_id}, The video generation task has failed, possibly due to a server connection issue, please try again.")
raise gr.Error("The video generation task has failed, possibly due to a server connection issue, please try again.")
# return None
print(f"=================end request_id: {request_id}")
# return sign_oss_path
def valid_check(self, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt='', prompt_template='',model_id=False):
if is_wanx_platform:
user_id = 'wanx_lab'
if user_id is None or user_id == '':
user_id = 'test_version_phone'
if request_id is None or request_id == '':
request_id = get_random_string()
print(f"-----------------request_id: {request_id}, user_id: {user_id}---------------")
self.lock.acquire()
if user_id in self.all_user_requests:
if len(self.all_user_requests[user_id]) > 0:
self.lock.release()
# raise gr.Error("Your video is in the process of being generated. Please wait until the process is complete before submitting a new task. Click refresh to get the latest progress updates.")
return "Your video is in the process of being generated. Please wait until the process is complete before submitting a new task. Click refresh to get the latest progress updates."
self.lock.release()
cartoon_recog = 'realhuman' # input infer mode id 'face' and 'clothes'
if model_id is False:
cartoon_recog = 'realhuman'
elif model_id is True:
cartoon_recog = 'cartoon'
print(f"request_id: {request_id}, input_mode: {input_mode}")
print(f"request_id: {request_id}, ref_image_path: {ref_image_path}")
print(f"request_id: {request_id}, ref_video_path: {ref_video_path}")
print(f"request_id: {request_id}, input_prompt = {input_prompt}")
print(f"request_id: {request_id}, prompt_template = {prompt_template}")
print(f"request_id: {request_id}, scale_depth = {scale_depth}")
print(f"request_id: {request_id}, scale_pose = {scale_pose}")
print(f"request_id: {request_id}, model_id = {model_id}")
print(f"request_id: {request_id}, style: {cartoon_recog}")
if ref_image_path is None or ref_image_path == '' or (not os.path.exists(ref_image_path)):
print(f"request_id: {request_id}, No image input, task over!")
# raise gr.Error("请输入图片!")
return "Please input a image."
if input_mode == 'template_mode' and cartoon_recog == 'realhuman' and (prompt_template == '' or prompt_template == [] or prompt_template is None):
print(f"request_id: {request_id}, No prompt input, task over!")
# raise gr.Error("请输入prompt!")
return "Please input a prompt."
if input_mode == 'prompt_mode' and cartoon_recog == 'realhuman' and (input_prompt == '' or input_prompt == [] or input_prompt is None):
print(f"request_id: {request_id}, No prompt input, task over!")
# raise gr.Error("请输入prompt!")
return "Please input a prompt."
if input_mode == 'template_mode' and (ref_video_path is None or ref_video_path == ''):
print(f"request_id: {request_id}, No video input, task over!")
# raise gr.Error("请输入视频!")
return "Please input a video."
ref_video_name = ''
user_mode = -1
if input_mode == 'template_mode':
import re
# pattern = r"template_\d+\.mp4"
pattern = r"template_.*\.mp4"
# Extract the MP4 file name
match = re.search(pattern, ref_video_path)
if match:
ref_video_name = match.group()
user_mode = 0 # mode 0: image + template video
else:
user_mode = 1 # mode 1: image + upload video
if input_mode == 'prompt_mode':
user_mode = 2 # mode 2: image + prompt
print(f"request_id: {request_id}, user_mode = {user_mode} (0: image + template video, 1: image + upload video, 2: image + prompt)")
return ''
def delete_request_id(self, user_id, request_id, lock=True):
if lock:
self.lock.acquire()
if request_id in self.all_requests:
self.all_requests.remove(request_id)
if user_id in self.all_user_requests and request_id in self.all_user_requests[user_id]:
self.all_user_requests[user_id].remove(request_id)
if request_id in self.all_requests_time:
del self.all_requests_time[request_id]
if lock:
self.lock.release()
def get_ranking_location(self, user_id):
if is_wanx_platform:
user_id = 'wanx_lab'
if user_id is None or user_id == '':
user_id = 'test_version_phone'
process_status = ''
if len(self.all_requests) > 0:
for i in range(min(num_instance_dashone, len(self.all_requests))):
req = self.all_requests[i]
waste_time = 10000
if req in self.all_requests_time:
endt = time.time()
startt = self.all_requests_time[req]
waste_time = (endt - startt)/60
if waste_time > avg_process_time + 1:
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":{}}}'
data = json.loads(data) # string to dict
data['header']['service_id'] = DASHONE_SERVICE_ID
data['header']['request_id'] = req
data['header']['task_id'] = req
data['header']['attributes'] = {}
data['header']['attributes']['user_id'] = user_id
data['payload']['input']['user_id'] = user_id
data = json.dumps(data) # to string
ret_status, ret_json = query_video_generation(request_id=req, data=data)
# print(f'ret_json = {ret_json}')
if ret_status == "SUCCESS" or ret_status == "FAILED":
if req in self.all_requests:
self.all_requests.remove(req) # delete request_id
if req in self.all_requests_time:
del self.all_requests_time[req]
else:
break
else:
break
else:
print(f'size of all_requests is empty.')
if user_id not in self.all_user_requests:
return f'You have not request a video generation task.', ''
if len(self.all_user_requests[user_id]) == 0:
return f'You do not have any tasks in progress, queue status.:{len(self.all_requests)}', ''
else:
self.lock.acquire()
lenn = len(self.all_user_requests[user_id])
if lenn > 1:
for j in range(lenn - 1):
req = self.all_user_requests[user_id][j]
self.delete_request_id(user_id, req, lock=False) # delete request_id
request_id = self.all_user_requests[user_id][0]
self.lock.release()
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":{}}}'
data = json.loads(data) # string to dict
data['header']['service_id'] = DASHONE_SERVICE_ID
data['header']['request_id'] = request_id
data['header']['task_id'] = request_id
data['header']['attributes'] = {}
data['header']['attributes']['user_id'] = user_id
data['payload']['input']['user_id'] = user_id
data = json.dumps(data) # to string
ret_status, ret_json = query_video_generation(request_id=request_id, data=data)
print(f'ret_json = {ret_json}')
if ret_status == "SUCCESS":
req = self.all_user_requests[user_id][0]
self.delete_request_id(user_id, req) # delete request_id
return 'Your video has been successfully generated.', ''
elif ret_status == "FAILED":
req = self.all_user_requests[user_id][0]
self.delete_request_id(user_id, req) # delete request_id
# if ret_json['header']['status_code'] == 200:
# if ret_json['header']['status_code'][]
return 'Your video generation failed, you may try enabling the "Cartoon Video Generation" option.', ''
else:
process_status = 'runing'
self.lock.acquire()
tmp_all_requests = self.all_requests.copy()
tmp_all_requests_time = self.all_requests_time.copy()
self.lock.release()
for i in range(len(tmp_all_requests)):
if tmp_all_requests[i] == request_id:
index = i + 1
endt = time.time()
rest_time_list = []
for k in range(i+1):
reqestid = tmp_all_requests[k]
startt = tmp_all_requests_time[reqestid]
wast_time = endt-startt
rest_time = max(0, avg_process_time * 60 - wast_time)
rest_time_list.append(rest_time)
sorted_time = sorted(rest_time_list, reverse=False)
print(f'rest_time_list: {rest_time_list}')
print(f'sorted_time: {sorted_time}')
process_time = 0
if index <= num_instance_dashone:
process_time = rest_time_list[i]/60
return f'Your video generation task is being processed, please wait for about {process_time:.1f} minutes, click refresh to get the latest progress update.', process_status
else:
rounds_to_wait = index // num_instance_dashone
rounds_to_rest = index % num_instance_dashone - 1
process_time = rounds_to_wait * avg_process_time + sorted_time[rounds_to_rest]/60
return f'There are {index-1} tasks ahead of you in the queue, please wait for about {process_time:.1f} minutes, click refresh to get the latest progress update.', process_status
return f'You do not have any tasks currently being processed.', process_status
|