File size: 1,566 Bytes
eefe9ae
 
2ec67ac
 
eefe9ae
 
 
80d2986
 
 
 
 
 
 
 
ebb2d0b
80d2986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564910c
2ec67ac
eefe9ae
 
 
564910c
 
 
eefe9ae
 
 
 
564910c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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()