bgtts / app.py
englissi's picture
Update app.py
482b401 verified
raw
history blame
1.24 kB
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()