🐷

huggingface candleに入門してみた

2023/08/09に公開

Huggingface candleとは

Huggingface candleは、whisperやstable diffusion, LLMをRust上で利用できようにしたライブラリです。
https://github.com/huggingface/candle/tree/main

リンク

Colab
github

準備

Google Colabを開き、メニューから「ランタイム→ランタイムのタイプを変更」でランタイムを「GPU」に変更します。

環境構築

Rustの環境構築です。

!curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
!cp /root/.cargo/bin/* /usr/local/bin

Rustの基本

僕自身が初めてRustを利用するのでちょっと色々試してみます。
(1)Runtime

%%writefile main.rs
fn main() {
    // 世界よ、こんにちは
    println!("hello world");
}
!rustc main.rs; ./main

(2)Build new project

!cargo new hello_cargo --bin
%%writefile /content/hello_cargo/src/main.rs
fn main() {
    // 世界よ、こんにちは
    println!("hello world");
}

%cd /content/hello_cargo/
# !cargo build
!cargo run

Candle

huggingface candleの利用手順です。今回はwhisperを利用してみます。

%cd /content
!git clone https://github.com/huggingface/candle.git

実行です。

%cd /content/candle
!cargo run --example whisper --release --features cuda

自分のファイルでやりたい場合は、適当なaudioファイルをアップロードしてもらって

!ffmpeg -i /content/audio.wav -ar 16000 /content/test.wav
!cargo run --example whisper --release --features cuda -- --input /content/test.wav

cargo runを利用しない場合は以下の通り

%cd /content/candle
!target/release/examples/whisper #  --input /content/test.wav

output

loaded mel filters [80, 201]
loaded wav data: Header { audio_format: 1, channel_count: 1, sampling_rate: 16000, bytes_per_second: 32000, bytes_per_sample: 2, bits_per_sample: 16 }
pcm data loaded 176000
loaded mel: [1, 80, 3000]
audio features: [1, 1500, 384]
3000: Segment { start: 0.0, duration: 30.0, dr: DecodingResult { tokens: [50257, 50363, 843, 523, 616, 5891, 3399, 1265, 407, 644, 534, 1499, 460, 466, 329, 345, 1265, 644, 345, 460, 466, 329, 534, 1499, 13, 50903, 50256], text: " And so my fellow Americans ask not what your country can do for you ask what you can do for your country.", avg_logprob: -0.330563396634537, no_speech_prob: 0.017772126942873, temperature: 0.0, compression_ratio: NaN } }, in 319.97019ms

最後に

今回はhuggingfaceから出されたRustの生成AIパッケージであるcandleを試してみました。ついに注目言語Rustに生成AIがやってきた。色々試すぞー

Discussion