Update README.md
Browse files
README.md
CHANGED
@@ -58,79 +58,28 @@ The architecture is the same as [openai/whisper-medium](https://huggingface.co/o
|
|
58 |
The model was trained on the Common Voice 11.0 dataset (`train+validation+other` splits) and the Romanian speech synthesis corpus, and was tested on the `test` split of the Common Voice 11.0 dataset.
|
59 |
|
60 |
## Usage
|
61 |
-
Inference with 🤗
|
62 |
```python
|
|
|
|
|
63 |
import torch
|
64 |
-
from datasets import load_dataset
|
65 |
-
from transformers import pipeline
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
#
|
79 |
-
|
80 |
-
pipe.model.config.max_length = 225 + 1
|
81 |
-
# sampling
|
82 |
-
# pipe.model.config.do_sample = True
|
83 |
-
# beam search
|
84 |
-
# pipe.model.config.num_beams = 5
|
85 |
-
# return
|
86 |
-
# pipe.model.config.return_dict_in_generate = True
|
87 |
-
# pipe.model.config.output_scores = True
|
88 |
-
# pipe.model.config.num_return_sequences = 5
|
89 |
-
# Run
|
90 |
-
generated_sentences = pipe(waveform)["text"]
|
91 |
-
```
|
92 |
-
Inference with 🤗 low-level APIs
|
93 |
-
```python
|
94 |
-
import torch
|
95 |
-
import torchaudio
|
96 |
-
|
97 |
-
from datasets import load_dataset
|
98 |
-
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
99 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
100 |
-
|
101 |
-
# Load model
|
102 |
-
model = AutoModelForSpeechSeq2Seq.from_pretrained("gigant/whisper-medium-romanian").to(device)
|
103 |
-
processor = AutoProcessor.from_pretrained("gigant/whisper-medium-romanian", language="romanian", task="transcribe")
|
104 |
-
|
105 |
-
# NB: set forced_decoder_ids for generation utils
|
106 |
-
model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language="ro", task="transcribe")
|
107 |
-
# 16_000
|
108 |
-
model_sample_rate = processor.feature_extractor.sampling_rate
|
109 |
-
|
110 |
-
# Load data
|
111 |
-
ds_mcv_test = load_dataset("mozilla-foundation/common_voice_11_0", "ro", split="test", streaming=True)
|
112 |
-
test_segment = next(iter(ds_mcv_test))
|
113 |
-
waveform = torch.from_numpy(test_segment["audio"]["array"])
|
114 |
-
sample_rate = test_segment["audio"]["sampling_rate"]
|
115 |
-
# Resample
|
116 |
-
if sample_rate != model_sample_rate:
|
117 |
-
resampler = torchaudio.transforms.Resample(sample_rate, model_sample_rate)
|
118 |
-
waveform = resampler(waveform)
|
119 |
-
# Get feat
|
120 |
-
inputs = processor(waveform, sampling_rate=model_sample_rate, return_tensors="pt")
|
121 |
-
input_features = inputs.input_features
|
122 |
-
input_features = input_features.to(device)
|
123 |
-
|
124 |
-
# Generate
|
125 |
-
generated_ids = model.generate(inputs=input_features, max_new_tokens=225) # greedy
|
126 |
-
# generated_ids = model.generate(inputs=input_features, max_new_tokens=225, num_beams=5) # beam search
|
127 |
-
# Detokenize
|
128 |
-
generated_sentences = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
129 |
-
|
130 |
-
# Normalise predicted sentences if necessary
|
131 |
```
|
132 |
|
133 |
-
The code was adapted from [
|
134 |
|
135 |
## Training procedure
|
136 |
|
|
|
58 |
The model was trained on the Common Voice 11.0 dataset (`train+validation+other` splits) and the Romanian speech synthesis corpus, and was tested on the `test` split of the Common Voice 11.0 dataset.
|
59 |
|
60 |
## Usage
|
61 |
+
Inference with 🤗 transformers
|
62 |
```python
|
63 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
64 |
+
from datasets import Audio, load_dataset
|
65 |
import torch
|
|
|
|
|
66 |
|
67 |
+
# load model and processor
|
68 |
+
processor = WhisperProcessor.from_pretrained("gigant/whisper-medium-romanian")
|
69 |
+
model = WhisperForConditionalGeneration.from_pretrained("gigant/whisper-medium-romanian")
|
70 |
+
|
71 |
+
# load dummy dataset and read soundfiles
|
72 |
+
ds = load_dataset("common_voice", "ro", split="test", streaming=True)
|
73 |
+
ds = ds.cast_column("audio", Audio(sampling_rate=16_000))
|
74 |
+
input_speech = next(iter(ds))["audio"]["array"]
|
75 |
+
model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language = "ro", task = "transcribe")
|
76 |
+
input_features = processor(input_speech, return_tensors="pt", sampling_rate=16_000).input_features
|
77 |
+
predicted_ids = model.generate(input_features, max_length=448)
|
78 |
+
# transcription = processor.batch_decode(predicted_ids)
|
79 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens = True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
```
|
81 |
|
82 |
+
The code was adapted from [openai/whisper-medium](https://huggingface.co/openai/whisper-medium).
|
83 |
|
84 |
## Training procedure
|
85 |
|