Spaces:
Runtime error
Runtime error
File size: 10,668 Bytes
890de26 6add720 890de26 f47ff49 890de26 ad7940b 890de26 f47ff49 1fa27cc f47ff49 890de26 dc88262 6f5757a dc88262 890de26 3744361 890de26 3744361 890de26 ceac5f8 5b8e5cd ceac5f8 5b8e5cd 10d71a1 890de26 20ce06a de1a106 20ce06a 6f5757a 20ce06a 890de26 83164e5 63231e5 83164e5 63231e5 83164e5 890de26 63231e5 7892708 63231e5 ad7940b 890de26 ad7940b 6add720 6b2f58c 6add720 0a89687 ad7940b d0812f3 ad7940b e67b5d2 83164e5 a45c068 83164e5 a45c068 4eefeea ad7940b f8f03c2 7cef7db ad7940b dd3e4c8 7cef7db 11803fe 6add720 da7eb82 6add720 6b2f58c 11803fe ad7940b 20ce06a ad7940b 20ce06a 7e7d111 20ce06a de1a106 20ce06a beb52e8 20ce06a cf72411 ad7940b cf72411 6f5757a cf72411 890de26 c943742 cf72411 e6e6d43 cf72411 890de26 63231e5 ad7940b cfe1036 beb52e8 cf72411 ad7940b 890de26 |
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
import os
import gradio as gr
import numpy as np
import soundfile as sf
import torchaudio
from speechbrain.pretrained.interfaces import foreign_class
from app_utils import video_score,video_test
from authors import AUTHORS
# Importing necessary components for the Gradio app
from description import DESCRIPTION_DYNAMIC # , DESCRIPTION_STATIC
# import scipy.io.wavfile as wav
from paraformer import AudioReader, CttPunctuator, FSMNVad, ParaformerOffline
from gradio_client import Client
client = Client("Liusuthu/TextDepression")
os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
###########################语音部分######################################
classifier = foreign_class(
source="pretrained_models/local-speechbrain/emotion-recognition-wav2vec2-IEMOCAP", # ".\\emotion-recognition-wav2vec2-IEMOCAP"
pymodule_file="custom_interface.py",
classname="CustomEncoderWav2vec2Classifier",
savedir="pretrained_models/local-speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
)
ASR_model = ParaformerOffline()
vad = FSMNVad()
punc = CttPunctuator()
def text_api(text:str):
result = client.predict(
text, # str in '输入文字' Textbox component
api_name="/predict",
)
return result
def get_text_score(text):
string=text_api(text)
part1 = str.partition(string, r"text")
want1 = part1[2]
label = want1[4:6]
part2 = str.partition(string, r"probability")
want2 = part2[2]
prob = float(want2[3:-4])
return label, prob
def classify_continuous(audio):
print(type(audio))
print(audio)
sample_rate, signal = audio # 这是语音的输入
signal = signal.astype(np.float32)
signal /= np.max(np.abs(signal))
sf.write("data/a.wav", signal, sample_rate)
signal, sample_rate = torchaudio.load("data/a.wav")
signal1 = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(
signal
)
torchaudio.save("data/out.wav", signal1, 16000, encoding="PCM_S", bits_per_sample=16)
Audio = "data/out.wav"
speech, sample_rate = AudioReader.read_wav_file(Audio)
if signal == "none":
return "none", "none", "haha"
else:
segments = vad.segments_offline(speech)
text_results = ""
for part in segments:
_result = ASR_model.infer_offline(
speech[part[0] * 16 : part[1] * 16], hot_words="任意热词 空格分开"
)
text_results += punc.punctuate(_result)[0]
out_prob, score, index, text_lab = classifier.classify_batch(signal1)
print(type(out_prob.squeeze(0).numpy()))
print(out_prob.squeeze(0).numpy())
print(type(text_lab[-1]))
print(text_lab[-1])
return text_results, out_prob.squeeze(0).numpy(), text_lab[-1], Audio
def speech_score(audio):
print(type(audio))
print(audio)
sample_rate, signal = audio # 这是语音的输入
signal = signal.astype(np.float32)
signal /= np.max(np.abs(signal))
sf.write("data/a.wav", signal, sample_rate)
signal, sample_rate = torchaudio.load("data/a.wav")
signal1 = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(
signal
)
torchaudio.save("data/out.wav", signal1, 16000, encoding="PCM_S", bits_per_sample=16)
Audio = "data/out.wav"
speech, sample_rate = AudioReader.read_wav_file(Audio)
if signal == "none":
return "none", "none", "haha"
else:
segments = vad.segments_offline(speech)
text_results = ""
for part in segments:
_result = ASR_model.infer_offline(
speech[part[0] * 16 : part[1] * 16], hot_words="任意热词 空格分开"
)
text_results += punc.punctuate(_result)[0]
out_prob, score, index, text_lab = classifier.classify_batch(signal1)
print(type(out_prob.squeeze(0).numpy()))
print(out_prob.squeeze(0).numpy())
print(type(text_lab[-1]))
print(text_lab[-1])
#return text_results, out_prob.squeeze(0).numpy(), text_lab[-1], Audio
prob=out_prob.squeeze(0).numpy()
print(prob)
score2=10*prob[0]-10*prob[1]
print("score2",score2)
print(text_lab[-1])
text_emo=str(get_text_score(text_results))
print(text_emo)
return score2,text_emo
#########################################视频部分###################################
def clear_dynamic_info():
return (
gr.Video(value=None),
gr.Plot(value=None),
gr.Textbox(""),
)
##################################设置各自的app类####################
with gr.Blocks(css="app.css") as video:
with gr.Row():
with gr.Column(scale=2):
input_video = gr.Video(
sources=["webcam", "upload"], elem_classes="video1", format='mp4'
)
with gr.Row():
clear_btn_dynamic = gr.Button(
value="Clear", interactive=True, scale=1
)
# submit_dynamic = gr.Button(
# value="Submit", interactive=True, scale=1, elem_classes="submit"
# )
submit_and_rank = gr.Button(
value="Score", interactive=True, scale=1, elem_classes="submit"
)
with gr.Column(scale=2, elem_classes="dl4"):
with gr.Row():
output_score = gr.Textbox(label="scores")
output_statistics = gr.Plot(
label="Statistics of emotions", elem_classes="stat"
)
output_audio=gr.Audio(interactive=False)
audio_test_button=gr.Button("分析语音")
out1=gr.Textbox(label="语音分析结果")
out2=gr.Textbox(label="音频情感识别1")
out3=gr.Textbox(label="音频情感识别2")
text_test_button=gr.Button("分析文本")
text_result=gr.Textbox(interactive=False)
clear_btn_dynamic.click(
fn=clear_dynamic_info,
inputs=[],
outputs=[
input_video,
output_statistics,
output_score,
],
queue=True,
)
submit_and_rank.click(
fn=video_score,
inputs=input_video,
outputs=[
output_statistics,
output_score,
output_audio,
],
)
audio_test_button.click(
fn=classify_continuous,
inputs=output_audio,
outputs=[out1,out2,out3]
)
text_test_button.click(
fn=text_api,
inputs=out1,
outputs=text_result,
)
####################################
speech = gr.Interface(
classify_continuous,
gr.Audio(sources=["microphone"]),
[
gr.Text(label="语音识别结果"),
gr.Text(label="音频情感识别1"),
gr.Text(label="音频情感识别2"),
],
)
############################################################
def clear_video():
return (
gr.Video(value=None),
gr.Textbox(""),
gr.Plot(value=None),
gr.Audio(value=None),
gr.Textbox(""),
gr.Textbox(""),
)
with gr.Blocks() as video_all:
with gr.Row():
with gr.Column(scale=2):
input_video = gr.Video(
sources=["webcam","upload"], elem_classes="video1", format='mp4'
)
with gr.Row():
clear_1 = gr.Button(
value="Clear", interactive=True, scale=1
)
submit_1 = gr.Button(
value="Score", interactive=True, scale=1, elem_classes="submit"
)
with gr.Column(scale=2):
with gr.Row():
score1 = gr.Textbox(label="score1")
output_statistics = gr.Plot(
label="Statistics of emotions", elem_classes="stat",visible=False,
)
output_audio=gr.Audio(interactive=False,visible=False)
score2=gr.Textbox(label="score2")
score3=gr.Textbox(label="score3")
clear_1.click(
fn=clear_video,
inputs=[],
outputs=[input_video,score1,output_statistics,output_audio,score2,score3],
queue=True,
)
submit_1.click(
fn=video_test,
inputs=input_video,
outputs=[
output_statistics,
score1,
output_audio,
score2,
score3,
],
)
###################################################################
def clear_2():
return (
gr.Audio(value=None),
gr.Textbox(""),
gr.Textbox(""),
)
with gr.Blocks() as speech_all:
with gr.Row():
with gr.Column(scale=2):
input_audio=gr.Audio(sources="microphone")
with gr.Row():
clear_audio = gr.Button(
value="Clear", interactive=True, scale=1
)
submit_audio = gr.Button(
value="Score", interactive=True, scale=1, elem_classes="submit"
)
with gr.Column(scale=2):
score2=gr.Textbox(interactive=False,label="score2")
text_emo=gr.Textbox(interactive=False,label="text_emo")
clear_audio.click(
fn=clear_2,
outputs=[input_audio,score2,text_emo]
)
submit_audio.click(
fn=speech_score,
inputs=[input_audio],
outputs=[score2,text_emo],
)
###################################################################
def clear_3():
return gr.Textbox(""),gr.Textbox("")
def text_score(text):
result=str(get_text_score(text))
return result
with gr.Blocks() as text_all:
with gr.Row():
with gr.Column(scale=2):
input_text=gr.Textbox(label="input")
with gr.Row():
clear_text = gr.Button(
value="Clear", interactive=True, scale=1
)
submit_text = gr.Button(
value="Score", interactive=True, scale=1, elem_classes="submit"
)
with gr.Column(scale=2):
text_emo=gr.Textbox(label="text_emo")
clear_text.click(clear_3,outputs=[input_text,text_emo])
submit_text.click(text_score,inputs=input_text,outputs=text_emo)
with gr.Blocks() as app:
with gr.Tab("语音"):
speech.render()
with gr.Tab("视频"):
video.render()
with gr.Tab("视频集成打分"):
video_all.render()
with gr.Tab("语音集成打分"):
speech_all.render()
with gr.Tab("文本打分"):
text_all.render()
app.launch()
|