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()