Spaces:
Running
on
T4
Running
on
T4
Commit
•
891e37e
1
Parent(s):
0fcc790
first draft
Browse files
app.py
CHANGED
@@ -21,10 +21,94 @@ thread.start()
|
|
21 |
|
22 |
API_URL = "http://0.0.0.0:60808/chat"
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
OUT_CHUNK = 4096
|
25 |
OUT_RATE = 24000
|
26 |
OUT_CHANNELS = 1
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def process_audio(audio):
|
29 |
filepath = audio
|
30 |
print(f"filepath: {filepath}")
|
|
|
21 |
|
22 |
API_URL = "http://0.0.0.0:60808/chat"
|
23 |
|
24 |
+
# recording parameters
|
25 |
+
IN_FORMAT = pyaudio.paInt16
|
26 |
+
IN_CHANNELS = 1
|
27 |
+
IN_RATE = 24000
|
28 |
+
IN_CHUNK = 1024
|
29 |
+
IN_SAMPLE_WIDTH = 2
|
30 |
+
VAD_STRIDE = 0.5
|
31 |
+
|
32 |
+
# playing parameters
|
33 |
+
OUT_FORMAT = pyaudio.paInt16
|
34 |
+
OUT_CHANNELS = 1
|
35 |
+
OUT_RATE = 24000
|
36 |
+
OUT_SAMPLE_WIDTH = 2
|
37 |
+
OUT_CHUNK = 5760
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
OUT_CHUNK = 4096
|
42 |
OUT_RATE = 24000
|
43 |
OUT_CHANNELS = 1
|
44 |
|
45 |
+
def run_vad(ori_audio, sr):
|
46 |
+
_st = time.time()
|
47 |
+
try:
|
48 |
+
audio = np.frombuffer(ori_audio, dtype=np.int16)
|
49 |
+
audio = audio.astype(np.float32) / 32768.0
|
50 |
+
sampling_rate = 16000
|
51 |
+
if sr != sampling_rate:
|
52 |
+
audio = librosa.resample(audio, orig_sr=sr, target_sr=sampling_rate)
|
53 |
+
|
54 |
+
vad_parameters = {}
|
55 |
+
vad_parameters = VadOptions(**vad_parameters)
|
56 |
+
speech_chunks = get_speech_timestamps(audio, vad_parameters)
|
57 |
+
audio = collect_chunks(audio, speech_chunks)
|
58 |
+
duration_after_vad = audio.shape[0] / sampling_rate
|
59 |
+
|
60 |
+
if sr != sampling_rate:
|
61 |
+
# resample to original sampling rate
|
62 |
+
vad_audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=sr)
|
63 |
+
else:
|
64 |
+
vad_audio = audio
|
65 |
+
vad_audio = np.round(vad_audio * 32768.0).astype(np.int16)
|
66 |
+
vad_audio_bytes = vad_audio.tobytes()
|
67 |
+
|
68 |
+
return duration_after_vad, vad_audio_bytes, round(time.time() - _st, 4)
|
69 |
+
except Exception as e:
|
70 |
+
msg = f"[asr vad error] audio_len: {len(ori_audio)/(sr*2):.3f} s, trace: {traceback.format_exc()}"
|
71 |
+
print(msg)
|
72 |
+
return -1, ori_audio, round(time.time() - _st, 4)
|
73 |
+
|
74 |
+
|
75 |
+
def warm_up():
|
76 |
+
frames = b"\x00\x00" * 1024 * 2 # 1024 frames of 2 bytes each
|
77 |
+
dur, frames, tcost = run_vad(frames, 16000)
|
78 |
+
print(f"warm up done, time_cost: {tcost:.3f} s")
|
79 |
+
|
80 |
+
warm_up()
|
81 |
+
|
82 |
+
def determine_pause():
|
83 |
+
temp_audio = b""
|
84 |
+
vad_audio = b""
|
85 |
+
|
86 |
+
start_talking = False
|
87 |
+
last_temp_audio = None
|
88 |
+
|
89 |
+
while st.session_state.recording:
|
90 |
+
status.success("Listening...")
|
91 |
+
audio_bytes = stream.read(IN_CHUNK)
|
92 |
+
temp_audio += audio_bytes
|
93 |
+
|
94 |
+
if len(temp_audio) > IN_SAMPLE_WIDTH * IN_RATE * IN_CHANNELS * VAD_STRIDE:
|
95 |
+
dur_vad, vad_audio_bytes, time_vad = run_vad(temp_audio, IN_RATE)
|
96 |
+
|
97 |
+
print(f"duration_after_vad: {dur_vad:.3f} s, time_vad: {time_vad:.3f} s")
|
98 |
+
|
99 |
+
if dur_vad > 0.2 and not start_talking:
|
100 |
+
if last_temp_audio is not None:
|
101 |
+
st.session_state.frames.append(last_temp_audio)
|
102 |
+
start_talking = True
|
103 |
+
if start_talking:
|
104 |
+
st.session_state.frames.append(temp_audio)
|
105 |
+
if dur_vad < 0.1 and start_talking:
|
106 |
+
st.session_state.recording = False
|
107 |
+
print(f"speech end detected. excit")
|
108 |
+
last_temp_audio = temp_audio
|
109 |
+
temp_audio = b""
|
110 |
+
|
111 |
+
|
112 |
def process_audio(audio):
|
113 |
filepath = audio
|
114 |
print(f"filepath: {filepath}")
|