openfree commited on
Commit
729c163
·
verified ·
1 Parent(s): 406d112

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -27,15 +27,17 @@ def get_md5(content):
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
31
 
32
- # Get audio duration (for logging)
33
  audio = AudioSegment.from_file(audio_path)
34
- duration = len(audio) / 1000.0 # Convert ms to seconds
 
 
 
35
 
36
  face_info = pipe.preprocess(img_path, expand_ratio=expand_ratio)
37
  print(f"Face detection info: {face_info}")
38
- print(f"Audio duration: {duration} seconds")
39
 
40
  if face_info['face_num'] > 0:
41
  crop_image_path = img_path + '.crop.png'
@@ -43,7 +45,7 @@ def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
43
  img_path = crop_image_path
44
  os.makedirs(os.path.dirname(res_video_path), exist_ok=True)
45
 
46
- # Process the video (duration parameter removed)
47
  pipe.process(
48
  img_path,
49
  audio_path,
@@ -52,7 +54,7 @@ def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
52
  inference_steps=inference_steps,
53
  dynamic_scale=dynamic_scale
54
  )
55
- # ★ 수정: 생성된 비디오 파일 경로를 반환하도록 함.
56
  return res_video_path
57
  else:
58
  return -1
@@ -63,7 +65,7 @@ os.makedirs(tmp_path, exist_ok=True)
63
  os.makedirs(res_path, exist_ok=True)
64
 
65
  def process_sonic(image, audio, dynamic_scale):
66
- # Input validation
67
  if image is None:
68
  raise gr.Error("Please upload an image")
69
  if audio is None:
@@ -77,7 +79,7 @@ def process_sonic(image, audio, dynamic_scale):
77
  if len(arr.shape) == 1:
78
  arr = arr[:, None]
79
 
80
- # Create an audio segment from numpy array
81
  audio_segment = AudioSegment(
82
  arr.tobytes(),
83
  frame_rate=sampling_rate,
@@ -86,18 +88,18 @@ def process_sonic(image, audio, dynamic_scale):
86
  )
87
  audio_segment = audio_segment.set_frame_rate(sampling_rate)
88
 
89
- # Generate file paths
90
  image_path = os.path.abspath(os.path.join(tmp_path, f'{img_md5}.png'))
91
  audio_path = os.path.abspath(os.path.join(tmp_path, f'{audio_md5}.wav'))
92
  res_video_path = os.path.abspath(os.path.join(res_path, f'{img_md5}_{audio_md5}_{dynamic_scale}.mp4'))
93
 
94
- # Save input files if they don't exist
95
  if not os.path.exists(image_path):
96
  image.save(image_path)
97
  if not os.path.exists(audio_path):
98
  audio_segment.export(audio_path, format="wav")
99
 
100
- # If cached video exists, return it; otherwise, generate a new one
101
  if os.path.exists(res_video_path):
102
  print(f"Using cached result: {res_video_path}")
103
  return res_video_path
@@ -105,7 +107,7 @@ def process_sonic(image, audio, dynamic_scale):
105
  print(f"Generating new video with dynamic scale: {dynamic_scale}")
106
  return get_video_res(image_path, audio_path, res_video_path, dynamic_scale)
107
 
108
- # Dummy get_example function to prevent errors in examples section
109
  def get_example():
110
  return []
111
 
@@ -173,7 +175,6 @@ with gr.Blocks(css=css) as demo:
173
  elem_id="video_output"
174
  )
175
 
176
- # Process button click: when clicked, process_sonic() is called and its return value is sent to video_output.
177
  process_btn.click(
178
  fn=process_sonic,
179
  inputs=[image_input, audio_input, dynamic_scale],
@@ -181,7 +182,6 @@ with gr.Blocks(css=css) as demo:
181
  api_name="animate"
182
  )
183
 
184
- # Examples section
185
  gr.Examples(
186
  examples=get_example(),
187
  fn=process_sonic,
@@ -190,7 +190,6 @@ with gr.Blocks(css=css) as demo:
190
  cache_examples=False
191
  )
192
 
193
- # Footer with attribution and links
194
  gr.HTML("""
195
  <div style="text-align: center; margin-top: 2em;">
196
  <div style="margin-bottom: 1em;">
@@ -205,5 +204,5 @@ with gr.Blocks(css=css) as demo:
205
  </div>
206
  """)
207
 
208
- # To create a public link, share=True is set.
209
  demo.launch(share=True)
 
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)
40
  print(f"Face detection info: {face_info}")
 
41
 
42
  if face_info['face_num'] > 0:
43
  crop_image_path = img_path + '.crop.png'
 
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,
 
54
  inference_steps=inference_steps,
55
  dynamic_scale=dynamic_scale
56
  )
57
+ # 생성된 비디오 파일 경로 반환
58
  return res_video_path
59
  else:
60
  return -1
 
65
  os.makedirs(res_path, exist_ok=True)
66
 
67
  def process_sonic(image, audio, dynamic_scale):
68
+ # 입력 검증
69
  if image is None:
70
  raise gr.Error("Please upload an image")
71
  if audio is None:
 
79
  if len(arr.shape) == 1:
80
  arr = arr[:, None]
81
 
82
+ # numpy array로부터 AudioSegment 생성
83
  audio_segment = AudioSegment(
84
  arr.tobytes(),
85
  frame_rate=sampling_rate,
 
88
  )
89
  audio_segment = audio_segment.set_frame_rate(sampling_rate)
90
 
91
+ # 파일 경로 생성
92
  image_path = os.path.abspath(os.path.join(tmp_path, f'{img_md5}.png'))
93
  audio_path = os.path.abspath(os.path.join(tmp_path, f'{audio_md5}.wav'))
94
  res_video_path = os.path.abspath(os.path.join(res_path, f'{img_md5}_{audio_md5}_{dynamic_scale}.mp4'))
95
 
96
+ # 입력 파일이 없으면 저장
97
  if not os.path.exists(image_path):
98
  image.save(image_path)
99
  if not os.path.exists(audio_path):
100
  audio_segment.export(audio_path, format="wav")
101
 
102
+ # 캐시된 결과가 있으면 반환, 없으면 새로 생성
103
  if os.path.exists(res_video_path):
104
  print(f"Using cached result: {res_video_path}")
105
  return res_video_path
 
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
 
 
175
  elem_id="video_output"
176
  )
177
 
 
178
  process_btn.click(
179
  fn=process_sonic,
180
  inputs=[image_input, audio_input, dynamic_scale],
 
182
  api_name="animate"
183
  )
184
 
 
185
  gr.Examples(
186
  examples=get_example(),
187
  fn=process_sonic,
 
190
  cache_examples=False
191
  )
192
 
 
193
  gr.HTML("""
194
  <div style="text-align: center; margin-top: 2em;">
195
  <div style="margin-bottom: 1em;">
 
204
  </div>
205
  """)
206
 
207
+ # 공개 링크를 생성하려면 share=True 옵션 사용
208
  demo.launch(share=True)