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