Spaces:
Running
Running
import gradio as gr | |
from gtts import gTTS | |
from pydub import AudioSegment | |
import numpy as np | |
import os | |
def text_to_speech(prompt): | |
try: | |
# gTTS를 이용해 Bulgarian 텍스트를 음성으로 변환 | |
tts = gTTS(text=prompt, lang="bg") | |
audio_file = "output.mp3" | |
tts.save(audio_file) | |
except Exception as e: | |
return f"TTS 생성 오류: {e}" | |
try: | |
# pydub으로 mp3 파일 불러오기 | |
sound = AudioSegment.from_mp3(audio_file) | |
# pydub가 제공하는 raw 데이터를 NumPy 배열로 변환 (int16) | |
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로 정규화 (범위: [-1.0, 1.0]) | |
samples = samples.astype(np.float32) / 32768.0 | |
sample_rate = sound.frame_rate | |
except Exception as e: | |
return f"오디오 처리 오류: {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)") | |
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) | |
demo.launch() | |