Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ArkTS API for Keyword spotting. #1775

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion harmony-os/SherpaOnnxHar/sherpa_onnx/BuildProfile.ets
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Use these variables when you tailor your ArkTS code. They must be of the const type.
*/
export const HAR_VERSION = '1.10.40';
export const HAR_VERSION = '1.10.41';
export const BUILD_MODE_NAME = 'debug';
export const DEBUG = true;
export const TARGET_NAME = 'default';
Expand Down
5 changes: 5 additions & 0 deletions harmony-os/SherpaOnnxHar/sherpa_onnx/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ export { OfflineSpeakerSegmentationPyannoteModelConfig,
OfflineSpeakerDiarization,
FastClusteringConfig,
} from './src/main/ets/components/NonStreamingSpeakerDiarization';

export { KeywordSpotterConfig,
KeywordSpotterResult,
KeywordSpotter,
} from './src/main/ets/components/KeywordSpotting';
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ export const getOfflineSpeakerDiarizationSampleRate: (handle: object) => number;
export const offlineSpeakerDiarizationProcess: (handle: object, input: object) => object;
export const offlineSpeakerDiarizationProcessAsync: (handle: object, input: object, callback: object) => object;
export const offlineSpeakerDiarizationSetConfig: (handle: object, config: object) => void;

export const createKeywordSpotter: (config: object, mgr?: object) => object;
export const createKeywordStream: (handle: object, keywords?: string) => object;
export const isKeywordStreamReady: (handle: object, stream: object) => boolean;
export const decodeKeywordStream: (handle: object, stream: object) => void;
export const resetKeywordStream: (handle: object, stream: object) => void;
export const getKeywordResultAsJson: (handle: object, stream: object) => string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
createKeywordSpotter,
createKeywordStream,
isKeywordStreamReady,
decodeKeywordStream,
resetKeywordStream,
getKeywordResultAsJson,
} from 'libsherpa_onnx.so';

import { FeatureConfig } from './NonStreamingAsr';
import { OnlineModelConfig, OnlineStream } from './StreamingAsr';

export class KeywordSpotterConfig {
public featConfig: FeatureConfig = new FeatureConfig();
public modelConfig: OnlineModelConfig = new OnlineModelConfig();
public maxActivePaths: number = 4;
public numTrailingBlanks: number = 1;
public keywordsScore: number = 1;
public keywordsThreshold: number = 0.25;
public keywordsFile: string = '';
}

interface KeywordSpotterResultJson {
keyword: string;
timestamps: number[];
tokens: string[];
}

export class KeywordSpotterResult {
public keyword: string = '';
public tokens: string[] = [];
public timestamps: number[] = [];
public json: string = '';
}

export class KeywordSpotter {
public handle: object;
public config: KeywordSpotterConfig;

constructor(config: KeywordSpotterConfig, mgr?: object) {
this.handle = createKeywordSpotter(config, mgr);
this.config = config
}

createStream(keywords?: string): OnlineStream {
if (typeof keywords !== "undefined") {
return new OnlineStream(createKeywordStream(this.handle, keywords));
} else {
return new OnlineStream(createKeywordStream(this.handle));
}
}

isReady(stream: OnlineStream): boolean {
return isKeywordStreamReady(this.handle, stream.handle);
}

decode(stream: OnlineStream) {
decodeKeywordStream(this.handle, stream.handle);
}

reset(stream: OnlineStream) {
resetKeywordStream(this.handle, stream.handle);
}

getResult(stream: OnlineStream): KeywordSpotterResult {
const jsonStr: string = getKeywordResultAsJson(this.handle, stream.handle);

let o = JSON.parse(jsonStr) as KeywordSpotterResultJson;

const r = new KeywordSpotterResult()
r.keyword = o.keyword
r.timestamps = o.timestamps;
r.tokens = o.tokens;
r.json = jsonStr;

return r;
}
}