jiayong commited on
Commit
9d30ad2
1 Parent(s): adba9e7

Update gen_client.py

Browse files
Files changed (1) hide show
  1. gen_client.py +149 -305
gen_client.py CHANGED
@@ -42,10 +42,8 @@ def get_dirnames(filePath='', tail=".mp4", reserve_num=-1):
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:
@@ -101,7 +99,6 @@ def sync_request_cartoon(request_id, data):
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",
@@ -127,7 +124,8 @@ def sync_request_translate_en2cn(request_id, data):
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",
@@ -136,7 +134,7 @@ def async_request_video_generation(request_id, data):
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)
@@ -144,47 +142,46 @@ def async_request_video_generation(request_id, data):
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",
@@ -193,15 +190,24 @@ def async_query_video_generation(request_id, data):
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
@@ -247,15 +253,7 @@ def query_video_generation(request_id, data):
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
@@ -308,15 +306,12 @@ def sync_request_prompt_caption(request_id, data):
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):
@@ -361,6 +356,7 @@ class HumanGenService:
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:
@@ -368,50 +364,47 @@ class HumanGenService:
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}")
@@ -432,49 +425,19 @@ class HumanGenService:
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
@@ -501,6 +464,8 @@ class HumanGenService:
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) )
@@ -522,17 +487,17 @@ class HumanGenService:
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:
@@ -544,17 +509,20 @@ class HumanGenService:
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
@@ -593,89 +561,10 @@ class HumanGenService:
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
@@ -702,48 +591,10 @@ class HumanGenService:
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
 
@@ -753,14 +604,16 @@ class HumanGenService:
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'
@@ -780,32 +633,35 @@ class HumanGenService:
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
@@ -867,10 +723,10 @@ class HumanGenService:
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])
@@ -895,13 +751,13 @@ class HumanGenService:
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()
@@ -911,17 +767,15 @@ class HumanGenService:
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}')
@@ -929,21 +783,11 @@ class HumanGenService:
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
-
 
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
  sorted_list = sorted(file_list, key=lambda x: os.path.getctime(x))
46
+ sorted_list = sorted_list[::-1]
 
47
  if reserve_num > 0:
48
  for i in range(len(sorted_list)):
49
  if i >= reserve_num:
 
99
  print(f"request_id: {request_id}, request type: cartoon recognize, retuen message: Faild, result: {cartoon_recog}")
100
  return cartoon_recog
101
 
 
102
  def sync_request_translate_en2cn(request_id, data):
