LLM: Whisper long segment recognize example (#8826)
* LLM: Long segment recognize example
This commit is contained in:
parent
a232c5aa21
commit
9c652fbe95
2 changed files with 134 additions and 2 deletions
|
|
@ -0,0 +1,72 @@
|
|||
#
|
||||
# Copyright 2016 The BigDL Authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import time
|
||||
import librosa
|
||||
import argparse
|
||||
|
||||
from transformers import pipeline
|
||||
from bigdl.llm.transformers import AutoModelForSpeechSeq2Seq
|
||||
from transformers.models.whisper import WhisperFeatureExtractor, WhisperTokenizer
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Recognize Long Segment using `generate()` API for Whisper model')
|
||||
parser.add_argument('--repo-id-or-model-path', type=str, default="openai/whisper-medium",
|
||||
help='The huggingface repo id for the Whisper model to be downloaded'
|
||||
', or the path to the huggingface checkpoint folder')
|
||||
parser.add_argument('--audio-file', type=str, required=True,
|
||||
help='The path of the audio file to be recognized.')
|
||||
parser.add_argument('--language', type=str, default="english",
|
||||
help='language to be transcribed')
|
||||
parser.add_argument('--batch-size', type=int, default=2,
|
||||
help='The batch_size of pipeline inference, '
|
||||
'it usually equals of length of the audio divided by chunk-length.')
|
||||
parser.add_argument('--chunk-length', type=int, default=30,
|
||||
help="The maximum time lengths of chuncks of sampling_rate samples used to trim"
|
||||
"and pad longer or shorter audio sequences. Default to be 30s.")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Path to the .wav audio file
|
||||
audio_file_path = args.audio_file
|
||||
model_path = args.repo_id_or_model_path
|
||||
|
||||
# Load the input audio
|
||||
y, sr = librosa.load(audio_file_path, sr=None)
|
||||
|
||||
# Downsample the audio to 16kHz
|
||||
target_sr = 16000
|
||||
audio = librosa.resample(y,
|
||||
orig_sr=sr,
|
||||
target_sr=target_sr)
|
||||
|
||||
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_path, load_in_4bit=True)
|
||||
model.config.forced_decoder_ids = None
|
||||
|
||||
pipe = pipeline(
|
||||
"automatic-speech-recognition",
|
||||
model=model,
|
||||
feature_extractor= WhisperFeatureExtractor.from_pretrained(model_path),
|
||||
tokenizer= WhisperTokenizer.from_pretrained(model_path, language=args.language),
|
||||
chunk_length_s=args.chunk_length,
|
||||
)
|
||||
|
||||
start = time.time()
|
||||
prediction = pipe(audio, batch_size=args.batch_size)["text"]
|
||||
print(f"inference time is {time.time()-start}")
|
||||
|
||||
print(prediction)
|
||||
|
|
@ -5,7 +5,7 @@ In this directory, you will find examples on how you could apply BigDL-LLM INT4
|
|||
## 0. Requirements
|
||||
To run these examples with BigDL-LLM, we have some recommended requirements for your machine, please refer to [here](../README.md#recommended-requirements) for more information.
|
||||
|
||||
## Example: Recognize Tokens using `generate()` API
|
||||
## Example 1: Recognize Tokens using `generate()` API
|
||||
In the example [recognize.py](./recognize.py), we show a basic use case for a Whisper model to conduct transcription using `generate()` API, with BigDL-LLM INT4 optimizations.
|
||||
### 1. Install
|
||||
We suggest using conda to manage environment:
|
||||
|
|
@ -31,6 +31,7 @@ Arguments info:
|
|||
>
|
||||
> Please select the appropriate size of the Whisper model based on the capabilities of your machine.
|
||||
|
||||
|
||||
#### 2.1 Client
|
||||
On client Windows machine, it is recommended to run directly with full utilization of all cores:
|
||||
```powershell
|
||||
|
|
@ -57,4 +58,63 @@ numactl -C 0-47 -m 0 python ./recognize.py
|
|||
Inference time: xxxx s
|
||||
-------------------- Output --------------------
|
||||
[" Mr. Quilter is the Apostle of the Middle classes and we're glad to welcome his Gospel."]
|
||||
```
|
||||
```
|
||||
|
||||
|
||||
## Example 2: Recognize Long Segment using `generate()` API
|
||||
In the example [long-segment-recognize.py](./long-segment-recognize.py), we show a basic use case for a Whisper model to conduct transcription using `pipeline()` API for long audio input, with BigDL-LLM INT4 optimizations.
|
||||
### 1. Install
|
||||
We suggest using conda to manage environment:
|
||||
```bash
|
||||
conda create -n llm python=3.9
|
||||
conda activate llm
|
||||
|
||||
pip install bigdl-llm[all] # install bigdl-llm with 'all' option
|
||||
pip install datasets soundfile librosa # required by audio processing
|
||||
```
|
||||
|
||||
### 2. Run
|
||||
The Whisper model is intrinsically designed to work on audio samples of up to 30s in duration. For audio recordings longer than 30 seconds, it is possible to enable batched inference with `pipeline` method:
|
||||
```
|
||||
python ./long-segment-recognize.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --audio-file PATH_TO_THE_AUDIO_FILE --language LANGUAGE --chunk-length CHUNK_LENGTH
|
||||
```
|
||||
|
||||
Arguments info:
|
||||
- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the huggingface repo id for the Whisper model to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'openai/whisper-medium'`.
|
||||
- `--audio-file PATH_TO_THE_AUDIO_FILE`: argument defining the path of the audio file to be recognized.
|
||||
- `--language LANGUAGE`: argument defining language to be transcribed. It is default to be `english`.
|
||||
- `--chunk-length CHUNK_LENGTH`: argument defining the maximum number of chuncks of sampling_rate samples used to trim and pad longer or shorter audio sequences. It is default to be 30, and chunk-length should not be larger than 30s for whisper model.
|
||||
- `--batch-size`: argument defining the batch_size of pipeline inference, it usually equals of length of the audio divided by chunk-length. It is default to be 2.
|
||||
|
||||
> **Note**: When loading the model in 4-bit, BigDL-LLM converts linear layers in the model into INT4 format. In theory, a *X*B model saved in 16-bit will requires approximately 2*X* GB of memory for loading, and ~0.5*X* GB memory for further inference.
|
||||
>
|
||||
> Please select the appropriate size of the Whisper model based on the capabilities of your machine.
|
||||
|
||||
#### 2.1 Client
|
||||
On client Windows machine, it is recommended to run directly with full utilization of all cores:
|
||||
```powershell
|
||||
# Long Segment Recognize
|
||||
python ./long-segment-recognize.py --audio-file /PATH/TO/AUDIO_FILE
|
||||
```
|
||||
|
||||
#### 2.2 Server
|
||||
For optimal performance on server, it is recommended to set several environment variables (refer to [here](../README.md#best-known-configuration-on-linux) for more information), and run the example with all the physical cores of a single socket.
|
||||
|
||||
E.g. on Linux,
|
||||
```bash
|
||||
# set BigDL-Nano env variables
|
||||
source bigdl-nano-init
|
||||
|
||||
# e.g. long segment recognize for a server with 48 cores per socket
|
||||
export OMP_NUM_THREADS=48
|
||||
numactl -C 0-47 -m 0 python ./long-segment-recognize.py --audio-file /PATH/TO/AUDIO_FILE
|
||||
```
|
||||
|
||||
#### 2.3 Sample Output
|
||||
#### [openai/whisper-medium](https://huggingface.co/openai/whisper-medium)
|
||||
|
||||
For audio file(.wav) download from https://www.youtube.com/watch?v=-LIIf7E-qFI, it should be extracted as:
|
||||
```log
|
||||
inference time is xxxx s
|
||||
I don't know who you are. I don't know what you want. If you're looking for ransom, I can tell you I don't have money. But what I do have are a very particular set of skills. Skills I have acquired over a very long career. Skills that make me a nightmare for people like you. If you let my daughter go now, that'll be the end of it. I will not look for you. I will not pursue you. But if you don't, I will look for you. I will find you. And I will kill you. Good luck.
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in a new issue