Skip to content

Commit b63ed51

Browse files
authored
[Feature] Use the built-in browser AI APIs when available (#15)
* really simple pattern * simple load and use native summarizer * summarization of really long text works now * enable concurrency * remove concurrency support * linting * linter * much better text to speech * text to speech wasm support * test improvement * v0.4.2 * cleanup
1 parent 7f32adf commit b63ed51

21 files changed

+6482
-3285
lines changed

.eslintignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Dependencies
2+
node_modules/
3+
4+
# Build outputs
5+
dist/
6+
build/
7+
*.min.js
8+
9+
# Coverage reports
10+
coverage/
11+
12+
# Logs
13+
*.log
14+
npm-debug.log*
15+
16+
# OS generated files
17+
.DS_Store
18+
Thumbs.db
19+
20+
# Editor directories and files
21+
.vscode/
22+
.idea/
23+
*.swp
24+
*.swo
25+
26+
# Temporary files
27+
*.tmp
28+
*.temp

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"eslint.validate": [
3+
"javascript",
4+
"typescript"
5+
],
6+
"editor.codeActionsOnSave": {
7+
"source.fixAll.eslint": "explicit"
8+
},
9+
"eslint.format.enable": true,
10+
"editor.formatOnSave": true
11+
}

LINTING.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Linting Setup
2+
3+
This project uses ESLint for code linting and formatting to maintain consistent code quality.
4+
5+
## Configuration
6+
7+
- **ESLint Config**: `eslint.config.js` (uses ESLint 9 flat config format)
8+
- **VS Code Settings**: `.vscode/settings.json` (integrates ESLint with VS Code)
9+
10+
## Available Scripts
11+
12+
- `npm run lint` - Run ESLint to check for issues
13+
- `npm run lint:fix` - Run ESLint and automatically fix issues where possible
14+
- `npm run lint:check` - Run ESLint with zero warnings tolerance (useful for CI)
15+
16+
## Rules Overview
17+
18+
### TypeScript Rules
19+
- Enforces explicit return types for functions (warning)
20+
- Prevents use of `any` type (warning)
21+
- Prevents unused variables (error)
22+
- Removes inferrable type annotations (error)
23+
24+
### Code Style
25+
- 2-space indentation
26+
- Single quotes for strings
27+
- Semicolons required
28+
- Trailing commas in multiline objects/arrays
29+
30+
### Import Organization
31+
- Alphabetical order within groups
32+
- Proper grouping: builtin, external, internal, parent, sibling, index
33+
- Empty lines between import groups
34+
35+
### Code Quality
36+
- Console statements are warnings (allowed in development)
37+
- No debugger statements (error)
38+
- Async promise executors are warnings
39+
- Consistent equality checks (===)
40+
- Prefer const over let where possible
41+
42+
## VS Code Integration
43+
44+
The project includes VS Code settings to:
45+
- Enable ESLint validation for TypeScript and JavaScript files
46+
- Auto-fix ESLint issues on save
47+
- Format code on save
48+
49+
## Customization
50+
51+
To modify rules, edit the `eslint.config.js` file. Rules can be:
52+
- `"error"` - Will cause ESLint to exit with an error code
53+
- `"warn"` - Will show warnings but won't fail
54+
- `"off"` - Disable the rule
55+
56+
## Ignoring Files
57+
58+
Files are ignored using the `ignores` property in `eslint.config.js`:
59+
- `node_modules/`
60+
- `dist/`
61+
- `build/`
62+
- `*.min.js`
63+
- `coverage/`

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

22
# Web AI Toolkit
33

4-
The Web AI Toolkit simplifies the integration of AI features, such as OCR, speech-to-text, text summarization and more into your application. It ensures data privacy and offline capability by running all AI workloads locally, leveraging WebNN when available, with a fallback to WebGPU.
4+
The Web AI Toolkit simplifies the integration of AI features, such as OCR, speech-to-text, text summarization and more into your application. It ensures data privacy and offline capability by running all AI workloads locally.
5+
6+
## Device Support
7+
8+
The functions the Web AI Toolkit provides will run on any device that supports WebGPU. Some of our APIs, such as the summarize function, will use the Built-In Summarizer API available in Chromium browsers when available, otherwise it will fall back to a WebGPU backend.
59

610
## Installation
711

@@ -18,17 +22,17 @@ the code will attempt to choose the GPU first and then the CPU otherwise.*
1822

1923
| Function Name | Parameter | Type | Default Value | Supported Hardware |
2024
|-----------------------|----------------|------------------------|---------------|--------------------|
21-
| transcribeAudioFile | audioFile | Blob | - | GPU / CPU |
25+
| transcribeAudioFile | audioFile | Blob | - | GPU |
2226
| | model | string | "Xenova/whisper-tiny"| |
2327
| | timestamps | boolean | false | |
2428
| | language | string | "en-US" | |
25-
| textToSpeech | text | string | - | GPU / CPU |
26-
| | model | string | "Xenova/mms-tts-eng"| |
27-
| summarize | text | string | - | GPU / CPU |
29+
| textToSpeech | text | string | - | GPU |
30+
| | model | string | "Kokoro-js"| |
31+
| summarize | text | string | - | GPU |
2832
| | model | string | "Xenova/distilbart-cnn-6-6"| |
29-
| ocr | image | Blob | - | GPU / CPU |
33+
| ocr | image | Blob | - | GPU |
3034
| | model | string | "Xenova/trocr-small-printed"| |
31-
| classifyImage | image | Blob | - | NPU / GPU / CPU |
35+
| classifyImage | image | Blob | - | NPU / GPU |
3236
| | model | string | "Xenova/resnet-50"| |
3337
| doRAGSearch | texts | Array<string> | [] | GPU
3438
| | query | string | "" | |
@@ -109,7 +113,7 @@ console.log(text);
109113

