oarab commited on
Commit
8dec64d
·
verified ·
1 Parent(s): 4e880a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
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
- BUFFER_DURATION = 5 # in seconds
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
- # Update audio buffer
35
- audio_buffer[:-len(y)] = audio_buffer[len(y):]
36
- audio_buffer[-len(y):] = y
37
-
38
- # Transcribe only the last 5 seconds of audio
39
- transcribed_text = transcriber({"sampling_rate": sr, "raw": audio_buffer[-BUFFER_SIZE:]})["text"]
 
 
 
 
40
 
41
- return transcribed_text
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,