Karshiev
Launch the app in gradio
1c49988
Raw History Blame Contribute Delete
3.5 kB
# %% [markdown]
# ### Automatic Speech Recognition, build shareable app with Gradio
# %%
# Install necessary packages for the project
# !pip install transformers
# !pip install -U datasets
# !pip install soundfile
# !pip install librosa
# !pip install gradio
# %%
from transformers.utils import logging
logging.set_verbosity_error()
# %%
from datasets import load_dataset
dataset = load_dataset("librispeech_asr", split="train.clean.100", streaming=True, trust_remote_code=True)
# stream hepls us to access the dataset without downloading it all at once, it saves a lot of time and memory
# trust_remote_code=True allows us to use the dataset without worrying about remote code execution issues
# %%
example = next(iter(dataset))
# %%
dataset_head = dataset.take(5)
dataset_head_list = list(dataset_head)
# %%
third_element = list(dataset_head_list)[2]
print(third_element)
# %%
example
# %%
# Display the audio as an audible widget
from IPython.display import Audio as audio_display
audio_display(example["audio"]["array"], rate=example["audio"]["sampling_rate"])
# %% [markdown]
# #### Build pipeline
# %%
from transformers import pipeline
asr = pipeline(task="automatic-speech-recognition",
model="distil-whisper/distil-small.en")
# %%
asr.feature_extractor.sampling_rate
# %%
example['audio']['sampling_rate']
# %%
# because the audio is already in the correct sampling rate, we can directly pass the audio array to the ASR pipeline
asr(example["audio"]["array"])
# %%
example["text"]
# %% [markdown]
# #### Build a Shareable app with Gradio
# %%
import os
import gradio as gr
demo = gr.Blocks()
# %%
# Define the Gradio interface for the transcription service
def transcribe_speech(filepath):
if filepath is None:
gr.Warning("No audio found, please retry.")
return ""
output = asr(filepath)
if "text" not in output:
gr.Warning("Transcription failed, no text found.")
return ""
gr.Success("Transcription successful.")
return output["text"]
# %%
# The input source can be from a microphone or an audio file
def transcribe_speech(filepath):
if filepath is None:
gr.Warning("No audio found, please retry.")
return ""
output = asr(filepath)
return output["text"]
# %%
# To get the source from the microphone, we can use gr.Audio(source="microphone")
mic_transcribe = gr.Interface(
fn=transcribe_speech,
inputs=gr.Audio(sources="microphone",
type="filepath"),
outputs=gr.Textbox(label="Transcription",
lines=3),
allow_flagging="never")
# %%
# To get the source from an audio file, we can use gr.Audio(source="upload", type="filepath")
file_transcribe = gr.Interface(
fn=transcribe_speech,
inputs=gr.Audio(sources="upload",
type="filepath"),
outputs=gr.Textbox(label="Transcription",
lines=3),
allow_flagging="never",
)
# %%
# Launch the Gradio interface
with demo:
gr.TabbedInterface(
[mic_transcribe,
file_transcribe],
["Microphone Transcription", "File Transcription"]
)
# demo.launch(share=True, server_port=os.environ["PORT1"], debug=True)
# The above code sets up a Gradio interface that allows users to transcribe audio from either a microphone or an uploaded audio file
# %%
demo.launch(share=True, server_port=os.environ.get("PORT1", 7860), debug=True)
# %%