Skip to content

Commit d2edee3

Browse files
committedFeb 22, 2025··
Added a voice only demo
1 parent 695e4ad commit d2edee3

20 files changed

+866
-306
lines changed
 

‎examples/chat-demo/src/App.tsx

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import ChatInterface from './components/ChatInterface'
2-
import EnhancedChatInterface from './components/EnhancedChatInterface'
3-
import React, { StrictMode } from 'react'
2+
import React from 'react'
43

54
function App() {
6-
return (
7-
<ChatInterface>
8-
{(props) => <EnhancedChatInterface {...props} />}
9-
</ChatInterface>
10-
);
5+
return <ChatInterface />;
116
}
127

138
export default App;

‎examples/chat-demo/src/components/ChatInterface.tsx

+375-298
Large diffs are not rendered by default.

‎examples/realtime-chat-demo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
13-
"@browserai/browserai": "^1.0.26",
13+
"@browserai/browserai": "^1.0.29",
1414
"@emotion/react": "^11.14.0",
1515
"@emotion/styled": "^11.14.0",
1616
"@huggingface/jinja": "^0.3.2",

‎examples/voice-demo/.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

‎examples/voice-demo/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# React + TypeScript + Vite
2+
3+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4+
5+
Currently, two official plugins are available:
6+
7+
- [@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
8+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9+
10+
## Expanding the ESLint configuration
11+
12+
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
13+
14+
- Configure the top-level `parserOptions` property like this:
15+
16+
```js
17+
export default tseslint.config({
18+
languageOptions: {
19+
// other options...
20+
parserOptions: {
21+
project: ['./tsconfig.node.json', './tsconfig.app.json'],
22+
tsconfigRootDir: import.meta.dirname,
23+
},
24+
},
25+
})
26+
```
27+
28+
- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
29+
- Optionally add `...tseslint.configs.stylisticTypeChecked`
30+
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:
31+
32+
```js
33+
// eslint.config.js
34+
import react from 'eslint-plugin-react'
35+
36+
export default tseslint.config({
37+
// Set the react version
38+
settings: { react: { version: '18.3' } },
39+
plugins: {
40+
// Add the react plugin
41+
react,
42+
},
43+
rules: {
44+
// other rules...
45+
// Enable its recommended rules
46+
...react.configs.recommended.rules,
47+
...react.configs['jsx-runtime'].rules,
48+
},
49+
})
50+
```

‎examples/voice-demo/eslint.config.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import js from '@eslint/js'
2+
import globals from 'globals'
3+
import reactHooks from 'eslint-plugin-react-hooks'
4+
import reactRefresh from 'eslint-plugin-react-refresh'
5+
import tseslint from 'typescript-eslint'
6+
7+
export default tseslint.config(
8+
{ ignores: ['dist'] },
9+
{
10+
extends: [js.configs.recommended, ...tseslint.configs.recommended],
11+
files: ['**/*.{ts,tsx}'],
12+
languageOptions: {
13+
ecmaVersion: 2020,
14+
globals: globals.browser,
15+
},
16+
plugins: {
17+
'react-hooks': reactHooks,
18+
'react-refresh': reactRefresh,
19+
},
20+
rules: {
21+
...reactHooks.configs.recommended.rules,
22+
'react-refresh/only-export-components': [
23+
'warn',
24+
{ allowConstantExport: true },
25+
],
26+
},
27+
},
28+
)

‎examples/voice-demo/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + TS</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

‎examples/voice-demo/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "voice-demo",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "tsc -b && vite build",
9+
"lint": "eslint .",
10+
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"@browserai/browserai": "^1.0.29",
14+
"@emotion/styled": "^11.14.0",
15+
"react": "^19.0.0",
16+
"react-dom": "^19.0.0"
17+
},
18+
"devDependencies": {
19+
"@eslint/js": "^9.19.0",
20+
"@types/react": "^19.0.8",
21+
"@types/react-dom": "^19.0.3",
22+
"@vitejs/plugin-react": "^4.3.4",
23+
"eslint": "^9.19.0",
24+
"eslint-plugin-react-hooks": "^5.0.0",
25+
"eslint-plugin-react-refresh": "^0.4.18",
26+
"globals": "^15.14.0",
27+
"typescript": "~5.7.2",
28+
"typescript-eslint": "^8.22.0",
29+
"vite": "^6.1.0"
30+
}
31+
}

‎examples/voice-demo/public/vite.svg

+1
Loading

