File size: 5,418 Bytes
6c1e91d
 
 
 
 
 
 
 
 
 
 
 
 
8fea299
6c1e91d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d10a32
6c1e91d
 
 
e497e45
7d10a32
 
6c1e91d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eed2a31
 
6c1e91d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eed2a31
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import gradio as gr
# from TTS.api import TTS
import librosa
import http.client
# Python 2.x引入urllib模块。
from restful_aliyun_tts import *
import urllib.parse
from base_chat import *
import json
from training import *
from process import *

title = "文本转语音"
# embed_record() # 把历史聊天记录向量化

def generateAudio(text):
    token = "e329d9b11a1d4fa68e2a212c5a7892d7"
    appKey = "YdRfHrZVeusHKfv6"
    # text = '我是A i Vtuber,很高兴认识你,你有什么开心的事情嘛。'
    # 采用RFC 3986规范进行urlencode编码。
    result = run_question(text)
    # textUrlencode = text
    textUrlencode = result
    # Python 2.x请使用urllib.quote。
    # textUrlencode = urllib.quote(textUrlencode, '')
    # Python 3.x请使用urllib.parse.quote_plus。
    textUrlencode = urllib.parse.quote_plus(textUrlencode)
    textUrlencode = textUrlencode.replace("+", "%20")
    textUrlencode = textUrlencode.replace("*", "%2A")
    textUrlencode = textUrlencode.replace("%7E", "~")
    # print('text: ' + textUrlencode)
    audioSaveFile = 'syAudios.wav'
    format = 'wav'
    sampleRate = 16000

    processGETRequest(appKey, token, textUrlencode, audioSaveFile, format, sampleRate)
    
    # audio, sr = librosa.load(path="syAudios.wav")
    
    # return  sr,audio
    return audioSaveFile

def generateAudio_from_file(file):
    # 验证获取文件的内容 ,需要把加载文件的内容 进行学习训练,然后再进行生成
    # print(file.name)
    files_path = "/".join((file.name).split("/")[0:-1]) + "/"
    print(files_path)
    res = train(files_path)
    print(res)
    # with open(file.name,"r",encoding="utf-8") as f:
    #     content = f.read()
    #     print(content)

def generateAudio_wav_file(text):
    token = "e329d9b11a1d4fa68e2a212c5a7892d7"
    appKey = "YdRfHrZVeusHKfv6"
    # print(text.name)
    print(text)
    res = runPrompt(text)
    print(res)
    textUrlencode = res
    textUrlencode = urllib.parse.quote_plus(textUrlencode)
    textUrlencode = textUrlencode.replace("+", "%20")
    textUrlencode = textUrlencode.replace("*", "%2A")
    textUrlencode = textUrlencode.replace("%7E", "~")
    # print('text: ' + textUrlencode)
    audioSaveFile = 'syAudios.wav'
    format = 'wav'
    sampleRate = 16000

    processGETRequest(appKey, token, textUrlencode, audioSaveFile, format, sampleRate)

    # audio, sr = librosa.load(path="syAudios.wav")

    # return  sr,audio
    return audioSaveFile


example = [["Ai Vtuber是什么"],["where is nanjing"],["who are you"],["今天吃什么好呢"],["武汉今天天气咋样"],["what are the Annie skills"]]

app = gr.Blocks()
with app :
    gr.Markdown("# 文本转语音以及根据加载的文本来回答\n\n"
                "Text:根据提问的问题回答之后转语音\n\n"
                "File:根据加载的文本通过训练后,按照文本来进行回答,只回答相关的问题\n\n")

    with gr.Tabs():
        with gr.TabItem("TTS"):
            with gr.Row():
                with gr.Column():
                    textbox = gr.Textbox(label="Text",
                                         placeholder="Type your sentence here",
                                         value="what is Ai Vtuber",elem_id="tts-input")
                    filebox = gr.File(file_count="single",
                                      type="file",
                                      label=None
                                    )
                with gr.Column():
                    # text_output = gr.Textbox(label="Message")
                    # 通过对话
                    audio_output = gr.Audio(label="Output Audio",elem_id="tts-audio")
                    btn = gr.Button("Generate")
                    btn.click(fn=generateAudio,
                              inputs=[textbox],
                              outputs=[audio_output])
                    # 通过文本进行学习之后的 只回答和文本相关的内容 并且输出成音频还是
                    audio_output2 = gr.Audio(label="Output Audio", elem_id="tts-audio-from-file")
                    btn2 = gr.Button("Training data(click onece can train data)")
                    btn2.click(fn=generateAudio_from_file,
                              inputs=[filebox],
                              outputs=[audio_output2])
                    # 经过训练后,点击进行生成语音
                    btn3 = gr.Button("Generate from file")
                    btn3.click(fn=generateAudio_wav_file,
                               inputs=[textbox],   # 传入的是文本 搜索的是经过向量化后的文本
                               outputs=[audio_output2])


        gr.Examples(examples=example,
                    inputs=[textbox],
                    outputs=[audio_output],
                    fn=generateAudio)

#app.launch(share=True)
app.launch()

# gr.Markdown("通过提问,然后输出结果是语音\n 加载文本,然后根据文本的内容回答问题")

# app = gr.Interface(
#     fn=generateAudio,
#     inputs="text",
#     outputs="audio",
#     title=title,
#     # streaming=True,
#     # live=True,
#     examples=[("Ai Vtuber是什么"),("where is nanjing"),("who are you"),("今天吃什么好呢"),("武汉今天天气咋样")]
#     )

# app.launch(server_name="0.0.0.0", server_port=7865, share=True)
# app.launch(share=True)
# app.launch()