Spaces:
Runtime error
Runtime error
import gradio as gr | |
import torch | |
from transformers import pipeline | |
from timestamp import format_timestamp | |
MODEL_NAME = "openai/whisper-medium" | |
BATCH_SIZE = 8 | |
device = 0 if torch.cuda.is_available() else "cpu" | |
pipe = pipeline( | |
task="automatic-speech-recognition", | |
model=MODEL_NAME, | |
chunk_length_s=30, | |
device=device, | |
) | |
def transcribe(file, task, return_timestamps): | |
outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True) | |
text = outputs["text"] | |
timestamps = outputs["chunks"] | |
if return_timestamps==True: | |
timestamps = [f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}" for chunk in timestamps] | |
else: | |
timestamps = [f"{chunk['text']}" for chunk in timestamps] | |
text = "\n".join(str(feature) for feature in timestamps) | |
return text | |
file_transcribe = gr.Interface( | |
fn=transcribe, | |
inputs=[ | |
gr.inputs.Audio(source="upload", optional=True, label="Audio file", type="filepath"), | |
gr.inputs.Radio(["transcribe"], label="Task", default="transcribe"), | |
gr.inputs.Checkbox(default=False, label="Return timestamps"), | |
], | |
outputs="text", | |
layout="horizontal", | |
theme="huggingface", | |
title="Whisper Demo: Transcribe Audio", | |
description=( | |
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the" | |
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files" | |
" of arbitrary length." | |
), | |
cache_examples=True, | |
allow_flagging="never", | |
) | |
file_transcribe.launch(enable_queue=True, debug = True) |