‎examples/voice-demo/src/App.css

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
7+
8+
.logo {
9+
height: 6em;
10+
padding: 1.5em;
11+
will-change: filter;
12+
transition: filter 300ms;
13+
}
14+
.logo:hover {
15+
filter: drop-shadow(0 0 2em #646cffaa);
16+
}
17+
.logo.react:hover {
18+
filter: drop-shadow(0 0 2em #61dafbaa);
19+
}
20+
21+
@keyframes logo-spin {
22+
from {
23+
transform: rotate(0deg);
24+
}
25+
to {
26+
transform: rotate(360deg);
27+
}
28+
}
29+
30+
@media (prefers-reduced-motion: no-preference) {
31+
a:nth-of-type(2) .logo {
32+
animation: logo-spin infinite 20s linear;
33+
}
34+
}
35+
36+
.card {
37+
padding: 2em;
38+
}
39+
40+
.read-the-docs {
41+
color: #888;
42+
}

‎examples/voice-demo/src/App.tsx

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { useState } from 'react'
2+
import styled from '@emotion/styled'
3+
import { BrowserAI } from '@browserai/browserai'
4+
import './App.css'
5+
6+
const Container = styled.div`
7+
width: 100%;
8+
min-height: 100vh;
9+
display: flex;
10+
flex-direction: column;
11+
align-items: center;
12+
background-color: #1a1a1a;
13+
color: #ffffff;
14+
padding: 2rem;
15+
`
16+
17+
const Title = styled.h1`
18+
color: #ffffff;
19+
text-align: center;
20+
margin-bottom: 30px;
21+
font-size: 2.5rem;
22+
background: linear-gradient(120deg, #4CAF50, #2196F3);
23+
-webkit-background-clip: text;
24+
-webkit-text-fill-color: transparent;
25+
`
26+
27+
const TranscriptionBox = styled.div`
28+
border: 1px solid rgba(255, 255, 255, 0.1);
29+
border-radius: 16px;
30+
width: 100%;
31+
max-width: 600px;
32+
min-height: 200px;
33+
padding: 20px;
34+
margin: 20px 0;
35+
background: rgba(42, 42, 42, 0.7);
36+
backdrop-filter: blur(10px);
37+
`
38+
39+
const ActionButton = styled.button<{ isRecording?: boolean }>`
40+
background: ${props => props.isRecording ?
41+
'linear-gradient(135deg, #ff4444, #cc0000)' :
42+
'linear-gradient(135deg, #4CAF50, #45a049)'};
43+
color: white;
44+
padding: 14px 28px;
45+
border-radius: 30px;
46+
border: none;
47+
cursor: pointer;
48+
font-size: 1.1rem;
49+
transition: all 0.3s ease;
50+
51+
&:hover {
52+
opacity: 0.9;
53+
transform: translateY(-2px);
54+
}
55+
56+
&:disabled {
57+
opacity: 0.5;
58+
cursor: not-allowed;
59+
transform: none;
60+
}
61+
`
62+
63+
function App() {
64+
const [isRecording, setIsRecording] = useState(false)
65+
const [transcription, setTranscription] = useState('')
66+
const [status, setStatus] = useState('Click Start to begin recording')
67+
const [audioAI] = useState(new BrowserAI())
68+
const [isModelLoaded, setIsModelLoaded] = useState(false)
69+
70+
const loadModel = async () => {
71+
try {
72+
setStatus('Loading model...')
73+
await audioAI.loadModel('whisper-tiny-en')
74+
setIsModelLoaded(true)
75+
setStatus('Ready to record')
76+
} catch (error) {
77+
console.error('Error loading model:', error)
78+
setStatus('Error loading model')
79+
}
80+
}
81+
82+
const startRecording = async () => {
83+
try {
84+
setIsRecording(true)
85+
setStatus('Recording...')
86+
await audioAI.startRecording()
87+
} catch (error) {
88+
console.error('Recording error:', error)
89+
setStatus('Error starting recording')
90+
setIsRecording(false)
91+
}
92+
}
93+
94+
const stopRecording = async () => {
95+
try {
96+
setStatus('Processing...')
97+
const audioBlob = await audioAI.stopRecording()
98+
setIsRecording(false)
99+
100+
if (audioBlob) {
101+
const result = await audioAI.transcribeAudio(audioBlob)
102+
const text = (result as { text: string })?.text || ''
103+
setTranscription(text)
104+
setStatus('Ready to record')
105+
}
106+
} catch (error) {
107+
console.error('Processing error:', error)
108+
setStatus('Error processing audio')
109+
setIsRecording(false)
110+
}
111+
}
112+
113+
return (
114+
<Container>
115+
<Title>Voice Demo</Title>
116+
117+
{!isModelLoaded && (
118+
<ActionButton onClick={loadModel}>
119+
Load Voice Models
120+
</ActionButton>
121+
)}
122+
123+
{isModelLoaded && (
124+
<>
125+
<ActionButton
126+
onClick={isRecording ? stopRecording : startRecording}
127+
isRecording={isRecording}
128+
>
129+
{isRecording ? 'Stop Recording' : 'Start Recording'}
130+
</ActionButton>
131+
132+
<TranscriptionBox>
133+
<p style={{ color: '#888', marginBottom: '10px' }}>{status}</p>
134+
{transcription && (
135+
<p style={{ color: '#fff' }}>{transcription}</p>
136+
)}
137+
</TranscriptionBox>
138+
</>
139+
)}
140+
</Container>
141+
)
142+
}
143+
144+
export default App
+1
Loading

‎examples/voice-demo/src/index.css

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
:root {
2+
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3+
line-height: 1.5;
4+
font-weight: 400;
5+
color-scheme: light dark;
6+
background-color: #1a1a1a;
7+
}
8+
9+
body {
10+
margin: 0;
11+
padding: 0;
12+
min-width: 320px;
13+
min-height: 100vh;
14+
}
15+
16+
#root {
17+
width: 100%;
18+
min-height: 100vh;
19+
}
20+
21+
a {
22+
font-weight: 500;
23+
color: #646cff;
24+
text-decoration: inherit;
25+
}
26+
a:hover {
27+
color: #535bf2;
28+
}
29+
30+
h1 {
31+
font-size: 3.2em;
32+
line-height: 1.1;
33+
}
34+
35+
button {
36+
border-radius: 8px;
37+
border: 1px solid transparent;
38+
padding: 0.6em 1.2em;
39+
font-size: 1em;
40+
font-weight: 500;
41+
font-family: inherit;
42+
background-color: #1a1a1a;
43+
cursor: pointer;
44+
transition: border-color 0.25s;
45+
}
46+
button:hover {
47+
border-color: #646cff;
48+
}
49+
button:focus,
50+
button:focus-visible {
51+
outline: 4px auto -webkit-focus-ring-color;
52+
}
53+
54+
@media (prefers-color-scheme: light) {
55+
:root {
56+
color: #213547;
57+
background-color: #ffffff;
58+
}
59+
a:hover {
60+
color: #747bff;
61+
}
62+
button {
63+
background-color: #f9f9f9;
64+
}
65+
}

‎examples/voice-demo/src/main.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { StrictMode } from 'react'
2+
import { createRoot } from 'react-dom/client'
3+
import './index.css'
4+
import App from './App.tsx'
5+
6+
createRoot(document.getElementById('root')!).render(
7+
<StrictMode>
8+
<App />
9+
</StrictMode>,
10+
)

‎examples/voice-demo/src/vite-env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

‎examples/voice-demo/tsconfig.app.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
4+
"target": "ES2020",
5+
"useDefineForClassFields": true,
6+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"module": "ESNext",
8+
"skipLibCheck": true,
9+
10+
/* Bundler mode */
11+
"moduleResolution": "bundler",
12+
"allowImportingTsExtensions": true,
13+
"isolatedModules": true,
14+
"moduleDetection": "force",
15+
"noEmit": true,
16+
"jsx": "react-jsx",
17+
18+
/* Linting */
19+
"strict": true,
20+
"noUnusedLocals": true,
21+
"noUnusedParameters": true,
22+
"noFallthroughCasesInSwitch": true,
23+
"noUncheckedSideEffectImports": true
24+
},
25+
"include": ["src"]
26+
}

