-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.tsx
380 lines (337 loc) · 12.2 KB
/
main.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import React, { useCallback, useEffect, useRef, useState } from 'react'
import ReactDOM from 'react-dom/client'
import axios from 'axios'
import { v4 as uuidv4 } from 'uuid'
import { SimliClient } from './SimliClient'
const sk = import.meta.env.VITE_SIMLI_API_KEY
const e11 = import.meta.env.VITE_ELEVENLABS_API_KEY
const completionEndpoint = import.meta.env?.VITE_COMPLETION_ENDPOINT || 'http://localhost:3000'
import './styles.css'
const AGENT_ID = 'ruby' // this comes from the agentId output from running the Eliza framework, it likely will be in uuid format, i.e. '123e4567-e89b-12d3-a456-426614174000'
const SIMLI_FACE_ID = '13fbb3e1-4489-4199-ad57-91be4a2dd38b'
const ELEVENLABS_VOICE_ID = '21m00Tcm4TlvDq8ikWAM'
const simliClient = new SimliClient()
const App = () => {
const [inputText, setInputText] = useState('')
const [isLoading, setIsLoading] = useState(false)
const [isConnecting, setIsConnecting] = useState(false)
const [error, setError] = useState('')
const [_, setChatgptText] = useState('')
const [startWebRTC, setStartWebRTC] = useState(false)
const [isListening, setIsListening] = useState(false)
const cancelTokenRef = useRef<any | null>(null)
const videoRef = useRef<HTMLVideoElement | null>(null)
const audioRef = useRef<HTMLAudioElement | null>(null)
const mediaRecorderRef = useRef<MediaRecorder | null>(null)
const chunksRef = useRef<Blob[]>([])
const audioContextRef = useRef<any | null>(null)
const analyserRef = useRef<any | null>(null)
const microphoneRef = useRef<any | null>(null)
// TODO: populate these from localStorage if roomid and useruuid are set, otherwise generate a random uuid
const [roomID, setRoomID] = useState('')
const [userUUID, setUserUUID] = useState('')
useEffect(() => {
const storedRoomID = localStorage.getItem('roomID')
const storedUserUUID = localStorage.getItem('userUUID')
if (storedRoomID && storedUserUUID) {
setRoomID(storedRoomID)
setUserUUID(storedUserUUID)
} else {
const newRoomID = uuidv4()
const newUserUUID = uuidv4()
setRoomID(newRoomID)
setUserUUID(newUserUUID)
localStorage.setItem('roomID', newRoomID)
localStorage.setItem('userUUID', newUserUUID)
}
}, [])
const initializeSimliClient = useCallback(() => {
if (videoRef.current && audioRef.current) {
const SimliConfig = {
apiKey: sk,
faceID: SIMLI_FACE_ID,
handleSilence: true,
videoRef: videoRef,
audioRef: audioRef,
}
simliClient.Initialize(SimliConfig)
console.log('Simli Client initialized')
}
}, [])
useEffect(() => {
initializeSimliClient()
const handleConnected = () => {
console.log('SimliClient is now connected!')
}
const handleDisconnected = () => {
console.log('SimliClient has disconnected!')
}
const handleFailed = () => {
console.log('SimliClient has failed to connect!')
setError('Failed to connect to Simli. Please try again.')
}
const handleStarted = () => {
console.log('SimliClient has started!')
setIsLoading(false)
setIsConnecting(false)
}
simliClient.on('connected', handleConnected)
simliClient.on('disconnected', handleDisconnected)
simliClient.on('failed', handleFailed)
simliClient.on('started', handleStarted)
return () => {
simliClient.off('connected', handleConnected)
simliClient.off('disconnected', handleDisconnected)
simliClient.off('failed', handleFailed)
simliClient.off('started', handleStarted)
simliClient.close()
}
}, [initializeSimliClient])
const handleStart = useCallback(() => {
simliClient.start()
setStartWebRTC(true)
setIsLoading(true)
setIsConnecting(true)
setTimeout(() => {
const audioData = new Uint8Array(6000).fill(0)
simliClient.sendAudioData(audioData)
}, 4000)
}, [])
const processInput = useCallback(async (text: any) => {
setIsLoading(true)
setError('')
if (cancelTokenRef.current) {
cancelTokenRef.current.cancel('Operation canceled by the user.')
}
cancelTokenRef.current = axios.CancelToken.source()
try {
console.log('sending input to chatgpt')
const chatGPTResponse = await axios.post(
completionEndpoint + `/${AGENT_ID}/message`,
{
text,
roomId: roomID,
userId: userUUID,
userName: 'User',
},
{
cancelToken: cancelTokenRef.current.token,
}
)
console.log('chatGPTResponse', chatGPTResponse)
const chatGPTText = chatGPTResponse.data[0].text
if (!chatGPTText || chatGPTText.length === 0) {
setError('No response from chatGPT. Please try again.')
return
}
setChatgptText(chatGPTText)
const elevenlabsResponse = await axios.post(
`https://api.elevenlabs.io/v1/text-to-speech/${ELEVENLABS_VOICE_ID}?output_format=pcm_16000`,
{
text: chatGPTText,
model_id: 'eleven_turbo_v2_5',
},
{
headers: {
'xi-api-key': e11,
'Content-Type': 'application/json',
},
responseType: 'arraybuffer',
cancelToken: cancelTokenRef.current.token,
}
)
const pcm16Data = new Uint8Array(elevenlabsResponse.data)
const chunkSize = 6000
for (let i = 0; i < pcm16Data.length; i += chunkSize) {
const chunk = pcm16Data.slice(i, i + chunkSize)
simliClient.sendAudioData(chunk)
}
} catch (err) {
if (axios.isCancel(err)) {
console.log('Request canceled:', err.message)
} else {
setError('An error occurred. Please try again.')
console.error(err)
}
} finally {
setIsLoading(false)
cancelTokenRef.current = null
}
}, [])
const toggleListening = useCallback(() => {
if (isListening) {
console.log('Stopping mic')
stopListening()
} else {
console.log('Starting mic')
startListening()
}
}, [isListening])
const sendAudioToWhisper = useCallback(
async (audioBlob: Blob) => {
const formData = new FormData()
formData.append('file', audioBlob, 'audio.wav')
try {
const response = await axios.post(`${completionEndpoint}/${AGENT_ID}/whisper`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
const transcribedText = response.data.text
await processInput(transcribedText)
} catch (error) {
console.error('Error transcribing audio:', error)
setError('Error transcribing audio. Please try again.')
}
},
[processInput]
)
const startListening = useCallback(() => {
setIsListening(true)
navigator.mediaDevices
.getUserMedia({ audio: true })
.then((stream) => {
if (!audioContextRef.current) {
audioContextRef.current = new (window.AudioContext ||
(window as any).webkitAudioContext)()
}
if (!analyserRef.current) {
analyserRef.current = audioContextRef.current.createAnalyser()
analyserRef.current.fftSize = 512
}
if (microphoneRef.current) {
microphoneRef.current.disconnect()
}
microphoneRef.current = audioContextRef.current.createMediaStreamSource(stream)
microphoneRef.current.connect(analyserRef.current)
mediaRecorderRef.current = new MediaRecorder(stream)
mediaRecorderRef.current.ondataavailable = (event) => {
console.log('Data available:', event.data)
chunksRef.current.push(event.data)
}
mediaRecorderRef.current.onstop = () => {
console.log('Recorder stopped')
const audioBlob = new Blob(chunksRef.current, { type: 'audio/wav' })
sendAudioToWhisper(audioBlob)
chunksRef.current = []
}
mediaRecorderRef.current.start()
})
.catch((err) => {
console.error('Error accessing microphone:', err)
setIsListening(false)
setError('Error accessing microphone. Please check your permissions and try again.')
})
}, [sendAudioToWhisper])
const stopListening = useCallback(() => {
if (mediaRecorderRef.current && mediaRecorderRef.current.state !== 'inactive') {
mediaRecorderRef.current.stop()
}
console.log('Stopping listening')
setIsListening(false)
}, [])
useEffect(() => {
console.log('isListening', isListening)
console.log('chunksRef.current', chunksRef.current)
if (!isListening && chunksRef.current.length > 0) {
console.log('Sending audio to Whisper')
const audioBlob = new Blob(chunksRef.current, { type: 'audio/wav' })
sendAudioToWhisper(audioBlob)
chunksRef.current = []
}
}, [isListening, sendAudioToWhisper])
const handleSubmit = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const input = inputText.trim()
setInputText('')
await processInput(input)
},
[inputText, processInput]
)
return (
<>
<div className='flex h-screen w-full flex-col items-center justify-center font-mono text-white'>
<div className='relative size-full'>
<video
ref={videoRef}
id='simli_video'
autoPlay
playsInline
className='size-full object-cover'
></video>
<audio ref={audioRef} id='simli_audio' autoPlay></audio>
</div>
{startWebRTC && !isConnecting ? (
<>
{/* {chatgptText && <p className='text-center'>{chatgptText}</p>} */}
<form
onSubmit={handleSubmit}
className='fixed bottom-4 mx-4 w-full max-w-md space-y-4 px-4'
>
<div className='flex w-full items-center space-x-2'>
<input
type='text'
value={inputText}
onChange={(e) => setInputText(e.target.value)}
placeholder='Enter your message'
className='grow rounded border border-white bg-black px-3 py-2 text-white focus:outline-none focus:ring-2 focus:ring-white'
/>
<button
type='submit'
disabled={isLoading || !inputText.trim()}
className='rounded bg-white px-3 py-1 text-3xl text-black transition-colors hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-black disabled:opacity-50'
>
{isLoading ? '➡️' : '➡️'}
</button>
<button
type='button'
onClick={toggleListening}
className='rounded bg-blue-500 px-3 py-1 text-2xl text-white transition-colors hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-black'
>
{isListening ? '🔴' : '🎤'}
</button>
</div>
</form>
</>
) : (
<>
{isConnecting && (
<p className='fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2'>
Connecting...
</p>
)}
{!isConnecting && (
<button
disabled={isConnecting}
onClick={handleStart}
className='fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 rounded bg-white px-4 py-2 text-black transition-colors hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-black'
>
Start
</button>
)}
</>
)}
{error && <p className='fixed bottom-20 mt-4 text-center text-red-500'>{error}</p>}
</div>
<div
style={{
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '100%',
backgroundImage: 'url(./bg.jpg)',
backgroundSize: 'cover',
backgroundPosition: 'center',
zIndex: -1000,
}}
/>
</>
)
}
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
)