Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
eustlb committed Dec 17, 2024
1 parent 6a0846b commit f75c5f3
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,23 @@ The files for the CTranslate2 versions of Moonshine are available at [huggingfac
Both models are also available on the HuggingFace hub and can be used with the `transformers` library, as follows:

```python
from transformers import AutoModelForSpeechSeq2Seq, AutoConfig, PreTrainedTokenizerFast
import torch
from transformers import AutoProcessor, MoonshineForConditionalGeneration
from datasets import load_dataset

import torchaudio
import sys
processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine-tiny")
model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine-tiny")

audio, sr = torchaudio.load(sys.argv[1])
if sr != 16000:
audio = torchaudio.functional.resample(audio, sr, 16000)
ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
audio_array = ds[0]["audio"]["array"]

# 'usefulsensors/moonshine-base' for the base model
model = AutoModelForSpeechSeq2Seq.from_pretrained('usefulsensors/moonshine-tiny', trust_remote_code=True)
tokenizer = PreTrainedTokenizerFast.from_pretrained('usefulsensors/moonshine-tiny')
inputs = processor(audio_array, return_tensors="pt")
input_values = inputs.input_values

tokens = model(audio)
print(tokenizer.decode(tokens[0], skip_special_tokens=True))
generated_ids = model.generate(input_values, max_new_tokens=100)

transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(transcription)
```

## TODO
Expand Down

0 comments on commit f75c5f3

Please sign in to comment.