m.rs 1.28 KB
Newer Older
yongshk's avatar
yongshk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use candle_wasm_example_whisper::worker::{Decoder as D, ModelData};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct Decoder {
    decoder: D,
}

#[wasm_bindgen]
impl Decoder {
    #[wasm_bindgen(constructor)]
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        weights: Vec<u8>,
        tokenizer: Vec<u8>,
        mel_filters: Vec<u8>,
        config: Vec<u8>,
        quantized: bool,
        is_multilingual: bool,
        timestamps: bool,
        task: Option<String>,
        language: Option<String>,
    ) -> Result<Decoder, JsError> {
        let decoder = D::load(ModelData {
            tokenizer,
            mel_filters,
            config,
            quantized,
            weights,
            is_multilingual,
            timestamps,
            task,
            language,
        });

        match decoder {
            Ok(decoder) => Ok(Self { decoder }),
            Err(e) => Err(JsError::new(&e.to_string())),
        }
    }

    #[wasm_bindgen]
    pub fn decode(&mut self, wav_input: Vec<u8>) -> Result<String, JsError> {
        let segments = self
            .decoder
            .convert_and_run(&wav_input)
            .map_err(|e| JsError::new(&e.to_string()))?;
        let json = serde_json::to_string(&segments)?;
        Ok(json)
    }
}

fn main() {}