-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipelines): implement parts of speech model
- Loading branch information
Showing
11 changed files
with
140 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
Cargo.lock | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { ModelManager } from "../model_manager.ts"; | ||
import { Model } from "../model.ts"; | ||
import { encode } from "../utils/encode.ts"; | ||
import { decode } from "../utils/decode.ts"; | ||
|
||
export interface POSEntity { | ||
word: string; | ||
score: number; | ||
label: string; | ||
} | ||
|
||
export class POSModel extends Model { | ||
constructor(manager: ModelManager, rid: number) { | ||
super(manager, rid); | ||
} | ||
|
||
async predict(inputs: string[]): Promise<POSEntity[]> { | ||
const { bindings, assertCode } = this.manager; | ||
const bytes = encode(JSON.stringify(inputs)); | ||
const len = await bindings | ||
.pos_predict(this.rid, bytes, bytes.length) | ||
.then(assertCode); | ||
const buf = new Uint8Array(len); | ||
await bindings.fill_result(buf, len); | ||
const json = decode(buf); | ||
return JSON.parse(json); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::{exec, models, set_result, Model}; | ||
use anyhow::Context; | ||
use rust_bert::pipelines::pos_tagging::POSModel; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn create_pos_model() -> isize { | ||
exec(|| { | ||
let model = POSModel::new(Default::default()) | ||
.context("Failed to load Parts of Speech Tagging model.")?; | ||
|
||
models::allocate(Model::POSModel(model)).map(|a| a as isize) | ||
}) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn pos_predict(rid: usize, buf: *const u8, buf_len: usize) -> isize { | ||
exec(|| { | ||
let inputs: Vec<String> = | ||
serde_json::from_slice(unsafe { std::slice::from_raw_parts(buf, buf_len) })?; | ||
|
||
models::with_access(rid, |model| { | ||
let model = match model { | ||
Model::POSModel(m) => m, | ||
_ => return Err(anyhow::anyhow!("Expected POS Model at rid '{}'.", rid)), | ||
}; | ||
|
||
let outputs = model.predict(&inputs); | ||
let outputs = serde_json::to_vec(&outputs).context("Failed to serialize POS tags.")?; | ||
Ok(set_result(outputs) as isize) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::{exec, models, Model}; | ||
use anyhow::Context; | ||
use rust_bert::pipelines::zero_shot_classification::ZeroShotClassificationModel; | ||
use serde::Deserialize; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn create_zero_shot_model() -> isize { | ||
exec(|| { | ||
let model = ZeroShotClassificationModel::new(Default::default()) | ||
.context("Failed to create zero shot classification model.")?; | ||
|
||
models::allocate(Model::ZeroShotClassificationModel(model)).map(|a| a as isize) | ||
}) | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct ZeroShotInput {} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn zero_shot_predict(rid: usize, buf: *const u8, buf_len: usize) -> isize { | ||
exec(|| { | ||
let inputs: Vec<String> = | ||
serde_json::from_slice(unsafe { std::slice::from_raw_parts(buf, buf_len) }) | ||
.context("Failed to parse zero shot input.")?; | ||
|
||
Ok(0) | ||
}) | ||
} |