This repository has been archived by the owner on Jan 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
174 lines (133 loc) · 4.66 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
import {
ChakraProvider,
Center, VStack, Heading, Box,
FormControl, FormLabel, Input,
FormHelperText, Link, Button,
FormErrorMessage, Text
} from "@chakra-ui/react";
import { useEffect, useState } from "react";
import { useFormik } from 'formik';
import { object, string } from 'yup';
import ReactPlayer from 'react-player'
function App() {
const [defaultAudio, setDefaultAudio] = useState(null);
const [playerURL, setPlayerURL] = useState(null);
const [loading, setLoading] = useState(false);
const [response, setResponse] = useState();
const api = 'https://api.gladia.io/v2'
, format = new RegExp('^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$')
, providedFile = 'rimbaud-sensation.wav'
;
useEffect(() => {
(async () => {
setDefaultAudio(
new File([await (await fetch('/' + providedFile)).blob()], providedFile, { type: 'audio/wav' })
)
})()
}, []);
const formik = useFormik({
initialValues: {
key: '',
file: ''
},
onSubmit: async values => {
const data = new FormData();
data.append("audio", values.file || defaultAudio);
setPlayerURL(URL.createObjectURL(values.file || defaultAudio));
setResponse(false);
setLoading(true);
try {
// Task 1 - Main Part of the API Integration - START
const upload = await fetch(api + '/upload', {
method: 'POST',
headers: { 'x-gladia-key': values.key },
body: data
}).then(r => r.json());
const transcription = await fetch(api + '/transcription', {
method: 'POST',
headers: {
'x-gladia-key': values.key,
'Content-Type': 'application/json'
},
body: JSON.stringify({ audio_url: upload.audio_url })
}).then(r => r.json());
const repeat = () => {
const check = setTimeout(async () => {
const actual = await fetch(transcription.result_url, {
method: 'GET',
headers: { 'x-gladia-key': values.key }
}).then(r => r.json());
clearTimeout(check);
if (actual.status === 'done') {
setResponse(actual.result.transcription.full_transcript);
setLoading(false)
} else repeat()
}, 500)
}
repeat()
// Task 1 - Main Part of the API Integration - END
} catch (error) {
setResponse(error);
setLoading(false)
}
},
validationSchema: object({
key: string().matches(format, "Does not match the Gladia format").required("Required")
})
});
return (
<ChakraProvider>
<Center>
<VStack>
<Heading as='h1' size='xl' mt='20'>Speech-to-Text API</Heading>
<Box w="320">
<form onSubmit={formik.handleSubmit}>
<FormControl mt='10' isInvalid={formik.touched.key && formik.errors.key} isRequired>
<FormLabel htmlFor='key'>Enter your Gladia API key</FormLabel>
<Input
id='key'
name='key'
type='text'
onChange={formik.handleChange}
value={formik.values.key}
/>
<FormHelperText>
Generate your key from <Link color='blue' href='https://app.gladia.io/account'>your account</Link>.
</FormHelperText>
<FormErrorMessage>{formik.errors.key}</FormErrorMessage>
</FormControl>
<FormControl mt='10'>
<FormLabel htmlFor='file'>Select an audio file</FormLabel>
<input
id='file'
name='file'
type='file' accept='audio/*'
onChange={(event) => {
formik.setFieldValue("file", event.currentTarget.files[0])
}}
/>
<FormHelperText>
A default audio file will be sent if you do not select any
</FormHelperText>
</FormControl>
<Button type='submit' mt='10' w='100%' colorScheme='green' isLoading={loading}>Submit</Button>
</form>
</Box>
{
response && <Box mt='20' w='80%'>
<ReactPlayer
url={playerURL}
controls={true}
playing={false}
width="100%"
height="50px"
/>
<Text mt='10' mb='20' fontSize='xl'>{response}</Text>
</Box>
}
</VStack>
</Center>
</ChakraProvider>
)
}
export default App;