Spaces:
Sleeping
Sleeping
import gradio as gr | |
from gtts import gTTS | |
from pydub import AudioSegment | |
import numpy as np | |
import os | |
def text_to_speech(prompt): | |
try: | |
# 1) gTTS๋ก mp3 ์์ฑ | |
tts = gTTS(text=prompt, lang="bg") | |
audio_file = "output.mp3" | |
tts.save(audio_file) | |
except Exception as e: | |
print("gTTS ์์ฑ ์ค๋ฅ:", e) | |
raise e # ์๋ฌ๋ฅผ ๋ค์ ๋ฐ์์์ผ Gradio์์ ๊ฐ์งํ๋๋ก ํจ | |
try: | |
# 2) mp3 -> numpy ๋ณํ (pydub ์ฌ์ฉ) | |
sound = AudioSegment.from_mp3(audio_file) | |
samples = np.array(sound.get_array_of_samples()) | |
# ์คํ ๋ ์ค๋ฉด ๋ชจ๋ ธ๋ก ๋ณํ | |
if sound.channels > 1: | |
samples = samples.reshape((-1, sound.channels)) | |
samples = samples.mean(axis=1) | |
# int16 -> float32 ์ ๊ทํ | |
samples = samples.astype(np.float32) / 32768.0 | |
sample_rate = sound.frame_rate | |
except Exception as e: | |
print("pydub ๋ก๋ฉ/์ฒ๋ฆฌ ์ค๋ฅ:", e) | |
raise e | |
finally: | |
if os.path.exists(audio_file): | |
os.remove(audio_file) | |
return samples, 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:") | |
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() | |