Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,28 +17,25 @@ transcriber = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav
|
|
| 17 |
# else:
|
| 18 |
# stream = y
|
| 19 |
# return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"]
|
| 20 |
-
SAMPLE_RATE = 16000 # For example, replace with actual sample rate
|
| 21 |
|
| 22 |
-
|
| 23 |
-
BUFFER_SIZE = SAMPLE_RATE * BUFFER_DURATION
|
| 24 |
-
audio_buffer = np.zeros(BUFFER_SIZE, dtype=np.float32)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def transcribe(new_chunk):
|
| 28 |
-
global audio_buffer
|
| 29 |
|
| 30 |
sr, y = new_chunk
|
| 31 |
y = y.astype(np.float32)
|
| 32 |
y /= np.max(np.abs(y))
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
return
|
| 42 |
|
| 43 |
demo = gr.Interface(
|
| 44 |
transcribe,
|
|
|
|
| 17 |
# else:
|
| 18 |
# stream = y
|
| 19 |
# return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"]
|
|
|
|
| 20 |
|
| 21 |
+
def transcribe(stream, new_chunk, silence_threshold=0.5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
sr, y = new_chunk
|
| 24 |
y = y.astype(np.float32)
|
| 25 |
y /= np.max(np.abs(y))
|
| 26 |
|
| 27 |
+
print(np.mean(np.abs(y)))
|
| 28 |
+
# Check if audio level is below silence threshold
|
| 29 |
+
if np.mean(np.abs(y)) < 0.5:
|
| 30 |
+
print("Stream reset due to silence") # Print message indicating stream reset
|
| 31 |
+
stream = None # Reset the stream if silence is detected
|
| 32 |
+
else:
|
| 33 |
+
if stream is not None:
|
| 34 |
+
stream = np.concatenate([stream, y])
|
| 35 |
+
else:
|
| 36 |
+
stream = y
|
| 37 |
|
| 38 |
+
return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"]
|
| 39 |
|
| 40 |
demo = gr.Interface(
|
| 41 |
transcribe,
|