110114
## Technical Details
111115

112-
The Web AI Toolkit utilizes the [transformers.js project](https://huggingface.co/docs/transformers.js/index) to run AI workloads. All AI processing is performed locally on the device, ensuring data privacy and reducing latency. AI workloads are run using the [WebNN API](https://learn.microsoft.com/en-us/windows/ai/directml/webnn-overview) when available, otherwise falling back to the WebGPU API, or even to the CPU with WebAssembly. Choosing the correct hardware to target is handled by the library.
116+
All AI processing is performed locally on the device, ensuring data privacy and reducing latency. All AI workloads are run using the WebGPU API or using the built in Chromium AI APIs in Edge and Chrome.
113117

114118
## Contribution
115119

eslint.config.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import js from '@eslint/js';
2+
import tseslint from '@typescript-eslint/eslint-plugin';
3+
import tsparser from '@typescript-eslint/parser';
4+
import importPlugin from 'eslint-plugin-import';
5+
import nodePlugin from 'eslint-plugin-n';
6+
import promisePlugin from 'eslint-plugin-promise';
7+
8+
export default [
9+
// Base JavaScript rules
10+
js.configs.recommended,
11+
12+
// TypeScript files configuration
13+
{
14+
files: ['**/*.ts', '**/*.tsx'],
15+
languageOptions: {
16+
parser: tsparser,
17+
parserOptions: {
18+
ecmaVersion: 'latest',
19+
sourceType: 'module',
20+
},
21+
globals: {
22+
console: 'readonly',
23+
navigator: 'readonly',
24+
self: 'readonly',
25+
Summarizer: 'readonly',
26+
Blob: 'readonly',
27+
URL: 'readonly',
28+
FileReader: 'readonly',
29+
AudioContext: 'readonly',
30+
fetch: 'readonly',
31+
},
32+
},
33+
plugins: {
34+
'@typescript-eslint': tseslint,
35+
'import': importPlugin,
36+
'n': nodePlugin,
37+
'promise': promisePlugin,
38+
},
39+
rules: {
40+
// TypeScript specific rules
41+
'@typescript-eslint/no-unused-vars': ['error', {
42+
'argsIgnorePattern': '^_',
43+
'varsIgnorePattern': '^_'
44+
}],
45+
'@typescript-eslint/no-explicit-any': 'warn',
46+
'@typescript-eslint/no-var-requires': 'error',
47+
'@typescript-eslint/explicit-function-return-type': 'warn',
48+
'@typescript-eslint/no-inferrable-types': 'error',
49+
50+
// Import rules
51+
'import/order': ['error', {
52+
'groups': [
53+
'builtin',
54+
'external',
55+
'internal',
56+
'parent',
57+
'sibling',
58+
'index'
59+
],
60+
'newlines-between': 'always',
61+
'alphabetize': {
62+
'order': 'asc',
63+
'caseInsensitive': true
64+
}
65+
}],
66+
'import/no-duplicates': 'error',
67+
68+
// Promise rules
69+
'promise/always-return': 'error',
70+
'promise/no-return-wrap': 'error',
71+
'promise/param-names': 'error',
72+
'promise/catch-or-return': 'error',
73+
'promise/no-nesting': 'warn',
74+
'promise/no-promise-in-callback': 'warn',
75+
76+
// Code quality rules
77+
'no-console': 'warn',
78+
'no-debugger': 'error',
79+
'no-unused-vars': 'off', // Use TypeScript version instead
80+
'no-async-promise-executor': 'warn', // Allow async promise executors with warning
81+
82+
// Style rules
83+
'indent': ['error', 2],
84+
'quotes': ['error', 'single'],
85+
'semi': ['error', 'always'],
86+
'comma-dangle': ['error', 'always-multiline'],
87+
88+
// Best practices
89+
'eqeqeq': ['error', 'always'],
90+
'curly': ['error', 'all'],
91+
'no-eval': 'error',
92+
'no-implied-eval': 'error',
93+
'no-new-func': 'error',
94+
'no-return-assign': 'error',
95+
'no-sequences': 'error',
96+
'no-throw-literal': 'error',
97+
'no-unmodified-loop-condition': 'error',
98+
'no-unused-expressions': 'error',
99+
'prefer-const': 'error',
100+
'prefer-arrow-callback': 'error',
101+
},
102+
settings: {
103+
'import/resolver': {
104+
typescript: {
105+
alwaysTryTypes: true,
106+
project: './tsconfig.json',
107+
},
108+
},
109+
},
110+
},
111+
112+
// Test files configuration
113+
{
114+
files: ['**/*.test.ts', '**/*.spec.ts', 'tests/**/*.ts'],
115+
rules: {
116+
'@typescript-eslint/no-explicit-any': 'off',
117+
'no-console': 'off',
118+
},
119+
},
120+
121+
// Configuration files
122+
{
123+
files: ['*.config.js', '*.config.ts', 'vite.config.*', 'vitest.config.*'],
124+
rules: {
125+
'@typescript-eslint/no-var-requires': 'off',
126+
'no-console': 'off',
127+
},
128+
},
129+
130+
// Global ignores
131+
{
132+
ignores: [
133+
'node_modules/**',
134+
'dist/**',
135+
'build/**',
136+
'*.min.js',
137+
'coverage/**',
138+
],
139+
},
140+
];

0 commit comments

Comments
 (0)