openfree commited on
Commit
244a523
·
verified ·
1 Parent(s): 729c163

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -8,7 +8,7 @@ from sonic import Sonic
8
  from PIL import Image
9
  import torch
10
 
11
- # Initialize the model
12
  cmd = (
13
  'python3 -m pip install "huggingface_hub[cli]"; '
14
  'huggingface-cli download LeonJoe13/Sonic --local-dir checkpoints; '
@@ -23,17 +23,15 @@ def get_md5(content):
23
  md5hash = hashlib.md5(content)
24
  return md5hash.hexdigest()
25
 
26
- @spaces.GPU(duration=300) # Increased duration to handle longer videos
27
  def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
28
  expand_ratio = 0.5
29
  min_resolution = 512
30
- fps = 25 # 원하는 프레임 레이트 설정 (예: 25 fps)
31
 
32
- # 오디오 파일로부터 실제 오디오 길이를 구하고, 그에 맞춰 추론 단계를 계산합니다.
33
  audio = AudioSegment.from_file(audio_path)
34
  duration = len(audio) / 1000.0 # 초 단위
35
- # 오디오 길이에 따른 프레임 수 계산 (예: 5초 -> 5*25 = 125 단계)
36
- inference_steps = int(duration * fps)
37
  print(f"Audio duration: {duration} seconds, using inference_steps: {inference_steps}")
38
 
39
  face_info = pipe.preprocess(img_path, expand_ratio=expand_ratio)
@@ -45,16 +43,15 @@ def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
45
  img_path = crop_image_path
46
  os.makedirs(os.path.dirname(res_video_path), exist_ok=True)
47
 
48
- # Sonic.process() 호출 시, 동적으로 계산된 inference_steps를 전달합니다.
49
  pipe.process(
50
- img_path,
51
- audio_path,
52
- res_video_path,
53
  min_resolution=min_resolution,
54
  inference_steps=inference_steps,
55
  dynamic_scale=dynamic_scale
56
  )
57
- # 생성된 비디오 파일 경로 반환
58
  return res_video_path
59
  else:
60
  return -1
@@ -107,7 +104,7 @@ def process_sonic(image, audio, dynamic_scale):
107
  print(f"Generating new video with dynamic scale: {dynamic_scale}")
108
  return get_video_res(image_path, audio_path, res_video_path, dynamic_scale)
109
 
110
- # 예시 데이터를 위한 dummy 함수 (필요시 실제 예시 데이터로 수정)
111
  def get_example():
112
  return []
113
 
@@ -204,5 +201,5 @@ with gr.Blocks(css=css) as demo:
204
  </div>
205
  """)
206
 
207
- # 공개 링크를 생성하려면 share=True 옵션 사용
208
  demo.launch(share=True)
 
8
  from PIL import Image
9
  import torch
10
 
11
+ # 모델 초기화
12
  cmd = (
13
  'python3 -m pip install "huggingface_hub[cli]"; '
14
  'huggingface-cli download LeonJoe13/Sonic --local-dir checkpoints; '
 
23
  md5hash = hashlib.md5(content)
24
  return md5hash.hexdigest()
25
 
26
+ @spaces.GPU(duration=300) # 비디오 처리를 위해 duration 300초로 설정
27
  def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
28
  expand_ratio = 0.5
29
  min_resolution = 512
30
+ inference_steps = 25 # 2초 분량의 비디오(25 프레임)로 고정
31
 
32
+ # 오디오 길이(참고용) 출력
33
  audio = AudioSegment.from_file(audio_path)
34
  duration = len(audio) / 1000.0 # 초 단위
 
 
35
  print(f"Audio duration: {duration} seconds, using inference_steps: {inference_steps}")
36
 
37
  face_info = pipe.preprocess(img_path, expand_ratio=expand_ratio)
 
43
  img_path = crop_image_path
44
  os.makedirs(os.path.dirname(res_video_path), exist_ok=True)
45
 
46
+ # 고정된 inference_steps(25) 비디오 생성
47
  pipe.process(
48
+ img_path,
49
+ audio_path,
50
+ res_video_path,
51
  min_resolution=min_resolution,
52
  inference_steps=inference_steps,
53
  dynamic_scale=dynamic_scale
54
  )
 
55
  return res_video_path
56
  else:
57
  return -1
 
104
  print(f"Generating new video with dynamic scale: {dynamic_scale}")
105
  return get_video_res(image_path, audio_path, res_video_path, dynamic_scale)
106
 
107
+ # 예시 데이터를 위한 dummy 함수 (필요시 실제 예시 데이터를 추가하세요)
108
  def get_example():
109
  return []
110
 
 
201
  </div>
202
  """)
203
 
204
+ # 공개 링크 생성: share=True
205
  demo.launch(share=True)