Views
No views yet
AutoModel and AutoModelForAudioClassification (or AutoModelForSequenceClassification, if you prefer) with this repository:1# best to use both feature extractor and model with GPU!
2from datasets import load_dataset
3from transformers import (
4 AutoModel,
5 AutoModelForAudioClassification,
6 AutoFeatureExtractor,
7)
8import torch
9import torchaudio
10
11device = "cuda:0"
12
13feature_extractor = AutoFeatureExtractor.from_pretrained(
14 "WueNLP/seamless-m4t-v2-large-speech-encoder", trust_remote_code=True
15)
16model = AutoModel.from_pretrained(
17 "WueNLP/seamless-m4t-v2-large-speech-encoder",
18 trust_remote_code=True,
19 torch_dtype=torch.bfloat16,
20).to(device)
21
22audio, orig_freq = torchaudio.load(
23 "https://www2.cs.uic.edu/~i101/SoundFiles/preamble10.wav"
24)
25audio = torchaudio.functional.resample(
26 audio, orig_freq=orig_freq, new_freq=16_000
27) # must be a 16 kHz waveform array
28# return_attention_mask=True for batching
29audio_inputs = feature_extractor(audio, return_attention_mask=True, return_tensors="pt", device=device)
30audio_inputs = audio_inputs.to(device)
31with torch.autocast(dtype=torch.bfloat16, device_type="cuda"):
32 audio_hidden_states = model(**audio_inputs)[0].detach().cpu().numpy().squeeze()
33
34
35# instantiate a model for AudioClassification
36model = AutoModelForAudioClassification.from_pretrained(
37 "WueNLP/seamless-m4t-v2-large-speech-encoder",
38 trust_remote_code=True,
39 torch_dtype=torch.bfloat16,
40 # SIB-Fleurs has 7 labels
41 num_labels=7,
42).to(device)
43eng_Latn = load_dataset("wuenlp/sib-fleurs", "eng_Latn", split="train")
44examples = [eng_Latn[i] for i in range(5)]
45labels = torch.LongTensor([example["category"] for example in examples]).to(device)
46batch = feature_extractor(
47 # [0] indexing here since there typically are multiple utterances per instance, we just ignore those
48 [example["audio"][0]["array"] for example in examples],
49 sampling_rate=16000,
50 device=device,
51 return_attention_mask=True,
52 return_tensors="pt",
53).to(device)
54batch["labels"] = labels
55with torch.autocast(dtype=torch.bfloat16, device_type="cuda"):
56 # outputs comprises loss & logits
57 outputs = model(**batch)@misc{communication2023seamlessmultilingualexpressivestreaming,
title={Seamless: Multilingual Expressive and Streaming Speech Translation},
author={Seamless Communication and Loïc Barrault and Yu-An Chung and Mariano Coria Meglioli and David Dale and Ning Dong and Mark Duppenthaler and Paul-Ambroise Duquenne and Brian Ellis and Hady Elsahar and Justin Haaheim and John Hoffman and Min-Jae Hwang and Hirofumi Inaguma and Christopher Klaiber and Ilia Kulikov and Pengwei Li and Daniel Licht and Jean Maillard and Ruslan Mavlyutov and Alice Rakotoarison and Kaushik Ram Sadagopan and Abinesh Ramakrishnan and Tuan Tran and Guillaume Wenzek and Yilin Yang and Ethan Ye and Ivan Evtimov and Pierre Fernandez and Cynthia Gao and Prangthip Hansanti and Elahe Kalbassi and Amanda Kallet and Artyom Kozhevnikov and Gabriel Mejia Gonzalez and Robin San Roman and Christophe Touret and Corinne Wong and Carleigh Wood and Bokai Yu and Pierre Andrews and Can Balioglu and Peng-Jen Chen and Marta R. Costa-jussà and Maha Elbayad and Hongyu Gong and Francisco Guzmán and Kevin Heffernan and Somya Jain and Justine Kao and Ann Lee and Xutai Ma and Alex Mourachko and Benjamin Peloquin and Juan Pino and Sravya Popuri and Christophe Ropers and Safiyyah Saleem and Holger Schwenk and Anna Sun and Paden Tomasello and Changhan Wang and Jeff Wang and Skyler Wang and Mary Williamson},
year={2023},
eprint={2312.05187},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2312.05187},
}