103
  headers = {
104
  "Content-Type": "application/json",
 
124
  print(f"[{request_id}], request type: translate en2cn, retuen message: Faild, result: {translate_cn}")
125
  return translate_cn
126
 
127
+
128
+ def async_request_and_query(request_id, data):
129
  headers = {
130
  "Content-Type": "application/json",
131
  "Accept": "application/json",
 
134
  # "X-DashScope-DataInspection": "enable"
135
  }
136
 
137
+ # 1. async request
138
  url_create_task = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/submit'
139
  print(f"request_id: {request_id}, request type: video generation, json input: {data}")
140
  res_ = requests.post(url_create_task, data=data, headers=headers)
 
142
  # print(res_.content)
143
  result_json = json.loads(res_.content.decode("utf-8"))
144
 
145
+ # 2.async query
146
+ is_running = True
147
+ running_print_count = 0
148
+ res_video_path = None
149
+ while is_running:
150
+ url_query = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/query-result'
151
+ res_ = requests.post(url_query, data=data, headers=headers)
152
+ respose_code = res_.status_code
153
+ if 200 == respose_code:
154
+ res = json.loads(res_.content.decode())
155
+ if "SUCCESS" == res['header']['task_status']:
156
+ if 200 == res['payload']['output']['error_code']:
157
+ res_video_path = res['payload']['output']['res_video_path']
158
+ print(f"request_id: {request_id}, request type: video generation, retuen message: Succees, result: {res_video_path}")
159
+ break
160
+ else:
161
+ print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
162
+ # raise gr.Error(f'algo error.')
163
+ break
164
+ elif "RUNNING" == res['header']['task_status']:
165
+ if running_print_count == 0:
166
+ print(f'request_id: {request_id}, request type: video generation, retuen message: running..., result: {result_json}')
167
+ running_print_count += 1
168
+ time.sleep(1)
169
+ elif "FAILED" == res['header']['task_status']:
170
+ print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
171
+ # raise gr.Error(f'query result faild.')
172
+ break
173
+ else:
174
+ print(f'request_id: {request_id}, request type: video generation, retuen message: Faild, result: {result_json}')
175
+ # raise gr.Error(f'query result faild.')
176
+ break
177
+ else:
178
+ print(f'request_id: {request_id}: Fail to query task result: {res_.content}')
179
+ # raise gr.Error("Fail to query task result.")
180
+ break
181
+ return res_video_path
 
182
 
183
 
184
+ def async_request_video_generation(request_id, data):
185
  headers = {
186
  "Content-Type": "application/json",
187
  "Accept": "application/json",
 
190
  # "X-DashScope-DataInspection": "enable"
191
  }
192
 
193
+ # async request
194
+ url_create_task = 'http://dashscope-scheduler-perf.1656375133437235.cn-beijing.pai-eas.aliyuncs.com/api/v1/task/submit'
195
+ print(f"request_id: {request_id}, request type: video generation, json input: {data}")
196
+ res_ = requests.post(url_create_task, data=data, headers=headers)
197
+ # print("res_=", res_)
198
+ # print(res_.content)
199
  # result_json = json.loads(res_.content.decode("utf-8"))
200
+
201
+ def async_query_video_generation(request_id, data):
202
+ headers = {
203
+ "Content-Type": "application/json",
204
+ "Accept": "application/json",
205
+ # "Authorization": APP_AUTH_TEXTURE,
206
+ # "X-DashScope-Async": "enable",
207
+ # "X-DashScope-DataInspection": "enable"
208
+ }
209
 
210
+ # async query
211
  is_running = True
212
  running_print_count = 0
213
  res_video_path = None
 
253
  # "X-DashScope-DataInspection": "enable"
254
  }
255
 
256
+ # async query
 
 
 
 
 
 
 
 
257
  is_running = True
258
  running_print_count = 0
259
  res_video_path = None
 
306
  if res['payload']['output']['error_message'] == 'Success':
307
  key = res['payload']['output']['key'] # {"gender":"female","prompt":"Asian woman, 25-35, long black hair, dark brown eyes, average height","style":""}
308
  gender = key['gender']
309
+ prompt_caption = key['prompt']
310
  style = key['style']
 
 
 
311
  print(f"request_id: {request_id}, request type: prompt_caption, retuen message: Succees, result: {prompt_caption}")
312
  else:
313
  print(f"request_id: {request_id}, request type: prompt_caption, retuen message: Faild, result: {prompt_caption}")
314
+ return prompt_caption, gender, style
315
 
316
  import re
317
  def extract_mp4_filename(input_string):
 
356
  print(f'[{request_id}] - [HumanGen] - translate ok')
357
  return translate_cn
358
 
359
+
360
  def click_button_prompt(self, user_id, request_id, input_mode, ref_image_path, ref_video_path, input_prompt='', prompt_template='',model_id=False):
361
  cartoon_recog = 'realhuman' # input infer mode id 'face' and 'clothes'
362
  if model_id is False:
 
364
  elif model_id is True:
365
  cartoon_recog = 'cartoon'
366
 
367
+ if input_mode == 'prompt_mode':
368
+ return input_prompt
369
+ if cartoon_recog == 'cartoon':
370
+ return prompt_template
371
+
372
  print(f"request_id: {request_id}, input_mode: {input_mode}")
