Spaces:
Configuration error
Configuration error
just need the buffer to work
Browse files
app.py
CHANGED
|
@@ -1,17 +1,13 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
from faster_whisper import WhisperModel
|
| 5 |
-
from faster_whisper.transcribe import Segment
|
| 6 |
|
| 7 |
audio_model = WhisperModel("tiny.en", device="cpu", compute_type="int8")
|
| 8 |
-
transcription = ['']
|
| 9 |
|
| 10 |
-
def transcribe(
|
| 11 |
-
segments, info = audio_model.transcribe(
|
| 12 |
-
result =
|
| 13 |
text = ""
|
| 14 |
-
|
| 15 |
if result and len(result) > 0:
|
| 16 |
text = result[0].text
|
| 17 |
print("Text:", text)
|
|
@@ -19,42 +15,41 @@ def transcribe(SampleRate, data):
|
|
| 19 |
text = ""
|
| 20 |
print("No text found")
|
| 21 |
print(result)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return(text)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
def normaliseData(audioInput, stream):
|
| 29 |
sr, y = audioInput
|
| 30 |
-
|
| 31 |
# Convert to mono if stereo
|
| 32 |
if y.ndim > 1:
|
| 33 |
y = y.mean(axis=1)
|
| 34 |
-
|
| 35 |
y = y.astype(np.float32)
|
| 36 |
y /= np.max(np.abs(y))
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
else:
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
|
|
|
| 48 |
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
audioInput = gr.Audio(sources=["microphone"], streaming=True)
|
| 51 |
audioOutput = gr.Textbox(label="Output")
|
| 52 |
-
|
| 53 |
|
| 54 |
audioInput.stream(
|
| 55 |
fn=normaliseData,
|
| 56 |
-
inputs=[audioInput,
|
| 57 |
-
outputs=[
|
| 58 |
)
|
| 59 |
demo.launch()
|
| 60 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
from faster_whisper import WhisperModel
|
|
|
|
| 4 |
|
| 5 |
audio_model = WhisperModel("tiny.en", device="cpu", compute_type="int8")
|
|
|
|
| 6 |
|
| 7 |
+
def transcribe(sr, buffer):
|
| 8 |
+
segments, info = audio_model.transcribe(buffer, beam_size=5)
|
| 9 |
+
result = list(segments)
|
| 10 |
text = ""
|
|
|
|
| 11 |
if result and len(result) > 0:
|
| 12 |
text = result[0].text
|
| 13 |
print("Text:", text)
|
|
|
|
| 15 |
text = ""
|
| 16 |
print("No text found")
|
| 17 |
print(result)
|
| 18 |
+
return text
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def normaliseData(audioInput, buffer_state):
|
|
|
|
| 21 |
sr, y = audioInput
|
| 22 |
+
|
| 23 |
# Convert to mono if stereo
|
| 24 |
if y.ndim > 1:
|
| 25 |
y = y.mean(axis=1)
|
|
|
|
| 26 |
y = y.astype(np.float32)
|
| 27 |
y /= np.max(np.abs(y))
|
| 28 |
|
| 29 |
+
# Append to buffer
|
| 30 |
+
if buffer_state is not None:
|
| 31 |
+
buffer = np.concatenate([buffer_state, y])
|
| 32 |
else:
|
| 33 |
+
buffer = y
|
| 34 |
|
| 35 |
+
# Only process if buffer is at least 3 seconds
|
| 36 |
+
min_samples = 3 * sr
|
| 37 |
+
words = ""
|
| 38 |
+
if len(buffer) >= min_samples:
|
| 39 |
+
chunk = buffer[:min_samples]
|
| 40 |
+
words = transcribe(sr, chunk)
|
| 41 |
+
buffer = buffer[min_samples:] # Remove processed samples
|
| 42 |
|
| 43 |
+
return buffer, words
|
| 44 |
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
audioInput = gr.Audio(sources=["microphone"], streaming=True)
|
| 47 |
audioOutput = gr.Textbox(label="Output")
|
| 48 |
+
buffer_state = gr.State()
|
| 49 |
|
| 50 |
audioInput.stream(
|
| 51 |
fn=normaliseData,
|
| 52 |
+
inputs=[audioInput, buffer_state],
|
| 53 |
+
outputs=[buffer_state, audioOutput]
|
| 54 |
)
|
| 55 |
demo.launch()
|
|
|