forked from supabase-community/babelfish.ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.jsx
182 lines (160 loc) · 5.82 KB
/
receiver.jsx
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
import { useEffect, useRef, useState } from 'react';
import LanguageSelector from '../components/LanguageSelectorReceiver';
import Progress from '../components/Progress';
import GitHubLink from '../components/GitHubLink';
import { LANGUAGES, languageMapping } from '../utils/languages';
import { useParams } from 'react-router-dom';
function App({ supabase }) {
// Model loading
const [ready, setReady] = useState(null);
const disabled = useRef(false);
const [progressItems, setProgressItems] = useState([]);
// Inputs and outputs
const [input, setInput] = useState('Hallo.');
const inputRef = useRef(input);
const [sourceLanguage, setSourceLanguage] = useState('deu_Latn');
const sourceLanguageRef = useRef(sourceLanguage);
const [targetLanguage, setTargetLanguage] = useState('eng_Latn');
const targetLanguageRef = useRef(targetLanguage);
const [output, setOutput] = useState('');
// Broadcast
const { channelId } = useParams();
// Create a reference to the worker object.
const worker = useRef(null);
// We use the `useEffect` hook to setup the worker as soon as the `App` component is mounted.
useEffect(() => {
if (!worker.current) {
// Create the worker if it does not yet exist.
worker.current = new Worker(
new URL('../translationWorker.js', import.meta.url),
{
type: 'module',
}
);
}
// Create a callback function for messages from the worker thread.
const onMessageReceived = (e) => {
switch (e.data.status) {
case 'initiate':
// Model file start load: add a new progress item to the list.
setReady(false);
setProgressItems((prev) => [...prev, e.data]);
break;
case 'progress':
// Model file progress: update one of the progress items.
setProgressItems((prev) =>
prev.map((item) => {
if (item.file === e.data.file) {
return { ...item, progress: e.data.progress };
}
return item;
})
);
break;
case 'done':
// Model file loaded: remove the progress item from the list.
setProgressItems((prev) =>
prev.filter((item) => item.file !== e.data.file)
);
break;
case 'ready':
// Pipeline ready: the worker is ready to accept messages.
setReady(true);
break;
case 'update':
// Generation update: update the output text.
setOutput(e.data.output);
break;
case 'complete':
setOutput(e.data.output[0].translation_text);
disabled.current = false;
break;
}
};
// Attach the callback function as an event listener.
worker.current.addEventListener('message', onMessageReceived);
// Define a cleanup function for when the component is unmounted.
return () =>
worker.current.removeEventListener('message', onMessageReceived);
});
const translate = () => {
if (disabled.current) return;
if (sourceLanguageRef.current === targetLanguageRef.current) {
setOutput(inputRef.current);
return;
}
disabled.current = true;
console.log('Translating...');
worker.current.postMessage({
text: inputRef.current,
src_lang: sourceLanguageRef.current,
tgt_lang: targetLanguageRef.current,
});
};
// Start on load
useEffect(() => {
translate();
// Subscribe to Supabase realtime broadcast
const channel = supabase.channel(channelId);
channel
.on('broadcast', { event: 'transcript' }, ({ payload }) => {
setInput(payload.message);
inputRef.current = payload.message;
setSourceLanguage(languageMapping[payload.language]);
sourceLanguageRef.current = languageMapping[payload.language];
translate();
})
.subscribe();
}, []);
return (
<div className="flex flex-col h-screen mx-auto justify-end text-gray-800 bg-white">
<div className="h-full overflow-auto scrollbar-thin flex justify-center items-center flex-col relative">
<GitHubLink url="https://github.com/supabase-community/babelfish.ai" />
<div className="flex flex-col items-center mb-1 max-w-[400px] text-center">
<h1 className="text-4xl font-bold mb-1">Babelfish.ai - Receiver</h1>
<h2 className="text-xl font-semibold">
Real-time in-browser speech recognition & decentralized in-browser
AI translation.
</h2>
</div>
<div className="w-[500px] p-2">
<div className="relative">
<h3 className="text-l font-semibold">
Transcript:{' '}
{
Object.entries(LANGUAGES).find(
([key, val]) => val === sourceLanguage
)?.[0]
}
</h3>
</div>
<p className="w-full h-[80px] overflow-y-auto overflow-wrap-anywhere border rounded-lg p-2">
{input}
</p>
<div className="textbox-container">
<LanguageSelector
type={'Target'}
defaultLanguage={targetLanguage}
onChange={(x) => {
setTargetLanguage(x.target.value);
targetLanguageRef.current = x.target.value;
}}
/>
</div>
<p className="w-full h-[80px] overflow-y-auto overflow-wrap-anywhere border rounded-lg p-2">
{output}
</p>
</div>
<div className="progress-bars-container">
{ready === false && <label>Loading models... (only run once)</label>}
{progressItems.map((data) => (
<div key={data.file}>
<Progress text={data.file} percentage={data.progress} />
</div>
))}
</div>
</div>
</div>
);
}
export default App;