373
  print(f"request_id: {request_id}, cartoon_recog: {cartoon_recog}")
374
  print(f"request_id: {request_id}, ref_image_path: {ref_image_path}")
375
  print(f"request_id: {request_id}, ref_video_path: {ref_video_path}")
376
+
 
 
 
 
 
 
 
 
 
 
377
  if input_mode == 'template_mode' and cartoon_recog == 'realhuman' and (ref_video_path is None or ref_video_path == ''):
378
+ print(f"request_id: {request_id}, No video input, prompt caption over")
379
+ raise gr.Error("No video input!")
380
  # return "请输入视频!"
381
+
382
+ ref_video_name = ''
383
+ user_mode = -1
384
+ if input_mode == 'template_mode':
385
+ import re
386
+ # pattern = r"template_\d+\.mp4"
387
+ pattern = r"template_.*\.mp4"
388
+ # Extract the MP4 file name
389
+ match = re.search(pattern, ref_video_path)
390
+ if match:
391
+ ref_video_name = match.group()
392
+ user_mode = 0 # mode 0: image + template video
393
+ else:
394
+ user_mode = 1 # mode 1: image + upload video
395
+ if input_mode == 'prompt_mode':
396
+ user_mode = 2 # mode 2: image + prompt
397
+
398
+ if user_mode == 0 and cartoon_recog == 'realhuman':
399
+ return prompt_template
400
 
401
  date_string = datetime.datetime.now().strftime('%Y-%m-%d')
402
  img1_oss_path = ''
403
  vid1_oss_path = ''
404
  try:
 
 
 
 
 
 
 
 
405
  if input_mode == 'template_mode' and cartoon_recog == 'realhuman':
406
  vid_file_name = os.path.basename(ref_video_path)
407
+ vid_extension = os.path.splitext(vid_file_name)[1] # .mp4
408
  vid1_oss_path = self.oss_service.ObjectName + '/Service/' + date_string + '/' + user_id + '/' + request_id + '/' + "ref_video" + vid_extension
409
  is_success1, sign_vid_oss_path = self.oss_service.uploadOssFile(vid1_oss_path, ref_video_path)
410
  print(f"request_id: {request_id}, is_success1={is_success1}, sign_vid_oss_path={sign_vid_oss_path}")
 
425
  prompt_caption_data['payload'] = {}
426
  prompt_caption_data['payload']['input'] = {}
427
  prompt_caption_data['payload']['input']['work_type'] = 'prompt_caption'
428
+ sign_vid_oss_path = 'https://vigen-invi.oss-cn-shanghai-internal' + sign_vid_oss_path[len('https://vigen-invi.oss-cn-shanghai'):]
429
+ prompt_caption_data['payload']['input']['key'] = sign_vid_oss_path
 
 
 
 
 
 
 
 
430
  prompt_caption_data['payload']['parameters'] = {}
431
+ prompt_caption_data['payload']['parameters']['input_type'] = 'reference_video' # id_image,cartoon_id_image,reference_video
 
 
 
 
 
 
 
 
 
432
  prompt_caption_data['payload']['parameters']['input_format'] = 'url' # url,oss_path
433
  prompt_caption_data = json.dumps(prompt_caption_data) # to string
434
 
435
  # serving api
436
+ prompt_caption_en,__,__ = sync_request_prompt_caption(request_id=request_id, data=prompt_caption_data)
437
+ print(f"request_id: {request_id}, prompt caption: {prompt_caption_en}")
 
 
 
438
  #-----------------------------prompt caption-----------------------------#
439
+ return prompt_caption_en # prompt_caption_cn
440
+
 
 
 
 
 
 
 
 
 
 
441
 
442
  def template_video_2_prompt(self, ref_video_name):
443
  file_name = ref_video_name
 
464
  print(f"request_id: {request_id}, generate user_id: {user_id} and request_id: {request_id}")
465
  if user_id is None or user_id == '':
466
  user_id = 'test_version_phone'
