|
| 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 |
0 commit comments