File size: 2,504 Bytes
89c506f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
"""
@author:XuMing([email protected])
@description: Re-train by TWMAN
"""
import hashlib
import os
import ssl

import gradio as gr
import torch
from loguru import logger

ssl._create_default_https_context = ssl._create_unverified_context
import nltk

nltk.download('cmudict')
from parrots import TextToSpeech

device = "cuda" if torch.cuda.is_available() else "cpu"
logger.info(f"device: {device}")
half = True if device == "cuda" else False

m = TextToSpeech(speaker_model_path="DeepLearning101/GPT-SoVITS_TWMAN", speaker_name="TWMAN", device=device, half=half)
m.predict(text="台灣南波萬。Taiwan Number One.", text_language="auto", output_path="output_audio.wav")
assert os.path.exists("output_audio.wav"), "output_audio.wav not found"


def get_text_hash(text: str):
    return hashlib.md5(text.encode('utf-8')).hexdigest()


def do_tts_wav_predict(text: str, output_path: str = None):
    if output_path is None:
        output_path = f"output_audio_{get_text_hash(text)}.wav"
    if not os.path.exists(output_path):
        m.predict(text, text_language="auto", output_path=output_path)
    return output_path


with gr.Blocks(title="TTS WebUI") as app:
    gr.Markdown(value="""
    # <center>線上語音合成;speaker:TWMAN\n

     ### <center>語音處理:https://www.twman.org/AI/ASR\n
     ### <center>那些語音處理 (Speech Processing) 踩的坑 https://blog.twman.org/2021/04/ASR.html\n
     ### <center>parrots專案:https://github.com/shibing624/parrots\n
     ### <center>使用模型:https://github.com/RVC-Boss/GPT-SoVITS\n
     ### <center>使用本模型請嚴格遵守法規! 發布二創作品請標註本專案作者及連結、作品使用GPT-SoVITS AI生成! \n
     ### <center>⚠️在線端不穩定且生成速度較慢,建議使用parrots本地推理! \n
                """)

    with gr.Group():
        gr.Markdown(value="*請在這裡輸入要進行語音合成的文字")
        with gr.Row():
            text = gr.Textbox(label="想語音合成的文字(100字以内)或使用預設", value="床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。", placeholder="請輸入您想要的文字", lines=3)
            inference_button = gr.Button("語音合成", variant="primary")
            output = gr.Audio(label="合成的語音")
        inference_button.click(
            do_tts_wav_predict,
            [text],
            [output],
        )

app.queue(max_size=10)
app.launch(inbrowser=True)