Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,8 @@ from pynvml import *
|
|
6 |
nvmlInit()
|
7 |
gpu_h = nvmlDeviceGetHandleByIndex(0)
|
8 |
ctx_limit = 1024
|
|
|
|
|
9 |
title1 = "RWKV-4-Raven-7B-v8-Eng-20230408-ctx4096"
|
10 |
|
11 |
os.environ["RWKV_JIT_ON"] = '1'
|
@@ -17,24 +19,6 @@ model = RWKV(model=model_path, strategy='cuda fp16i8 *8 -> cuda fp16')
|
|
17 |
from rwkv.utils import PIPELINE, PIPELINE_ARGS
|
18 |
pipeline = PIPELINE(model, "20B_tokenizer.json")
|
19 |
|
20 |
-
from TTS.api import TTS
|
21 |
-
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=True)
|
22 |
-
import whisper
|
23 |
-
model1 = whisper.load_model("small")
|
24 |
-
|
25 |
-
os.system('pip install voicefixer --upgrade')
|
26 |
-
from voicefixer import VoiceFixer
|
27 |
-
voicefixer = VoiceFixer()
|
28 |
-
|
29 |
-
import torchaudio
|
30 |
-
from speechbrain.pretrained import SpectralMaskEnhancement
|
31 |
-
|
32 |
-
enhance_model = SpectralMaskEnhancement.from_hparams(
|
33 |
-
source="speechbrain/metricgan-plus-voicebank",
|
34 |
-
savedir="pretrained_models/metricgan-plus-voicebank",
|
35 |
-
run_opts={"device":"cuda"},
|
36 |
-
)
|
37 |
-
|
38 |
def generate_prompt(instruction, input=None):
|
39 |
if input:
|
40 |
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
@@ -52,17 +36,17 @@ def generate_prompt(instruction, input=None):
|
|
52 |
"""
|
53 |
|
54 |
def evaluate(
|
55 |
-
upload,
|
56 |
-
audio,
|
57 |
# instruction,
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
64 |
):
|
65 |
|
|
|
66 |
audio = whisper.load_audio(audio)
|
67 |
audio = whisper.pad_or_trim(audio)
|
68 |
|
@@ -76,18 +60,15 @@ def evaluate(
|
|
76 |
# decode the audio
|
77 |
options = whisper.DecodingOptions()
|
78 |
result = whisper.decode(model1, mel, options)
|
79 |
-
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
alpha_presence = 0.4,
|
85 |
token_ban = [], # ban the generation of some tokens
|
86 |
token_stop = [0]) # stop generation whenever you see any token here
|
87 |
|
88 |
-
instruction = result.text
|
89 |
-
input=
|
90 |
-
# input = input.strip()
|
91 |
ctx = generate_prompt(instruction, input)
|
92 |
|
93 |
gpu_info = nvmlDeviceGetMemoryInfo(gpu_h)
|
@@ -98,7 +79,7 @@ def evaluate(
|
|
98 |
out_str = ''
|
99 |
occurrence = {}
|
100 |
state = None
|
101 |
-
for i in range(int(
|
102 |
out, state = model.forward(pipeline.encode(ctx)[-ctx_limit:] if i == 0 else [token], state)
|
103 |
for n in occurrence:
|
104 |
out[n] -= (args.alpha_presence + occurrence[n] * args.alpha_frequency)
|
@@ -119,46 +100,25 @@ def evaluate(
|
|
119 |
out_last = i + 1
|
120 |
gc.collect()
|
121 |
torch.cuda.empty_cache()
|
122 |
-
|
123 |
-
res.append(out_str.strip())
|
124 |
-
|
125 |
-
# res1 = ''.join(str(x) for x in res)
|
126 |
-
|
127 |
-
tts.tts_to_file(res, speaker_wav = upload, language="en", file_path="output.wav")
|
128 |
-
|
129 |
-
voicefixer.restore(input="output.wav", # input wav file path
|
130 |
-
output="audio1.wav", # output wav file path
|
131 |
-
cuda=True, # whether to use gpu acceleration
|
132 |
-
mode = 0) # You can try out mode 0, 1, or 2 to find out the best result
|
133 |
-
|
134 |
-
noisy = enhance_model.load_audio(
|
135 |
-
"audio1.wav"
|
136 |
-
).unsqueeze(0)
|
137 |
-
|
138 |
-
enhanced = enhance_model.enhance_batch(noisy, lengths=torch.tensor([1.]))
|
139 |
-
torchaudio.save("enhanced.wav", enhanced.cpu(), 16000)
|
140 |
-
|
141 |
-
return [result.text, res, "enhanced.wav"]
|
142 |
-
|
143 |
-
# yield out_str.strip()
|
144 |
|
145 |
g = gr.Interface(
|
146 |
fn=evaluate,
|
147 |
inputs=[
|
148 |
-
gr.Audio(source="upload", label = "请上传您喜欢的声音(wav文件)", type="filepath"),
|
149 |
-
gr.Audio(source="microphone", label = "和您的专属AI聊天吧!", type="filepath"),
|
150 |
# gr.components.Textbox(lines=2, label="Instruction", value="Tell me about ravens."),
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
157 |
],
|
158 |
outputs=[
|
159 |
-
gr.Textbox(
|
160 |
-
|
161 |
-
|
|
|
162 |
],
|
163 |
title="🥳💬💕 - TalktoAI,随时随地,谈天说地!",
|
164 |
description="🤖 - 让有人文关怀的AI造福每一个人!AI向善,文明璀璨!TalktoAI - Enable the future!",
|
|
|
6 |
nvmlInit()
|
7 |
gpu_h = nvmlDeviceGetHandleByIndex(0)
|
8 |
ctx_limit = 1024
|
9 |
+
import whisper
|
10 |
+
model1 = whisper.load_model("small")
|
11 |
title1 = "RWKV-4-Raven-7B-v8-Eng-20230408-ctx4096"
|
12 |
|
13 |
os.environ["RWKV_JIT_ON"] = '1'
|
|
|
19 |
from rwkv.utils import PIPELINE, PIPELINE_ARGS
|
20 |
pipeline = PIPELINE(model, "20B_tokenizer.json")
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def generate_prompt(instruction, input=None):
|
23 |
if input:
|
24 |
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
|
|
36 |
"""
|
37 |
|
38 |
def evaluate(
|
|
|
|
|
39 |
# instruction,
|
40 |
+
audio,
|
41 |
+
input=None,
|
42 |
+
token_count=200,
|
43 |
+
temperature=1.0,
|
44 |
+
top_p=0.7,
|
45 |
+
presencePenalty = 0.1,
|
46 |
+
countPenalty = 0.1,
|
47 |
):
|
48 |
|
49 |
+
# load audio and pad/trim it to fit 30 seconds
|
50 |
audio = whisper.load_audio(audio)
|
51 |
audio = whisper.pad_or_trim(audio)
|
52 |
|
|
|
60 |
# decode the audio
|
61 |
options = whisper.DecodingOptions()
|
62 |
result = whisper.decode(model1, mel, options)
|
|
|
63 |
|
64 |
+
args = PIPELINE_ARGS(temperature = max(0.2, float(temperature)), top_p = float(top_p),
|
65 |
+
alpha_frequency = countPenalty,
|
66 |
+
alpha_presence = presencePenalty,
|
|
|
67 |
token_ban = [], # ban the generation of some tokens
|
68 |
token_stop = [0]) # stop generation whenever you see any token here
|
69 |
|
70 |
+
instruction = result.text
|
71 |
+
input = input.strip()
|
|
|
72 |
ctx = generate_prompt(instruction, input)
|
73 |
|
74 |
gpu_info = nvmlDeviceGetMemoryInfo(gpu_h)
|
|
|
79 |
out_str = ''
|
80 |
occurrence = {}
|
81 |
state = None
|
82 |
+
for i in range(int(token_count)):
|
83 |
out, state = model.forward(pipeline.encode(ctx)[-ctx_limit:] if i == 0 else [token], state)
|
84 |
for n in occurrence:
|
85 |
out[n] -= (args.alpha_presence + occurrence[n] * args.alpha_frequency)
|
|
|
100 |
out_last = i + 1
|
101 |
gc.collect()
|
102 |
torch.cuda.empty_cache()
|
103 |
+
yield out_str.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
g = gr.Interface(
|
106 |
fn=evaluate,
|
107 |
inputs=[
|
|
|
|
|
108 |
# gr.components.Textbox(lines=2, label="Instruction", value="Tell me about ravens."),
|
109 |
+
gr.Audio(source="microphone", label = "请开始对话吧!", type="filepath"),
|
110 |
+
gr.components.Textbox(lines=2, label="Input", placeholder="none"),
|
111 |
+
gr.components.Slider(minimum=10, maximum=200, step=10, value=150), # token_count
|
112 |
+
gr.components.Slider(minimum=0.2, maximum=2.0, step=0.1, value=1.0), # temperature
|
113 |
+
gr.components.Slider(minimum=0, maximum=1, step=0.05, value=0.5), # top_p
|
114 |
+
gr.components.Slider(0.0, 1.0, step=0.1, value=0.4), # presencePenalty
|
115 |
+
gr.components.Slider(0.0, 1.0, step=0.1, value=0.4), # countPenalty
|
116 |
],
|
117 |
outputs=[
|
118 |
+
gr.inputs.Textbox(
|
119 |
+
lines=5,
|
120 |
+
label="Output",
|
121 |
+
)
|
122 |
],
|
123 |
title="🥳💬💕 - TalktoAI,随时随地,谈天说地!",
|
124 |
description="🤖 - 让有人文关怀的AI造福每一个人!AI向善,文明璀璨!TalktoAI - Enable the future!",
|