467
+ if request_id is None or request_id == '':
468
+ request_id = get_random_string()
469
  # key by: ref_video_name, digest(ref_image_path), prompt_template, input_prompt,
470
  # scale_depth, scale_pose
471
  #print("ref_image_path:%s ref_video_path:%s" % (ref_image_path, ref_video_path) )
 
487
  # _, video_url = self.oss_service.sign(oss_path, timeout=3600*100)
488
 
489
  total_time_minutes = (time.time() -start_time) / 60 # minites
490
+ print(f"request_id: {request_id}, Total process time: {total_time_minutes:.1f} minutes")
491
 
492
  # return video_url, snapshot_image
493
 
494
+ #@RedisCache(expire=60*60*24*7) # 7 days
495
  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):
496
  self.lock.acquire()
497
  if user_id in self.all_user_requests and len(self.all_user_requests[user_id]) > 0:
498
+ print(f"request_id: {request_id}, You still have unfinished tasks!")
499
  self.lock.release()
500
+ raise gr.Error("You still have unfinished tasks!")
501
  # return None
502
  self.all_requests.append(request_id)
503
  if request_id not in self.all_requests_time:
 
509
  self.all_user_requests[user_id].append(request_id)
510
  self.lock.release()
511
 
512
+ print(f">>>request_id: {request_id}, user_id: {user_id}, add a new task.")
513
 
514
  print(f"request_id: {request_id}, start process")
515
 
516
  ref_video_name = ''
 
 
 
517
  user_mode = -1
518
  if input_mode == 'template_mode':
519
+ import re
520
+ # pattern = r"template_\d+\.mp4"
521
+ pattern = r"template_.*\.mp4"
522
+ # Extract the MP4 file name
523
+ match = re.search(pattern, ref_video_path)
524
+ if match:
525
+ ref_video_name = match.group()
526
  user_mode = 0 # mode 0: image + template video
527
  else:
528
  user_mode = 1 # mode 1: image + upload video
 
561
  is_success1, sign_vid_oss_path = self.oss_service.uploadOssFile(vid1_oss_path, ref_video_path)
562
  print(f"request_id: {request_id}, is_success1={is_success1}, sign_vid_oss_path={sign_vid_oss_path}")
563
  except Exception as e:
564
+ print(f"request_id: {request_id}, Data upload faild!")
565
+ raise gr.Error("Data upload faild!")
566
  # return None
567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
  #-----------------------------motion generation-----------------------------#
569
  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":{}}}'
570
  data = json.loads(data) # string to dict
 
591
  async_request_video_generation(request_id=request_id, data=data) # async
592
  print(f"request_id: {request_id}, async_request_video_generation")
593
  except Exception as e:
594
+ print(f"request_id: {request_id}, The video generation task has failed, possibly due to a server connection issue, please try again.")
595
+ raise gr.Error("The video generation task has failed, possibly due to a server connection issue, please try again.")
596
  # return None
597
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598
  print(f"=================end request_id: {request_id}")
599
  # return sign_oss_path
600
 
 
604
  user_id = 'wanx_lab'
605
  if user_id is None or user_id == '':
606
  user_id = 'test_version_phone'
607
+ if request_id is None or request_id == '':
608
+ request_id = get_random_string()
609
  print(f"-----------------request_id: {request_id}, user_id: {user_id}---------------")
610
 
611
  self.lock.acquire()
612
  if user_id in self.all_user_requests:
613
  if len(self.all_user_requests[user_id]) > 0:
614
  self.lock.release()
615
+ # 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.")
616
+ 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."
617
  self.lock.release()
618
 
619
  cartoon_recog = 'realhuman' # input infer mode id 'face' and 'clothes'
 
633
  print(f"request_id: {request_id}, style: {cartoon_recog}")
634
 
635
  if ref_image_path is None or ref_image_path == '' or (not os.path.exists(ref_image_path)):
