-
Notifications
You must be signed in to change notification settings - Fork 86
/
App.tsx
330 lines (308 loc) · 8.15 KB
/
App.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
import React, { Component } from 'react';
import {
Modal,
Platform,
TouchableOpacity,
ScrollView,
StyleSheet,
Text,
View,
} from 'react-native';
import {
RhinoManager,
RhinoInference,
RhinoErrors,
} from '@picovoice/rhino-react-native';
import { language, context } from './params.json';
type Props = {};
type State = {
buttonText: string;
buttonDisabled: boolean;
rhinoText: string;
isListening: boolean;
isError: boolean;
errorMessage: string;
showContextInfo: boolean;
};
const marginOffset = Platform.OS === 'ios' ? 30 : 0;
export default class App extends Component<Props, State> {
readonly _accessKey: string = '${YOUR_ACCESS_KEY_HERE}'; // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
_rhinoManager: RhinoManager | undefined;
constructor(props: Props) {
super(props);
this.state = {
buttonText: 'Start',
buttonDisabled: false,
rhinoText: '',
isListening: false,
isError: false,
errorMessage: '',
showContextInfo: false,
};
}
async componentDidMount() {
let contextPath = `contexts/${context}_${Platform.OS}.rhn`;
let modelPath: string | undefined;
if (language !== 'en') {
modelPath = `models/rhino_params_${language}.pv`;
}
try {
this._rhinoManager = await RhinoManager.create(
this._accessKey,
contextPath,
this.inferenceCallback.bind(this),
(error) => {
this.errorCallback(error.message);
},
modelPath,
);
} catch (err) {
let errorMessage;
if (err instanceof RhinoErrors.RhinoInvalidArgumentError) {
errorMessage = err.message;
} else if (err instanceof RhinoErrors.RhinoActivationError) {
errorMessage = 'AccessKey activation error';
} else if (err instanceof RhinoErrors.RhinoActivationLimitError) {
errorMessage = 'AccessKey reached its device limit';
} else if (err instanceof RhinoErrors.RhinoActivationRefusedError) {
errorMessage = 'AccessKey refused';
} else if (err instanceof RhinoErrors.RhinoActivationThrottledError) {
errorMessage = 'AccessKey has been throttled';
} else {
errorMessage = err.toString();
}
this.errorCallback(errorMessage);
}
}
inferenceCallback(inference: RhinoInference) {
this.setState({
rhinoText: this.prettyPrint(inference),
buttonText: 'Start',
buttonDisabled: false,
isListening: false,
});
}
errorCallback(error: string) {
this.setState({
isError: true,
errorMessage: error,
});
}
prettyPrint(inference: RhinoInference): string {
let printText = `{\n "isUnderstood" : "${inference.isUnderstood}",\n`;
if (inference.isUnderstood) {
printText += ` "intent" : "${inference.intent}",\n`;
if (Object.entries(inference.slots).length > 0) {
printText += ' "slots" : {\n';
for (let [key, slot] of Object.entries(inference.slots)) {
printText += ` "${key}" : "${slot}",\n`;
}
printText += ' }\n';
}
}
printText += '}';
return printText;
}
componentWillUnmount() {
this._rhinoManager?.delete();
}
async _startProcessing() {
if (this.state.isListening) {
return;
}
this.setState({
buttonDisabled: true,
});
try {
await this._rhinoManager?.process();
this.setState({
buttonText: '...',
rhinoText: '',
buttonDisabled: false,
isListening: true,
});
} catch (e: any) {
this.setState({
isError: true,
errorMessage: e.message,
});
}
}
showContextInfo() {
if (!this.state.isError) {
this.setState({ showContextInfo: true });
}
}
render() {
return (
<View style={[styles.container]}>
<View style={styles.statusBar}>
<Text style={styles.statusBarText}>Rhino</Text>
<View style={styles.statusBarButtonContainer}>
<TouchableOpacity
style={{ backgroundColor: '#ffd105' }}
onPress={() => this.showContextInfo()}>
<Text style={styles.statusBarButtonStyle}>Context Info</Text>
</TouchableOpacity>
</View>
</View>
<Modal
animationType="slide"
transparent={true}
visible={this.state.showContextInfo}>
<View style={styles.modalView}>
<ScrollView style={{ flex: 0.95, marginBottom: 10 }}>
<Text>{this._rhinoManager?.contextInfo}</Text>
</ScrollView>
<TouchableOpacity
style={{
alignSelf: 'center',
justifyContent: 'center',
backgroundColor: '#377DFF',
padding: 3,
flex: 0.05,
}}
onPress={() => this.setState({ showContextInfo: false })}>
<Text style={{ color: 'white' }}>CLOSE</Text>
</TouchableOpacity>
</View>
</Modal>
<View style={{ flex: 1, padding: 20 }}>
<View
style={{
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
padding: 30,
backgroundColor: '#25187E',
}}>
<Text style={styles.rhinoText}>{this.state.rhinoText}</Text>
</View>
</View>
{this.state.isError && (
<View style={styles.errorBox}>
<Text
style={{
color: 'white',
fontSize: 16,
}}>
{this.state.errorMessage}
</Text>
</View>
)}
<View
style={{
flex: 0.35,
justifyContent: 'center',
alignContent: 'center',
}}>
<TouchableOpacity
style={{
width: '50%',
height: '50%',
alignSelf: 'center',
justifyContent: 'center',
backgroundColor: this.state.isError ? '#cccccc' : '#377DFF',
borderRadius: 100,
}}
onPress={() => this._startProcessing()}
disabled={this.state.buttonDisabled || this.state.isError}>
<Text style={styles.buttonText}>{this.state.buttonText}</Text>
</TouchableOpacity>
</View>
<View
style={{ flex: 0.08, justifyContent: 'flex-end', paddingBottom: 25 }}>
<Text style={styles.instructions}>
Made in Vancouver, Canada by Picovoice
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
backgroundColor: '#F5FCFF',
},
subContainer: {
flex: 1,
justifyContent: 'center',
},
statusBar: {
flex: 0.2,
backgroundColor: '#377DFF',
justifyContent: 'space-between',
flexDirection: 'row',
alignItems: 'center',
},
statusBarText: {
fontSize: 18,
color: 'white',
fontWeight: 'bold',
marginLeft: 15,
marginTop: marginOffset,
},
statusBarButtonContainer: {
marginRight: 5,
marginTop: marginOffset,
},
statusBarButtonStyle: {
padding: 7.5,
fontWeight: '500',
},
buttonStyle: {
backgroundColor: '#377DFF',
borderRadius: 100,
},
buttonText: {
fontSize: 30,
fontWeight: 'bold',
color: 'white',
textAlign: 'center',
},
rhinoText: {
flex: 1,
flexWrap: 'wrap',
color: 'white',
fontSize: 20,
},
itemStyle: {
fontWeight: 'bold',
fontSize: 20,
textAlign: 'center',
},
instructions: {
textAlign: 'center',
color: '#666666',
},
errorBox: {
backgroundColor: 'red',
borderRadius: 5,
margin: 20,
marginTop: 5,
marginBottom: 5,
padding: 10,
textAlign: 'center',
},
modalView: {
margin: 10,
marginTop: 10 + marginOffset,
backgroundColor: 'white',
borderRadius: 10,
padding: 10,
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
flex: 1,
flexDirection: 'column',
justifyContent: 'space-between',
},
});