Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -66,6 +66,61 @@ def preprocess_audio(audio_path, target_sr=16000, trim_silence=True, normalize=T
|
|
| 66 |
# fallback: ๊ธฐ์กด ๋ฐฉ์
|
| 67 |
return load_wav(audio_path, target_sr)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def advanced_silence_removal(audio_path, target_sr=16000):
|
| 70 |
"""
|
| 71 |
๋ ์ ๊ตํ ๋ฌด์ ์ ๊ฑฐ
|
|
@@ -555,14 +610,22 @@ async def handle_form(
|
|
| 555 |
if not results:
|
| 556 |
raise Exception("์์ฑ ํฉ์ฑ ๊ฒฐ๊ณผ๊ฐ ๋น์ด์์ต๋๋ค.")
|
| 557 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
# ๊ฒฐ๊ณผ ์ ์ฅ (์ถ๋ ฅ ๋๋ ํ ๋ฆฌ ์ง์ )
|
| 559 |
output_dir = '/app/outputs'
|
| 560 |
os.makedirs(output_dir, exist_ok=True)
|
| 561 |
output_filename = f'korean_tts_output_{hash(text)}.wav'
|
| 562 |
output_path = os.path.join(output_dir, output_filename)
|
| 563 |
|
| 564 |
-
torchaudio.save(output_path,
|
| 565 |
-
print(f"์ค๋์ค ํ์ผ ์ ์ฅ ์๋ฃ: {output_path}")
|
| 566 |
|
| 567 |
# ๋ค์ด๋ก๋์ฉ ์๋ ๊ฒฝ๋ก
|
| 568 |
download_filename = f'outputs/{output_filename}'
|
|
@@ -615,7 +678,7 @@ async def handle_form(
|
|
| 615 |
style="background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
|
| 616 |
color: white; padding: 0.75rem 1.5rem; text-decoration: none;
|
| 617 |
border-radius: 8px; display: inline-block; margin-top: 1rem;">
|
| 618 |
-
|
| 619 |
</a>
|
| 620 |
</div>
|
| 621 |
|
|
|
|
| 66 |
# fallback: ๊ธฐ์กด ๋ฐฉ์
|
| 67 |
return load_wav(audio_path, target_sr)
|
| 68 |
|
| 69 |
+
def postprocess_synthesized_audio(audio_tensor, sample_rate):
|
| 70 |
+
"""
|
| 71 |
+
ํฉ์ฑ๋ ์์ฑ์ ํ์ฒ๋ฆฌ: ๋๋ถ๋ถ ์ํฐํฉํธ ๋ฐ ๋ฌด์ ์ ๊ฑฐ
|
| 72 |
+
"""
|
| 73 |
+
try:
|
| 74 |
+
# tensor๋ฅผ numpy๋ก ๋ณํ
|
| 75 |
+
if len(audio_tensor.shape) > 1:
|
| 76 |
+
audio = audio_tensor.squeeze().numpy()
|
| 77 |
+
else:
|
| 78 |
+
audio = audio_tensor.numpy()
|
| 79 |
+
|
| 80 |
+
# 1. ๋๋ถ๋ถ ์ ์๋ ๊ตฌ๊ฐ ์ ๊ฑฐ (๋ aggressiveํ๊ฒ)
|
| 81 |
+
# ์๋์ง ๊ธฐ๋ฐ์ผ๋ก ์ค์ ์์ฑ ๋ ์ง์ ์ฐพ๊ธฐ
|
| 82 |
+
frame_length = 2048
|
| 83 |
+
hop_length = 512
|
| 84 |
+
|
| 85 |
+
# ๋จ๊ตฌ๊ฐ ์๋์ง ๊ณ์ฐ
|
| 86 |
+
frames = librosa.util.frame(audio, frame_length=frame_length, hop_length=hop_length)
|
| 87 |
+
energy = np.sum(frames**2, axis=0)
|
| 88 |
+
|
| 89 |
+
# ์๋์ง๊ฐ ๋์ ๊ตฌ๊ฐ๋ง ์ ์ง (๋ ๋์ threshold ์ฌ์ฉ)
|
| 90 |
+
energy_threshold = np.percentile(energy, 70) # ์์ 30%๋ง ์ ์ง
|
| 91 |
+
voiced_frames = energy > energy_threshold
|
| 92 |
+
|
| 93 |
+
if np.any(voiced_frames):
|
| 94 |
+
# ๋ง์ง๋ง voiced frame ์ฐพ๊ธฐ
|
| 95 |
+
voiced_indices = np.where(voiced_frames)[0]
|
| 96 |
+
last_voiced_frame = voiced_indices[-1]
|
| 97 |
+
|
| 98 |
+
# ๋ง์ง๋ง ์์ฑ ๊ตฌ๊ฐ์์ ์ฝ๊ฐ ์ฌ์ ๋ฅผ ๋๊ณ ์๋ฅด๊ธฐ
|
| 99 |
+
end_sample = min(len(audio), (last_voiced_frame + 5) * hop_length + frame_length)
|
| 100 |
+
audio = audio[:end_sample]
|
| 101 |
+
|
| 102 |
+
print(f"๋๋ถ๋ถ ์ ๊ฑฐ: ์๋ณธ {len(audio_tensor.squeeze())/sample_rate:.2f}์ด โ {len(audio)/sample_rate:.2f}์ด")
|
| 103 |
+
|
| 104 |
+
# 2. ํ์ด๋์์ ์ ์ฉ (์์ฐ์ค๋ฌ์ด ๋๋งบ์)
|
| 105 |
+
fade_length = min(int(0.1 * sample_rate), len(audio) // 4) # 0.1์ด ๋๋ ์ ์ฒด ๊ธธ์ด์ 1/4
|
| 106 |
+
if fade_length > 0:
|
| 107 |
+
fade_curve = np.linspace(1, 0, fade_length)
|
| 108 |
+
audio[-fade_length:] *= fade_curve
|
| 109 |
+
|
| 110 |
+
# 3. ๋งค์ฐ ์์ ๊ฐ๋ค ์ ๊ฑฐ (๋
ธ์ด์ฆ ๊ฒ์ดํธ)
|
| 111 |
+
noise_gate_threshold = 0.001
|
| 112 |
+
audio = np.where(np.abs(audio) < noise_gate_threshold, 0, audio)
|
| 113 |
+
|
| 114 |
+
# 4. ์ต์ข
์ ๊ทํ
|
| 115 |
+
if np.max(np.abs(audio)) > 0:
|
| 116 |
+
audio = audio / np.max(np.abs(audio)) * 0.9
|
| 117 |
+
|
| 118 |
+
return torch.from_numpy(audio).unsqueeze(0)
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
print(f"ํ์ฒ๋ฆฌ ์คํจ: {e}, ์๋ณธ ๋ฐํ")
|
| 122 |
+
return audio_tensor
|
| 123 |
+
|
| 124 |
def advanced_silence_removal(audio_path, target_sr=16000):
|
| 125 |
"""
|
| 126 |
๋ ์ ๊ตํ ๋ฌด์ ์ ๊ฑฐ
|
|
|
|
| 610 |
if not results:
|
| 611 |
raise Exception("์์ฑ ํฉ์ฑ ๊ฒฐ๊ณผ๊ฐ ๋น์ด์์ต๋๋ค.")
|
| 612 |
|
| 613 |
+
# ํฉ์ฑ๋ ์์ฑ ํ์ฒ๋ฆฌ (๋๋ถ๋ถ ์ํฐํฉํธ ์ ๊ฑฐ)
|
| 614 |
+
synthesized_audio = results[0]['tts_speech']
|
| 615 |
+
processed_audio = postprocess_synthesized_audio(synthesized_audio, cosyvoice_model.sample_rate)
|
| 616 |
+
|
| 617 |
+
# ํฉ์ฑ๋ ์์ฑ ํ์ฒ๋ฆฌ (๋๋ถ๋ถ ์ํฐํฉํธ ์ ๊ฑฐ)
|
| 618 |
+
synthesized_audio = results[0]['tts_speech']
|
| 619 |
+
processed_audio = postprocess_synthesized_audio(synthesized_audio, cosyvoice_model.sample_rate)
|
| 620 |
+
|
| 621 |
# ๊ฒฐ๊ณผ ์ ์ฅ (์ถ๋ ฅ ๋๋ ํ ๋ฆฌ ์ง์ )
|
| 622 |
output_dir = '/app/outputs'
|
| 623 |
os.makedirs(output_dir, exist_ok=True)
|
| 624 |
output_filename = f'korean_tts_output_{hash(text)}.wav'
|
| 625 |
output_path = os.path.join(output_dir, output_filename)
|
| 626 |
|
| 627 |
+
torchaudio.save(output_path, processed_audio, cosyvoice_model.sample_rate)
|
| 628 |
+
print(f"ํ์ฒ๋ฆฌ๋ ์ค๋์ค ํ์ผ ์ ์ฅ ์๋ฃ: {output_path}")
|
| 629 |
|
| 630 |
# ๋ค์ด๋ก๋์ฉ ์๋ ๊ฒฝ๋ก
|
| 631 |
download_filename = f'outputs/{output_filename}'
|
|
|
|
| 678 |
style="background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
|
| 679 |
color: white; padding: 0.75rem 1.5rem; text-decoration: none;
|
| 680 |
border-radius: 8px; display: inline-block; margin-top: 1rem;">
|
| 681 |
+
๐ฅ ํ์ผ ๋ค์ด๋ก๋
|
| 682 |
</a>
|
| 683 |
</div>
|
| 684 |
|