‎examples/voice-demo/tsconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.app.json" },
5+
{ "path": "./tsconfig.node.json" }
6+
]
7+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
4+
"target": "ES2022",
5+
"lib": ["ES2023"],
6+
"module": "ESNext",
7+
"skipLibCheck": true,
8+
9+
/* Bundler mode */
10+
"moduleResolution": "bundler",
11+
"allowImportingTsExtensions": true,
12+
"isolatedModules": true,
13+
"moduleDetection": "force",
14+
"noEmit": true,
15+
16+
/* Linting */
17+
"strict": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedSideEffectImports": true
22+
},
23+
"include": ["vite.config.ts"]
24+
}

‎examples/voice-demo/vite.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
import react from '@vitejs/plugin-react'
3+
4+
// https://vite.dev/config/
5+
export default defineConfig({
6+
plugins: [react()],
7+
})

‎vite.config.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from 'vite';
2+
import react from '@vitejs/plugin-react';
3+
4+
export default defineConfig({
5+
plugins: [react()],
6+
build: {
7+
commonjsOptions: {
8+
include: [/@browserai\/browserai/, /node_modules/]
9+
}
10+
},
11+
optimizeDeps: {
12+
include: ['@browserai/browserai']
13+
}
14+
});

0 commit comments

Comments
 (0)
Please sign in to comment.