bigeco commited on
Commit
bc9cd2c
ยท
verified ยท
1 Parent(s): 91aac11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -3
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, results[0]['tts_speech'], cosyvoice_model.sample_rate)
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
- feat: ์Œ์„ฑ ํ’ˆ์งˆ ๊ฐœ์„ ์„ ์œ„ํ•œ ์˜ค๋””์˜ค ์ „์ฒ˜๋ฆฌ ์ถ”๊ฐ€ ๐Ÿ“ฅ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ
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