File size: 7,024 Bytes
890de26
 
 
 
 
 
 
 
ad7940b
890de26
 
 
 
 
 
 
 
f47ff49
 
 
890de26
ad7940b
 
890de26
 
 
 
 
 
 
 
 
 
 
f47ff49
 
1fa27cc
f47ff49
 
 
 
890de26
 
 
 
 
 
 
3744361
 
890de26
 
 
3744361
 
890de26
 
 
 
 
 
 
 
 
 
 
 
 
ceac5f8
5b8e5cd
ceac5f8
5b8e5cd
10d71a1
890de26
 
 
 
 
 
 
 
 
 
ad7940b
 
 
 
 
 
 
890de26
ad7940b
890de26
 
 
 
 
2d21960
890de26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e7226b
5cbdda6
 
 
 
f47ff49
 
890de26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e7226b
890de26
 
5cbdda6
 
 
3744361
e32152a
f47ff49
 
 
 
 
ad7940b
890de26
 
 
 
 
 
 
 
 
 
ad7940b
2ea94f9
ad7940b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132a4e6
ad7940b
 
 
 
132a4e6
ad7940b
 
 
890de26
 
 
 
 
 
ad7940b
 
 
890de26
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
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 preprocess_video_and_rank,video_score
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 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 clear_dynamic_info():
    return (
        gr.Video(value=None),
        gr.Plot(value=None),
        gr.Textbox(""),
    )

def clear_video():
    return (
        gr.Video(value=None),
        gr.Number(value=None),
        gr.Number(value=None),
        gr.Textbox("")
    )
##################################设置各自的app类####################
with gr.Blocks(css="app.css") as video:                
    with gr.Tab("Dynamic App"):
        gr.Markdown(value=DESCRIPTION_DYNAMIC)
        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)
        gr.Examples(
            [
                "videos/video1.mp4",
                "videos/video2.mp4",
                "videos/sample.webm",
                "videos/cnm.mp4",
            ],
            [input_video],
        )

    with gr.Tab("Authors"):
        gr.Markdown(value=AUTHORS)

    clear_btn_dynamic.click(
        fn=clear_dynamic_info,
        inputs=[],
        outputs=[
            input_video,
            output_statistics,
            output_score,
        ],
        queue=True,
    )
    submit_and_rank.click(
        fn=preprocess_video_and_rank,
        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"),
    ],
)
############################################################
with gr.Blocks() as video_score:
    with gr.Row():
        with gr.Column(scale=2):
            input_video = gr.Video(
                sources=["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.Number(interactive=False,label="score1")
                score2=gr.Number(interactive=False,label="score2")
                result3=gr.Textbox(interactive=False)

    clear_1.click(
        fn=clear_video,
        inputs=[],
        outputs=[input_video,score1,score2,result3]
    )
    submit_1.click(
        fn=video_score,
        inputs=[input_video],
        outputs=[score1,score2,result3]
    )



with gr.Blocks() as app:
    with gr.Tab("语音"):
        speech.render()
    with gr.Tab("视频"):
        video.render()
    with gr.Tab("视频集成打分"):
        video_score.render()


        
app.launch()