636
+ print(f"request_id: {request_id}, No image input, task over!")
637
  # raise gr.Error("请输入图片!")
638
+ return "Please input a image."
639
+
640
+ if input_mode == 'template_mode' and cartoon_recog == 'realhuman' and (prompt_template == '' or prompt_template == [] or prompt_template is None):
641
+ print(f"request_id: {request_id}, No prompt input, task over!")
642
+ # raise gr.Error("请输入prompt!")
643
+ return "Please input a prompt."
644
 
645
+ if input_mode == 'prompt_mode' and cartoon_recog == 'realhuman' and (input_prompt == '' or input_prompt == [] or input_prompt is None):
646
+ print(f"request_id: {request_id}, No prompt input, task over!")
647
  # raise gr.Error("请输入prompt!")
648
+ return "Please input a prompt."
649
 
650
  if input_mode == 'template_mode' and (ref_video_path is None or ref_video_path == ''):
651
+ print(f"request_id: {request_id}, No video input, task over!")
652
  # raise gr.Error("请输入视频!")
653
+ return "Please input a video."
654
 
655
  ref_video_name = ''
 
 
 
 
 
 
 
 
656
  user_mode = -1
657
  if input_mode == 'template_mode':
658
+ import re
659
+ # pattern = r"template_\d+\.mp4"
660
+ pattern = r"template_.*\.mp4"
661
+ # Extract the MP4 file name
662
+ match = re.search(pattern, ref_video_path)
663
+ if match:
664
+ ref_video_name = match.group()
665
  user_mode = 0 # mode 0: image + template video
666
  else:
667
  user_mode = 1 # mode 1: image + upload video
 
723
  print(f'size of all_requests is empty.')
724
 
725
  if user_id not in self.all_user_requests:
726
+ return f'You have not request a video generation task.', ''
727
 
728
  if len(self.all_user_requests[user_id]) == 0:
729
+ return f'You do not have any tasks in progress, queue status.:{len(self.all_requests)}', ''
730
  else:
731
  self.lock.acquire()
732
  lenn = len(self.all_user_requests[user_id])
 
751
  if ret_status == "SUCCESS":
752
  req = self.all_user_requests[user_id][0]
753
  self.delete_request_id(user_id, req) # delete request_id
754
+ return 'Your video has been successfully generated.', ''
755
  elif ret_status == "FAILED":
756
  req = self.all_user_requests[user_id][0]
757
  self.delete_request_id(user_id, req) # delete request_id
758
  # if ret_json['header']['status_code'] == 200:
759
  # if ret_json['header']['status_code'][]
760
+ return 'Your video generation failed, you may try enabling the "Cartoon Video Generation" option.', ''
761
  else:
762
  process_status = 'runing'
763
  self.lock.acquire()
 
767
  for i in range(len(tmp_all_requests)):
768
  if tmp_all_requests[i] == request_id:
769
  index = i + 1
 
770
  endt = time.time()
771
  rest_time_list = []
772
  for k in range(i+1):
773
  reqestid = tmp_all_requests[k]
774
+ startt = tmp_all_requests_time[reqestid]
775
+ wast_time = endt-startt
776
+ rest_time = max(0, avg_process_time * 60 - wast_time)
777
  rest_time_list.append(rest_time)
778
+ sorted_time = sorted(rest_time_list, reverse=False)
 
779
 
780
  print(f'rest_time_list: {rest_time_list}')
781
  print(f'sorted_time: {sorted_time}')
 
783
  process_time = 0
784
  if index <= num_instance_dashone:
785
  process_time = rest_time_list[i]/60
786
+ 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
787
  else:
788
  rounds_to_wait = index // num_instance_dashone
789
  rounds_to_rest = index % num_instance_dashone - 1
790
  process_time = rounds_to_wait * avg_process_time + sorted_time[rounds_to_rest]/60
791
+ 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
792
+ return f'You do not have any tasks currently being processed.', process_status
793