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

feat: webgpu-whisper in typescript better UI #1266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions examples/webgpu-whisper-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
54 changes: 54 additions & 0 deletions examples/webgpu-whisper-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:

```js
export default tseslint.config({
extends: [
// Remove ...tseslint.configs.recommended and replace with this
...tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
...tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
...tseslint.configs.stylisticTypeChecked,
],
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:

```js
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'

export default tseslint.config({
plugins: {
// Add the react-x and react-dom plugins
'react-x': reactX,
'react-dom': reactDom,
},
rules: {
// other rules...
// Enable its recommended typescript rules
...reactX.configs['recommended-typescript'].rules,
...reactDom.configs.recommended.rules,
},
})
```
777 changes: 777 additions & 0 deletions examples/webgpu-whisper-ts/bun.lock

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions examples/webgpu-whisper-ts/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
13 changes: 13 additions & 0 deletions examples/webgpu-whisper-ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Whisper LIVE</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/webgpu-whisper-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "whisper",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@huggingface/transformers": "^3.4.1",
"@tailwindcss/vite": "^4.0.17",
"@xenova/transformers": "^2.17.2",
"lucide-react": "^0.485.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"tailwindcss": "^4.0.17"
},
"devDependencies": {
"@eslint/js": "^9.21.0",
"@types/react": "^19.0.10",
"@types/react-dom": "^19.0.4",
"@vitejs/plugin-react": "^4.3.4",
"eslint": "^9.21.0",
"eslint-plugin-react-hooks": "^5.1.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^15.15.0",
"typescript": "~5.7.2",
"typescript-eslint": "^8.24.1",
"vite": "^6.2.0"
}
}
Binary file added examples/webgpu-whisper-ts/public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/webgpu-whisper-ts/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions examples/webgpu-whisper-ts/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Whisper from "./components/Whisper";

export default function App() {
return (
<section>
<Whisper />
</section>
);
}
1 change: 1 addition & 0 deletions examples/webgpu-whisper-ts/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 104 additions & 0 deletions examples/webgpu-whisper-ts/src/components/AudioVisualizer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { useCallback, useEffect, useRef } from "react";

interface AudioVisualizerProps {
stream: MediaStream;
props:[]
}

export default function AudioVisualizer({ stream, ...props }: AudioVisualizerProps) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const audioContextRef = useRef<AudioContext | null>(null);
const animationFrameRef = useRef<number>(0);

const visualize = useCallback((stream: MediaStream) => {
if (!stream) return;

const cleanup = () => {
if (animationFrameRef.current) {
cancelAnimationFrame(animationFrameRef.current);
}
audioContextRef.current?.close();
console.debug("Audio context cleaned up");
};

try {
audioContextRef.current = new AudioContext();
const source = audioContextRef.current.createMediaStreamSource(stream);
const analyser = audioContextRef.current.createAnalyser();
analyser.fftSize = 2048;
source.connect(analyser);

const canvas = canvasRef.current;
if (!canvas) return;

const canvasCTX = canvas.getContext("2d");
if (!canvasCTX) {
throw new Error("Failed to get canvas context");
}

const bufferLength = analyser.frequencyBinCount;
const arrayData = new Uint8Array(bufferLength);

const drawVisual = () => {
try {
analyser.getByteTimeDomainData(arrayData);

canvasCTX.fillStyle = "#ffffff";
canvasCTX.fillRect(0, 0, canvas.width, canvas.height);

canvasCTX.beginPath();
const sliceWidth = (canvas.width * 1.0) / bufferLength;
let x = 0;

for (let i = 0; i < bufferLength; i++) {
const v = arrayData[i] / 128.0;
const y = (v * canvas.height) / 2;

if (i === 0) {
canvasCTX.moveTo(x, y);
} else {
canvasCTX.lineTo(x, y);
}

x += sliceWidth;
}

canvasCTX.lineTo(canvas.width, canvas.height / 2);
canvasCTX.strokeStyle = "rgba(70, 24, 255, 1)";
canvasCTX.lineWidth = 2;
canvasCTX.stroke();

animationFrameRef.current = requestAnimationFrame(drawVisual);
} catch (drawError) {
console.error("Drawing error:", drawError);
cleanup();
}
};

drawVisual();
console.debug("Started visualization");

return () => {
source?.disconnect();
analyser?.disconnect();
cleanup();
};
} catch (error) {
console.error("Visualization setup failed:", error);
cleanup();
return () => {};
}
}, []);

useEffect(() => {
if (!stream) return;

const cleanup = visualize(stream);
return () => {
console.debug("Cleaning up visualizer");
cleanup?.();
};
}, [visualize, stream]);

return <canvas ref={canvasRef} {...props} width={720} height={240} />;
}
Loading