Kevin676 commited on
Commit
be1a4e4
1 Parent(s): 51d500c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from TTS.api import TTS
4
+
5
+ tts1 = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=True)
6
+
7
+ tts2 = TTS("tts_models/zh-CN/baker/tacotron2-DDC-GST")
8
+
9
+ import os
10
+
11
+ import openai
12
+
13
+ os.system('pip install voicefixer --upgrade')
14
+ from voicefixer import VoiceFixer
15
+ voicefixer = VoiceFixer()
16
+
17
+ import torch
18
+ import torchaudio
19
+ from speechbrain.pretrained import SpectralMaskEnhancement
20
+
21
+ enhance_model = SpectralMaskEnhancement.from_hparams(
22
+ source="speechbrain/metricgan-plus-voicebank",
23
+ savedir="pretrained_models/metricgan-plus-voicebank",
24
+ run_opts={"device":"cuda"},
25
+ )
26
+
27
+ mes = [
28
+ {"role": "system", "content": "You are my personal assistant. Try to be helpful."}
29
+ ]
30
+
31
+ def chatgpt(apikey, result):
32
+
33
+ openai.api_key = apikey
34
+
35
+ messages = mes
36
+
37
+ # chatgpt
38
+ content = result
39
+ messages.append({"role": "user", "content": content})
40
+
41
+ completion = openai.ChatCompletion.create(
42
+ model = "gpt-3.5-turbo",
43
+ messages = messages
44
+ )
45
+
46
+ chat_response = completion.choices[0].message.content
47
+
48
+ messages.append({"role": "assistant", "content": chat_response})
49
+
50
+ return chat_response
51
+
52
+ def english(text_en, upload):
53
+
54
+ tts1.tts_to_file(text_en, speaker_wav = upload, language="en", file_path="output.wav")
55
+
56
+ voicefixer.restore(input="output.wav", # input wav file path
57
+ output="audio1.wav", # output wav file path
58
+ cuda=True, # whether to use gpu acceleration
59
+ mode = 0) # You can try out mode 0, 1 to find out the best result
60
+
61
+ noisy = enhance_model.load_audio(
62
+ "audio1.wav"
63
+ ).unsqueeze(0)
64
+
65
+ enhanced = enhance_model.enhance_batch(noisy, lengths=torch.tensor([1.]))
66
+ torchaudio.save("enhanced.wav", enhanced.cpu(), 16000)
67
+
68
+ return "enhanced.wav"
69
+
70
+ def chinese(text_cn, upload1):
71
+ tts2.tts_with_vc_to_file(
72
+ text_cn + "。",
73
+ speaker_wav=upload1,
74
+ file_path="ouptut1.wav"
75
+ )
76
+
77
+ return "ouptut1.wav"
78
+
79
+ block = gr.Blocks()
80
+
81
+ with block:
82
+ with gr.Group():
83
+ gr.Markdown(
84
+ """ # <center>🥳💬💕 - TalktoAI,随时随地,谈天说地!</center>
85
+
86
+ ## <center>🤖 - 让有人文关怀的AI造福每一个人!AI向善,文明璀璨!TalktoAI - Enable the future!</center>
87
+
88
+ """
89
+ )
90
+
91
+ with gr.Box():
92
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
93
+
94
+ inp1 = gr.Textbox(label='请输入您的Openai-API-Key', type = "password")
95
+ inp2 = gr.Textbox(label='说点什么吧(中英皆可)', type = "password")
96
+
97
+ btn = gr.Button("开始对话吧")
98
+
99
+ texts1 = gr.Textbox(lines=3, label="ChatGPT的回答")
100
+
101
+ btn.click(chatgpt, [inp1, inp2], [texts1])
102
+
103
+ with gr.Box():
104
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
105
+ inp3 = texts1
106
+ inp4 = gr.Audio(source="upload", label = "请上传您喜欢的声音(wav/mp3文件)", type="filepath")
107
+ # inp5 = gr.Audio(source="microphone", type="filepath", label = '请用麦克风上传您喜欢的声音,与文件上传二选一即可')
108
+
109
+ btn1 = gr.Button("用喜欢的声音听一听吧(中文)")
110
+
111
+ out1 = gr.Audio(label="合成的专属声音(中文)")
112
+
113
+ btn1.click(chinese, [inp3, inp4], [out1])
114
+
115
+ with gr.Box():
116
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
117
+
118
+ btn2 = gr.Button("用喜欢的声音听一听吧(英文)")
119
+
120
+ out2 = gr.Audio(label="合成的专属声音(英文)")
121
+
122
+ btn2.click(english, [inp3, inp4], [out2])
123
+
124
+ gr.Markdown(
125
+ """ ### <center>注意❗:请不要输入或生成会对个人以及组织造成侵害的内容,此程序仅供科研、学习及娱乐使用。用户输入或生成的内容与程序开发者无关,请自觉合法合规使用,违反者一切后果自负。</center>
126
+
127
+ ### <center>Powered by [ChatGPT](https://chat.openai.com/). Please follow me on [Bilibili](https://space.bilibili.com/501495851?spm_id_from=333.1007.0.0).</center>
128
+
129
+ """
130
+ )
131
+
132
+ gr.HTML('''
133
+ <div class="footer">
134
+ <p>🎶🖼️🎡 - It’s the intersection of technology and liberal arts that makes our hearts sing. - Steve Jobs
135
+ </p>
136
+ <p>注:中文声音克隆实际上是由声音转换(Voice Conversion)实现,所以输出结果可能更像是一种新的声音,效果不一定很理想,希望大家理解!
137
+ </p>
138
+ </div>
139
+ ''')
140
+
141
+ block.launch(show_error=True)
142
+