Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,37 +5,41 @@ import numpy as np
|
|
5 |
import os
|
6 |
|
7 |
def text_to_speech(prompt):
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
# pydub์ raw ๋ฐ์ดํฐ๋ฅผ numpy ๋ฐฐ์ด๋ก ๋ณํ (int16)
|
17 |
-
samples = np.array(sound.get_array_of_samples())
|
18 |
-
|
19 |
-
# ๋ง์ฝ ์คํ
๋ ์ค๋ผ๋ฉด ๋ชจ๋
ธ๋ก ๋ณํ (์ฑ๋ ํ๊ท )
|
20 |
-
if sound.channels > 1:
|
21 |
-
samples = samples.reshape((-1, sound.channels))
|
22 |
-
samples = samples.mean(axis=1)
|
23 |
-
|
24 |
-
# int16 ๋ฐ์ดํฐ๋ฅผ float32๋ก ์ ๊ทํ (๋ฒ์: [-1.0, 1.0])
|
25 |
-
samples = samples.astype(np.float32) / 32768.0
|
26 |
-
sample_rate = sound.frame_rate
|
27 |
-
|
28 |
-
# ์์๋ก ์์ฑํ mp3 ํ์ผ ์ญ์
|
29 |
-
os.remove(audio_file)
|
30 |
-
|
31 |
-
# gr.Audio(type="numpy")๋ (numpy_array, sample_rate) ํํ์ ๊ธฐ๋ํฉ๋๋ค.
|
32 |
return samples, sample_rate
|
33 |
|
34 |
with gr.Blocks() as demo:
|
35 |
gr.Markdown("## Bulgarian Text-to-Speech (TTS)")
|
36 |
with gr.Row():
|
37 |
input_prompt = gr.Textbox(label="Enter a prompt in Bulgarian:")
|
38 |
-
# type์ "numpy"๋ก ์ค์ ํ์ฌ numpy ๋ฐฐ์ด์ ์ฌ์ฉํฉ๋๋ค.
|
39 |
output_audio = gr.Audio(label="Generated Speech", type="numpy")
|
40 |
generate_button = gr.Button("Generate Speech")
|
41 |
|
|
|
5 |
import os
|
6 |
|
7 |
def text_to_speech(prompt):
|
8 |
+
try:
|
9 |
+
# 1) gTTS๋ก mp3 ์์ฑ
|
10 |
+
tts = gTTS(text=prompt, lang="bg")
|
11 |
+
audio_file = "output.mp3"
|
12 |
+
tts.save(audio_file)
|
13 |
+
except Exception as e:
|
14 |
+
print("gTTS ์์ฑ ์ค๋ฅ:", e)
|
15 |
+
raise e # ์๋ฌ๋ฅผ ๋ค์ ๋ฐ์์์ผ Gradio์์ ๊ฐ์งํ๋๋ก ํจ
|
16 |
|
17 |
+
try:
|
18 |
+
# 2) mp3 -> numpy ๋ณํ (pydub ์ฌ์ฉ)
|
19 |
+
sound = AudioSegment.from_mp3(audio_file)
|
20 |
+
samples = np.array(sound.get_array_of_samples())
|
21 |
+
|
22 |
+
# ์คํ
๋ ์ค๋ฉด ๋ชจ๋
ธ๋ก ๋ณํ
|
23 |
+
if sound.channels > 1:
|
24 |
+
samples = samples.reshape((-1, sound.channels))
|
25 |
+
samples = samples.mean(axis=1)
|
26 |
+
|
27 |
+
# int16 -> float32 ์ ๊ทํ
|
28 |
+
samples = samples.astype(np.float32) / 32768.0
|
29 |
+
sample_rate = sound.frame_rate
|
30 |
+
except Exception as e:
|
31 |
+
print("pydub ๋ก๋ฉ/์ฒ๋ฆฌ ์ค๋ฅ:", e)
|
32 |
+
raise e
|
33 |
+
finally:
|
34 |
+
if os.path.exists(audio_file):
|
35 |
+
os.remove(audio_file)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
return samples, sample_rate
|
38 |
|
39 |
with gr.Blocks() as demo:
|
40 |
gr.Markdown("## Bulgarian Text-to-Speech (TTS)")
|
41 |
with gr.Row():
|
42 |
input_prompt = gr.Textbox(label="Enter a prompt in Bulgarian:")
|
|
|
43 |
output_audio = gr.Audio(label="Generated Speech", type="numpy")
|
44 |
generate_button = gr.Button("Generate Speech")
|
45 |
|