Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,77 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
xPathRes.singleNodeValue.click();}"""
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
else: return 'Speak'
|
14 |
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
# Initialize the automatic speech recognition pipeline using a pre-trained model
|
6 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
|
7 |
|
8 |
+
# Global variables to store the accumulated audio data and its streaming rate
|
9 |
+
audio_data = None
|
10 |
+
streaming_rate = None
|
|
|
11 |
|
12 |
+
def capture_audio(stream, new_chunk):
|
13 |
+
"""
|
14 |
+
Function to capture streaming audio and accumulate it in a global variable.
|
15 |
|
16 |
+
Args:
|
17 |
+
stream (numpy.ndarray): The accumulated audio data up to this point.
|
18 |
+
new_chunk (tuple): A tuple containing the sampling rate and the new audio data chunk.
|
|
|
19 |
|
20 |
+
Returns:
|
21 |
+
numpy.ndarray: The updated stream with the new chunk appended.
|
22 |
+
"""
|
23 |
+
global audio_data
|
24 |
+
global streaming_rate
|
25 |
|
26 |
+
# Extract sampling rate and audio chunk, normalize the audio
|
27 |
+
sr, y = new_chunk
|
28 |
+
streaming_rate = sr
|
29 |
+
y = y.astype(np.float32)
|
30 |
+
y /= np.max(np.abs(y))
|
31 |
|
32 |
+
# Concatenate new audio chunk to the existing stream or start a new one
|
33 |
+
if stream is not None:
|
34 |
+
stream = np.concatenate([stream, y])
|
35 |
+
else:
|
36 |
+
stream = y
|
37 |
|
38 |
+
# Update the global variable with the new audio data
|
39 |
+
audio_data = stream
|
40 |
+
return stream
|
41 |
|
42 |
+
def get_transcript():
|
43 |
+
"""
|
44 |
+
Function to transcribe the accumulated audio data.
|
45 |
|
46 |
+
Returns:
|
47 |
+
str: The transcription of the accumulated audio data.
|
48 |
+
"""
|
49 |
+
global audio_data
|
50 |
+
global streaming_rate
|
51 |
+
|
52 |
+
# Transcribe the audio data if available
|
53 |
+
if audio_data is not None and streaming_rate is not None:
|
54 |
+
transcript = transcriber({"sampling_rate": streaming_rate, "raw": audio_data})["text"]
|
55 |
+
return transcript
|
56 |
+
return ""
|
57 |
|
58 |
+
# Building the Gradio interface using Blocks
|
59 |
+
with gr.Blocks() as demo:
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
# State variable to manage the streaming data
|
63 |
+
state = gr.State()
|
64 |
+
# Audio component for real-time audio capture from the microphone
|
65 |
+
audio = gr.Audio(sources=["microphone"], streaming=True, type="numpy")
|
66 |
+
# Textbox for displaying the transcription
|
67 |
+
transcript_box = gr.Textbox(label="Transcript")
|
68 |
+
# Button to initiate transcription of the captured audio
|
69 |
+
rfrsh_btn = gr.Button("Refresh")
|
70 |
|
71 |
+
# Streaming setup to handle real-time audio capture
|
72 |
+
audio.stream(fn=capture_audio, inputs=[state, audio], outputs=[state])
|
73 |
+
# Button click setup to trigger transcription
|
74 |
+
rfrsh_btn.click(fn=get_transcript, outputs=[transcript_box])
|
75 |
|
76 |
+
# Launch the Gradio interface
|
77 |
+
demo.launch()
|