Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,27 +23,45 @@ def get_md5(content):
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
min_resolution = 512
|
| 30 |
-
|
|
|
|
|
|
|
| 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)
|
| 38 |
print(f"Face detection info: {face_info}")
|
| 39 |
|
|
|
|
|
|
|
| 40 |
if face_info['face_num'] > 0:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
img_path
|
|
|
|
|
|
|
| 44 |
os.makedirs(os.path.dirname(res_video_path), exist_ok=True)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
pipe.process(
|
| 48 |
img_path,
|
| 49 |
audio_path,
|
|
@@ -56,11 +74,6 @@ def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
|
|
| 56 |
else:
|
| 57 |
return -1
|
| 58 |
|
| 59 |
-
tmp_path = './tmp_path/'
|
| 60 |
-
res_path = './res_path/'
|
| 61 |
-
os.makedirs(tmp_path, exist_ok=True)
|
| 62 |
-
os.makedirs(res_path, exist_ok=True)
|
| 63 |
-
|
| 64 |
def process_sonic(image, audio, dynamic_scale):
|
| 65 |
# 입력 검증
|
| 66 |
if image is None:
|
|
@@ -76,7 +89,7 @@ def process_sonic(image, audio, dynamic_scale):
|
|
| 76 |
if len(arr.shape) == 1:
|
| 77 |
arr = arr[:, None]
|
| 78 |
|
| 79 |
-
# numpy array
|
| 80 |
audio_segment = AudioSegment(
|
| 81 |
arr.tobytes(),
|
| 82 |
frame_rate=sampling_rate,
|
|
@@ -90,13 +103,13 @@ def process_sonic(image, audio, dynamic_scale):
|
|
| 90 |
audio_path = os.path.abspath(os.path.join(tmp_path, f'{audio_md5}.wav'))
|
| 91 |
res_video_path = os.path.abspath(os.path.join(res_path, f'{img_md5}_{audio_md5}_{dynamic_scale}.mp4'))
|
| 92 |
|
| 93 |
-
#
|
| 94 |
if not os.path.exists(image_path):
|
| 95 |
image.save(image_path)
|
| 96 |
if not os.path.exists(audio_path):
|
| 97 |
audio_segment.export(audio_path, format="wav")
|
| 98 |
|
| 99 |
-
# 캐시된 결과가 있으면
|
| 100 |
if os.path.exists(res_video_path):
|
| 101 |
print(f"Using cached result: {res_video_path}")
|
| 102 |
return res_video_path
|
|
@@ -104,7 +117,7 @@ def process_sonic(image, audio, dynamic_scale):
|
|
| 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,5 +214,5 @@ with gr.Blocks(css=css) as demo:
|
|
| 201 |
</div>
|
| 202 |
""")
|
| 203 |
|
| 204 |
-
# 공개 링크
|
| 205 |
demo.launch(share=True)
|
|
|
|
| 23 |
md5hash = hashlib.md5(content)
|
| 24 |
return md5hash.hexdigest()
|
| 25 |
|
| 26 |
+
tmp_path = './tmp_path/'
|
| 27 |
+
res_path = './res_path/'
|
| 28 |
+
os.makedirs(tmp_path, exist_ok=True)
|
| 29 |
+
os.makedirs(res_path, exist_ok=True)
|
| 30 |
+
|
| 31 |
@spaces.GPU(duration=300) # 긴 비디오 처리를 위해 duration 300초로 설정
|
| 32 |
def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
|
| 33 |
+
# ============================
|
| 34 |
+
# 1) 4초(프레임 50)로 늘리기
|
| 35 |
+
# 2) 원본 비율 유지(크롭 제거)
|
| 36 |
+
# ============================
|
| 37 |
+
|
| 38 |
+
## 수정됨: expand_ratio를 0으로 (기존 0.5)
|
| 39 |
+
expand_ratio = 0.0
|
| 40 |
+
|
| 41 |
min_resolution = 512
|
| 42 |
+
|
| 43 |
+
## 수정됨: 4초 분량 = 50프레임
|
| 44 |
+
inference_steps = 50 # 기존 25 -> 50
|
| 45 |
|
|
|
|
| 46 |
audio = AudioSegment.from_file(audio_path)
|
| 47 |
duration = len(audio) / 1000.0 # 초 단위
|
| 48 |
print(f"Audio duration: {duration} seconds, using inference_steps: {inference_steps}")
|
| 49 |
|
| 50 |
+
# 얼굴 인식 (face_info는 참고용)
|
| 51 |
face_info = pipe.preprocess(img_path, expand_ratio=expand_ratio)
|
| 52 |
print(f"Face detection info: {face_info}")
|
| 53 |
|
| 54 |
+
# 얼굴이 하나라도 검출되면(>0), 기존에는 크롭 과정을 진행했으나,
|
| 55 |
+
# 원본 이미지 비율 유지를 위해 크롭 부분 제거
|
| 56 |
if face_info['face_num'] > 0:
|
| 57 |
+
## 수정됨: 아래 3줄 크롭 코드 제거
|
| 58 |
+
# crop_image_path = img_path + '.crop.png'
|
| 59 |
+
# pipe.crop_image(img_path, crop_image_path, face_info['crop_bbox'])
|
| 60 |
+
# img_path = crop_image_path
|
| 61 |
+
|
| 62 |
os.makedirs(os.path.dirname(res_video_path), exist_ok=True)
|
| 63 |
|
| 64 |
+
# 원본 이미지를 그대로 전달
|
| 65 |
pipe.process(
|
| 66 |
img_path,
|
| 67 |
audio_path,
|
|
|
|
| 74 |
else:
|
| 75 |
return -1
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def process_sonic(image, audio, dynamic_scale):
|
| 78 |
# 입력 검증
|
| 79 |
if image is None:
|
|
|
|
| 89 |
if len(arr.shape) == 1:
|
| 90 |
arr = arr[:, None]
|
| 91 |
|
| 92 |
+
# numpy array -> AudioSegment 변환
|
| 93 |
audio_segment = AudioSegment(
|
| 94 |
arr.tobytes(),
|
| 95 |
frame_rate=sampling_rate,
|
|
|
|
| 103 |
audio_path = os.path.abspath(os.path.join(tmp_path, f'{audio_md5}.wav'))
|
| 104 |
res_video_path = os.path.abspath(os.path.join(res_path, f'{img_md5}_{audio_md5}_{dynamic_scale}.mp4'))
|
| 105 |
|
| 106 |
+
# 이미지/오디오 파일 캐싱
|
| 107 |
if not os.path.exists(image_path):
|
| 108 |
image.save(image_path)
|
| 109 |
if not os.path.exists(audio_path):
|
| 110 |
audio_segment.export(audio_path, format="wav")
|
| 111 |
|
| 112 |
+
# 캐시된 결과가 있으면 바로 사용, 없으면 새로 생성
|
| 113 |
if os.path.exists(res_video_path):
|
| 114 |
print(f"Using cached result: {res_video_path}")
|
| 115 |
return res_video_path
|
|
|
|
| 117 |
print(f"Generating new video with dynamic scale: {dynamic_scale}")
|
| 118 |
return get_video_res(image_path, audio_path, res_video_path, dynamic_scale)
|
| 119 |
|
| 120 |
+
# 예시 데이터를 위한 dummy 함수 (필요시 실제 예시 데이터 추가)
|
| 121 |
def get_example():
|
| 122 |
return []
|
| 123 |
|
|
|
|
| 214 |
</div>
|
| 215 |
""")
|
| 216 |
|
| 217 |
+
# 공개 링크 생성
|
| 218 |
demo.launch(share=True)
|