-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
215 lines (207 loc) · 7.4 KB
/
App.js
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
import React, {useEffect, useState} from 'react';
import {
StyleSheet,
ScrollView,
View,
Text,
TouchableOpacity,
SafeAreaView,
Platform,
PermissionsAndroid,
} from 'react-native';
import RNFS from 'react-native-fs';
import {initWhisper} from 'whisper.rn';
import {
PrimaryButton,
ButtonText,
LabelText,
} from '../../components/reusable/UIComponentsBeeApp';
const fileDir = `${RNFS.DocumentDirectoryPath}/whisper`;
const recordFile = `${fileDir}/realtime.wav`
// const modelHost =
// 'https://huggingface.co/TheAIchemist13/whisper-tiny-hi/resolve/main'; //change for language #switch
const modelHost =
'https://huggingface.co/TheAIchemist13/whisper-ind-cpp-models/resolve/main';
const createDir = async () => {
if (!(await RNFS.exists(fileDir))) {
console.log('Creating dir--', fileDir);
await RNFS.mkdir(fileDir);
}
};
const progress = params => {
console.log('params: ', params);
};
export default function WhisperScreenTest() {
const [whisperContext, setWhisperContext] = useState(null);
const [transcibeResult, setTranscibeResult] = useState(null);
const [stopTranscribe, setStopTranscribe] = useState(null);
const reqPermissions = async () => {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
);
console.log('granted: ', granted);
};
useEffect(() => {
reqPermissions();
}, []);
return (
<SafeAreaView style={styles.container}>
<View style={styles.buttons}>
<TouchableOpacity
style={styles.button}
onPress={async () => {
if (whisperContext) {
console.log('Found previous context');
await whisperContext.release();
setWhisperContext(null);
console.log('Released previous context');
}
await createDir();
const modelFilePath = `${fileDir}/ggml-model-kn.bin`; //change
// const modelFilePath = `${fileDir}/ggml-model-hi.bin`; //change for language #switch
console.log('fileDir: ', fileDir);
if (await RNFS.exists(modelFilePath)) {
console.log('Model already exists: at', modelFilePath);
} else {
console.log('---Local model not accessible');
const {promise: downloadPromise} = RNFS.downloadFile({
fromUrl: `${modelHost}/ggml-model-kn.bin`, //change for language #switch
// fromUrl: `${modelHost}/ggml-model-hi.bin`, //change for language #switch
toFile: modelFilePath,
readTimeout: 60000,
progress: res => {
let progressPercent =
(res.bytesWritten / res.contentLength) * 100; // to calculate in percentage
console.log('\n\nprogress===', progressPercent);
},
});
downloadPromise
.then(result => {
console.log('result', result);
console.log('result:-statuscode', result.statusCode);
console.log('---success download if statuscode 200');
})
.catch(err => {
console.log('err: ', err);
});
}
}}>
<Text style={styles.buttonText}>Initialize Model</Text>
</TouchableOpacity>
</View>
{/* --Initialize context */}
<View style={styles.buttons}>
<TouchableOpacity
style={[
styles.button,
stopTranscribe?.stop ? styles.buttonClear : null,
]}
onPress={async () => {
if (whisperContext) {
console.log('Found previous context');
await whisperContext.release();
setWhisperContext(null);
console.log('Released previous context');
}
const modelFilePath = `${fileDir}/ggml-model-kn.bin`; //change for language
// const modelFilePath = `${fileDir}/ggml-model-hi.bin`; //change for language #switch
if (await RNFS.exists(modelFilePath)) {
console.log('Model already exists: at', modelFilePath);
console.log('Initialize context...');
const options = {language: 'kn'}; //change for language #switch
// const options = {language: 'hi'}; //change for language #switch
const ctx = await initWhisper({filePath: modelFilePath}, options);
console.log('ctx: ', ctx);
console.log('Loaded model, ID:', ctx.id);
setWhisperContext(ctx);
} else {
console.log('model doesnt exist at location');
}
}}>
<Text style={styles.buttonText}>{'Initialize context'}</Text>
</TouchableOpacity>
</View>
{/* --Transcribe Result */}
<View style={styles.buttons}>
<TouchableOpacity
style={[
styles.button,
stopTranscribe?.stop ? styles.buttonClear : null,
]}
onPress={async () => {
if (!whisperContext) {
console.log('No Context');
}
if (stopTranscribe?.stop) {
const t0 = Date.now();
await stopTranscribe?.stop();
const t1 = Date.now();
console.log('Stopped transcribing in', t1 - t0, 'ms');
setStopTranscribe(null);
return;
}
console.log(
'Start realtime transcribing with context',
whisperContext,
);
try {
// await createDir();
const {stop, subscribe} = await whisperContext
.transcribeRealtime
// {
// language: 'kn', //change for language
// },
();
setStopTranscribe({stop});
subscribe(evt => {
const {isCapturing, data, processTime, recordingTime} = evt;
setTranscibeResult(
`Realtime transcribing: ${isCapturing ? 'ON' : 'OFF'}\n` +
`Result: ${data?.result}\n\n` +
`Process time: ${processTime}ms\n` +
`Recording time: ${recordingTime}ms`,
);
if (!isCapturing) {
setStopTranscribe(null);
}
});
} catch (e) {
console.log('Error:', e);
}
}}>
<Text style={styles.buttonText}>
{stopTranscribe?.stop
? 'Stop Realtime Transcribing'
: 'Start Realtime Transcribing'}
</Text>
</TouchableOpacity>
</View>
{transcibeResult && (
<View style={styles.logContainer}>
<Text style={styles.logText}>{transcibeResult}</Text>
</View>
)}
</SafeAreaView>
);
}
const styles = StyleSheet.create({
scrollview: {flexGrow: 1, justifyContent: 'center'},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'flex-start',
padding: 40,
},
buttons: {flexDirection: 'row'},
button: {margin: 4, backgroundColor: '#333', borderRadius: 4, padding: 8},
buttonClear: {backgroundColor: '#888'},
buttonText: {fontSize: 16, color: 'white', textAlign: 'center'},
logContainer: {
backgroundColor: 'lightgray',
padding: 8,
width: '95%',
borderRadius: 8,
marginVertical: 8,
},
logText: {fontSize: 18, color: '#333'},
});