File size: 1,490 Bytes
55f7374
5a030e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55f7374
 
 
 
 
 
 
 
 
 
 
 
 
 
5a030e1
 
 
55f7374
 
5a030e1
55f7374
 
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
import gradio as gr
import edge_tts
import asyncio
import librosa
import soundfile
import io

from inference.infer_tool import Svc

def get_or_create_eventloop():
    try:
        return asyncio.get_event_loop()
    except RuntimeError as ex:
        if "There is no current event loop in thread" in str(ex):
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            return asyncio.get_event_loop()

def tts_get_voices_list():
    voices = []
    tts_voice_list = asyncio.get_event_loop().run_until_complete(edge_tts.list_voices())
    for item in tts_voice_list:
        voices.append(item['ShortName'])
    
    return voices

def tts_mode(txt, voice):
    tts = asyncio.run(edge_tts.Communicate(txt, voice).save('temp\\test.mp3'))
    audio, sr = librosa.load('temp\\test.mp3', sr=16000, mono=True)
    raw_path = io.BytesIO()
    soundfile.write(raw_path, audio, 16000, format="wav")
    raw_path.seek(0)
    model = Svc(fr"Herta-Svc/G_10000.pth", f"Herta-Svc/config.json", device = 'cpu')
    out_audio, out_sr = model.infer('speaker0', 0, raw_path, auto_predict_f0 = True,)
    soundfile.write('temp\\xxx.wav', out_audio.cpu().numpy(), 44100)
    audio_file = open('temp\\xxx.wav', 'rb')
    audio_bytes = audio_file.read()

    return audio_bytes

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

txt = gr.Textbox(label = 'TTS text')
voice = gr.Dropdown(tts_get_voices_list(), value = str)

if gr.Button():
    tts_mode(txt, voice)