Skip to content

Commit 3eb56f0

Browse files
authored
Merge pull request #10 from jgw96/better-tests
v0.2.1
2 parents 8997dcb + 5c0415f commit 3eb56f0

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ npm install web-ai-toolkit
1313

1414
## Available Functions
1515

16+
*Note: Supported hardware is listed in priority of device selection. For example, for transcribing an audio file,
17+
the code will attempt to choose an NPU first, then a GPU and finally the CPU if neither of the first two are found.*
18+
1619
| Function Name | Parameter | Type | Default Value | Supported Hardware |
1720
|-----------------------|----------------|------------------------|---------------|--------------------|
18-
| transcribeAudioFile | audioFile | Blob | - | NPU |
21+
| transcribeAudioFile | audioFile | Blob | - | NPU / GPU / CPU |
1922
| | model | string | "Xenova/whisper-tiny"| |
2023
| | timestamps | boolean | false | |
2124
| | language | string | "en-US" | |
22-
| textToSpeech | text | string | - | GPU |
25+
| textToSpeech | text | string | - | GPU / CPU |
2326
| | model | string | "Xenova/mms-tts-eng"| |
24-
| summarize | text | string | - | GPU |
27+
| summarize | text | string | - | GPU / CPU |
2528
| | model | string | "Xenova/distilbart-cnn-6-6"| |
26-
| ocr | image | Blob | - | GPU |
29+
| ocr | image | Blob | - | GPU / CPU |
2730
| | model | string | "Xenova/trocr-small-printed"| |
2831

2932
## Technical Details

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web-ai-toolkit",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"repository": "https://github.com/jgw96/web-ai-toolkit",
55
"keywords": [
66
"ai",

src/services/ocr/ocr.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-async-promise-executor */
22
import { pipeline, env } from '@huggingface/transformers';
3+
import { webGPUCheck } from '../../utils';
34

45
let ocr: any = undefined;
56

@@ -29,7 +30,7 @@ async function loadOCR(model: string): Promise<void> {
2930
env.allowLocalModels = false;
3031
env.useBrowserCache = false;
3132
ocr = await pipeline('image-to-text', model || 'Xenova/trocr-small-printed', {
32-
device: (navigator as any).ml ? "webnn" : "webgpu"
33+
device: (navigator as any).ml ? "webnn" : await webGPUCheck() ? "webgpu" : "wasm"
3334
});
3435
console.log("loaded ocr", ocr)
3536
resolve();

src/services/speech-recognition/recognition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-async-promise-executor */
22
import { AutomaticSpeechRecognitionPipeline, pipeline, env } from '@huggingface/transformers';
3+
import { webGPUCheck } from '../../utils';
34

45
let transcriber: AutomaticSpeechRecognitionPipeline | undefined = undefined;
56

@@ -57,7 +58,7 @@ export async function loadTranscriber(model: string = "Xenova/whisper-tiny", tim
5758
// @ts-ignore
5859
return_timestamps: timestamps,
5960
language,
60-
device: (navigator as any).ml ? "webnn" : "webgpu"
61+
device: (navigator as any).ml ? "webnn" : await webGPUCheck() ? "webgpu" : "wasm"
6162
});
6263

6364
resolve();

src/services/summarization/summarization.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { pipeline, env } from '@huggingface/transformers';
2+
import { webGPUCheck } from '../../utils';
23

34
let summarizer: any = undefined;
45

@@ -26,7 +27,7 @@ async function loadSummarizer(model: string): Promise<void> {
2627

2728
summarizer = await pipeline('summarization', model || 'Xenova/distilbart-cnn-6-6', {
2829
dtype: "fp32",
29-
device: (navigator as any).ml ? "webnn" : "webgpu"
30+
device: (navigator as any).ml ? "webnn" : await webGPUCheck() ? "webgpu" : "wasm"
3031
});
3132
console.log("loaded summarizer", summarizer)
3233
resolve();

src/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export async function webGPUCheck(): Promise<boolean> {
2+
// check to see if navigator.gpu exists and if we can create a device
3+
if ((navigator as any).gpu) {
4+
try {
5+
const gpu = await (navigator as any).gpu.requestAdapter();
6+
return gpu ? true : false;
7+
}
8+
catch (err) {
9+
console.error(err);
10+
return false;
11+
}
12+
}
13+
else {
14+
return false;
15+
}
16+
}

0 commit comments

Comments
 (0)