Spaces:
Running
Running
import gradio as gr | |
from gtts import gTTS | |
import librosa | |
import os | |
def text_to_speech(prompt): | |
# gTTS๋ฅผ ์ฌ์ฉํ์ฌ Bulgarian ํ ์คํธ๋ฅผ ์์ฑ์ผ๋ก ๋ณํ | |
tts = gTTS(text=prompt, lang="bg") # 'bg'๋ Bulgarian ์ธ์ด ์ฝ๋์ ๋๋ค. | |
audio_file = "output.mp3" | |
tts.save(audio_file) | |
# librosa๋ฅผ ์ฌ์ฉํ์ฌ mp3 ํ์ผ์ numpy ๋ฐฐ์ด๊ณผ ์ํ๋ ์ดํธ๋ก ๋ก๋ํฉ๋๋ค. | |
# sr=None๋ก ์ค์ ํ๋ฉด ์๋ณธ ์ํ๋ ์ดํธ๋ฅผ ์ ์งํฉ๋๋ค. | |
audio_array, sample_rate = librosa.load(audio_file, sr=None) | |
# ์์ mp3 ํ์ผ ์ญ์ | |
os.remove(audio_file) | |
# gr.Audio(type="numpy")๋ (numpy array, sample_rate) ํํ์ ๊ธฐ๋ํฉ๋๋ค. | |
return audio_array, sample_rate | |
with gr.Blocks() as demo: | |
gr.Markdown("## Bulgarian Text-to-Speech (TTS)") | |
with gr.Row(): | |
input_prompt = gr.Textbox(label="Enter a prompt in Bulgarian:") | |
# type์ "numpy"๋ก ์ค์ ํ์ฌ numpy ๋ฐฐ์ด์ ์ฌ์ฉํฉ๋๋ค. | |
output_audio = gr.Audio(label="Generated Speech", type="numpy") | |
generate_button = gr.Button("Generate Speech") | |
generate_button.click(text_to_speech, inputs=input_prompt, outputs=output_audio) | |
if __name__ == "__main__": | |
demo.launch() | |