Liusuthu commited on
Commit
958c344
·
verified ·
1 Parent(s): 3b52837

Update consult.py

Browse files
Files changed (1) hide show
  1. consult.py +2 -111
consult.py CHANGED
@@ -3,7 +3,7 @@ import soundfile as sf
3
  import torchaudio
4
  from speechbrain.pretrained.interfaces import foreign_class
5
 
6
- from app_utils import video_score,video_test
7
  from authors import AUTHORS
8
 
9
  # Importing necessary components for the Gradio app
@@ -46,116 +46,7 @@ ASR_model = ParaformerOffline()
46
  vad = FSMNVad()
47
  punc = CttPunctuator()
48
 
49
- def text_api(text:str):
50
- result = client.predict(
51
- text, # str in '输入文字' Textbox component
52
- api_name="/predict",
53
- )
54
- return result
55
-
56
-
57
- def get_text_score(text):
58
- string=text_api(text)
59
- part1 = str.partition(string, r"text")
60
- want1 = part1[2]
61
- label = want1[4:6]
62
- part2 = str.partition(string, r"probability")
63
- want2 = part2[2]
64
- prob = float(want2[3:-4])
65
- return label, prob
66
-
67
- def classify_continuous(audio):
68
- print(type(audio))
69
- print(audio)
70
- sample_rate, signal = audio # 这是语音的输入
71
- signal = signal.astype(np.float32)
72
- signal /= np.max(np.abs(signal))
73
- sf.write("data/a.wav", signal, sample_rate)
74
- signal, sample_rate = torchaudio.load("data/a.wav")
75
- signal1 = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(
76
- signal
77
- )
78
- torchaudio.save("data/out.wav", signal1, 16000, encoding="PCM_S", bits_per_sample=16)
79
- Audio = "data/out.wav"
80
- speech, sample_rate = AudioReader.read_wav_file(Audio)
81
- if signal == "none":
82
- return "none", "none", "haha"
83
- else:
84
- segments = vad.segments_offline(speech)
85
- text_results = ""
86
- for part in segments:
87
- _result = ASR_model.infer_offline(
88
- speech[part[0] * 16 : part[1] * 16], hot_words="任意热词 空格分开"
89
- )
90
- text_results += punc.punctuate(_result)[0]
91
-
92
- out_prob, score, index, text_lab = classifier.classify_batch(signal1)
93
- print(type(out_prob.squeeze(0).numpy()))
94
- print(out_prob.squeeze(0).numpy())
95
- print(type(text_lab[-1]))
96
- print(text_lab[-1])
97
- return text_results, out_prob.squeeze(0).numpy(), text_lab[-1], Audio
98
-
99
-
100
- #######################################################################
101
- #规范函数,只管值输入输出:
102
- def text_score(text):
103
- string=text_api(text)
104
- part1 = str.partition(string, r"text")
105
- want1 = part1[2]
106
- label = want1[4:6]
107
- part2 = str.partition(string, r"probability")
108
- want2 = part2[2]
109
- prob = float(want2[3:-4])
110
- if label=="正向":
111
- score=-prob*10
112
- else:
113
- score=prob*10
114
- return text,score
115
-
116
- def speech_score(audio):
117
- print(type(audio))
118
- print(audio)
119
- sample_rate, signal = audio # 这是语音的输入
120
- signal = signal.astype(np.float32)
121
- signal /= np.max(np.abs(signal))
122
- sf.write("data/a.wav", signal, sample_rate)
123
- signal, sample_rate = torchaudio.load("data/a.wav")
124
- signal1 = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(
125
- signal
126
- )
127
- torchaudio.save("data/out.wav", signal1, 16000, encoding="PCM_S", bits_per_sample=16)
128
- Audio = "data/out.wav"
129
- speech, sample_rate = AudioReader.read_wav_file(Audio)
130
- if signal == "none":
131
- return "none", "none", "haha"
132
- else:
133
- segments = vad.segments_offline(speech)
134
- text_results = ""
135
- for part in segments:
136
- _result = ASR_model.infer_offline(
137
- speech[part[0] * 16 : part[1] * 16], hot_words="任意热词 空格分开"
138
- )
139
- text_results += punc.punctuate(_result)[0]
140
-
141
- out_prob, score, index, text_lab = classifier.classify_batch(signal1)
142
- print(type(out_prob.squeeze(0).numpy()))
143
- print(out_prob.squeeze(0).numpy())
144
- print(type(text_lab[-1]))
145
- print(text_lab[-1])
146
- #return text_results, out_prob.squeeze(0).numpy(), text_lab[-1], Audio
147
- prob=out_prob.squeeze(0).numpy()
148
- print(prob)
149
- score2=10*prob[0]-10*prob[1]
150
- print("score2",score2)
151
- print(text_lab[-1])
152
- text,score1=text_score4(text_results)
153
- # text_emo=str(get_text_score(text_results))
154
- print(text,score1)
155
- score=score1+score2
156
-
157
- return text,score
158
- #######################################################################
159
  #第四题专用函数:
160
  def text_score4(text):
161
  text,score=text_score(text)
 
3
  import torchaudio
4
  from speechbrain.pretrained.interfaces import foreign_class
5
 
6
+ from app_utils import video_score,speech_score,text_score
7
  from authors import AUTHORS
8
 
9
  # Importing necessary components for the Gradio app
 
46
  vad = FSMNVad()
47
  punc = CttPunctuator()
48
 
49
+ #########################################################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  #第四题专用函数:
51
  def text_score4(text):
52
  text,score=text_score(text)