CoolFace
Apppublic

Malek-AI/whisper-tiny

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py62 linesDownload Raw Back to root
1import torch2 3import gradio as gr4import pytube as pt5from transformers import pipeline6 7MODEL_NAME = "openai/whisper-tiny"8 9device = 0 if torch.cuda.is_available() else "cpu"10 11pipe = pipeline(12    task="automatic-speech-recognition",13    model=MODEL_NAME,14    chunk_length_s=30,15    device=device,16)17 18 19all_special_ids = pipe.tokenizer.all_special_ids20transcribe_token_id = all_special_ids[-5]21translate_token_id = all_special_ids[-6]22 23 24def transcribe(microphone, state, task="transcribe"):25    file = microphone26 27    pipe.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="transcribe" else translate_token_id]]28 29    text = pipe(file)["text"]30 31    return state + "\n" + text, state + "\n" + text32 33 34 35mf_transcribe = gr.Interface(36    fn=transcribe,37    inputs=[38        gr.Audio(source="microphone", type="filepath", optional=True),39        gr.State(value="")40    ],41    outputs=[42        gr.Textbox(lines=15),43        gr.State()]44    ,45    layout="horizontal",46    theme="huggingface",47    title="Whisper Tiny: Transcribe Audio",48    live=True,49    description=(50        "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"51        f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"52        " of arbitrary length."53    ),54    allow_flagging="never",55)56 57 58 59 60mf_transcribe.launch(enable_queue=True)61 62