Spaces:
Sleeping
Sleeping
File size: 2,901 Bytes
a93e5a6 1435f74 2036c34 a93e5a6 1435f74 a93e5a6 1435f74 a93e5a6 1435f74 a93e5a6 2036c34 a93e5a6 2036c34 1435f74 2238241 1435f74 2036c34 1435f74 09f794c 1435f74 2036c34 1435f74 2036c34 1435f74 |
1 2 3 4 5 6 7 8 9 10 11 12 13 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import torch
# from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from transformers import pipeline, WhisperProcessor, WhisperForConditionalGeneration
import gradio as gr
import datetime
"""
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "distil-whisper/distil-small.en"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, use_safetensors=True
)
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
max_new_tokens=128,
torch_dtype=torch_dtype,
device=device,
)
"""
# call a text generation model to display the audio content after identifying the word(s) in the text output
# import torch
# from transformers import pipeline
# from datasets import load_dataset
# from transformers import WhisperProcessor, WhisperForConditionalGeneration
# from datasets import load_dataset
# load model and processor
processor = WhisperProcessor.from_pretrained("microsoft/whisper-base-webnn")
model = WhisperForConditionalGeneration.from_pretrained("microsoft/whisper-base-webnn")
model.config.forced_decoder_ids = None
# load dummy dataset and read audio files
# ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
# sample = ds[0]["audio"]
"""
device = "cuda:0" if torch.cuda.is_available() else "cpu"
pipe = pipeline(
"automatic-speech-recognition",
# model="openai/whisper-base",
model = "microsoft/whisper-base-webnn",
chunk_length_s=30,
device=device,
)
"""
# ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
# sample = ds[0]["audio"]
# prediction = pipe(sample.copy(), batch_size=8)["text"]
# we can also return timestamps for the predictions
#prediction = pipe(sample.copy(), batch_size=8, return_timestamps=True)["chunks"]
def audio2text(audio_file, prompt : list):
input_features = processor(audio_file, sampling_rate=sample["sampling_rate"], return_tensors="pt").input_features
# generate token ids
predicted_ids = model.generate(input_features)
# decode token ids to text
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=False)
# transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
# prediction = pipe(audio_file, batch_size=8, return_timestamps=True)["chunks"]
#prediction=pipe(audio_file)
return transcription['text']
gr.Interface(fn=audio2text, inputs=[gr.Audio(label='upload your audio file', sources='upload', type='filepath'), gr.Textbox(label="provide word(s) to search for")], outputs=[gr.Textbox(label="transcription")]).launch()
|