-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
110 lines (98 loc) · 2.86 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
import React from 'react';
import {
ChakraProvider,
Box,
VStack,
Grid,
theme,
} from '@chakra-ui/react';
import { ColorModeSwitcher } from './ColorModeSwitcher';
import { Logo } from './Logo';
import {useState, useEffect} from 'react';
import {Recorder} from 'react-voice-recorder';
import 'react-voice-recorder/dist/index.css';
import axios from 'axios';
import Status from './Status';
import Result from './Result';
const assemblyApi = axios.create({
baseURL: 'https://api.assemblyai.com/v2',
headers: {
authorization: process.env.REACT_APP_ASSEMBLY_API_KEY,
'content-type': 'application/json',
},
});
const initialState = {
url: null,
blob: null,
chunks: null,
duration: {
h: 0,
m: 0,
s: 0,
},
}
function App() {
const [audioDetails, setAudioDetails] = useState(initialState);
const [transcript, setTranscript] = useState({ id: '' });
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
const interval = setInterval(async () => {
if (transcript.id && transcript.status !== 'completed' && isLoading) {
try {
const { data: transcriptData} = await assemblyApi.get(
`/transcript/${transcript.id}`
);
setTranscript({ ...transcript, ...transcriptData });
} catch (err) {
console.error(err);
}
} else {
setIsLoading(false);
clearInterval(interval);
}
}, 1000);
return () => clearInterval(interval);
}, [isLoading, transcript]);
const handleAudioStop = (data) => {
setAudioDetails(data);
};
const handleReset = () => {
setAudioDetails({...initialState});
setTranscript({ id: ''});
};
const handleAudioUpload = async (audioFile) => {
setIsLoading(true);
const { data: uploadResponse } = await assemblyApi.post('/upload', audioFile);
const { data } = await assemblyApi.post('/transcript', {
audio_url: uploadResponse.upload_url,
sentiment_analysis: true,
entity_detection: true,
iab_categories: true,
});
setTranscript({ id: data.id });
};
return (
<ChakraProvider theme={theme}>
<Box textAlign="center" fontSize="xl">
<Grid minH="100vh" p={3}>
<ColorModeSwitcher justifySelf="flex-end" />
<VStack spacing={8}>
<Box>
{transcript.text && transcript.status === 'completed' ? (<Result transcript={transcript} />) : (<Status isLoading={isLoading} status={transcript.status} />)}
</Box>
<Box width ={1000}>
<Recorder
record={true}
audioURL={audioDetails.url}
handleAudioStop={handleAudioStop}
handleAudioUpload={handleAudioUpload}
handleReset={handleReset}
/>
</Box>
</VStack>
</Grid>
</Box>
</ChakraProvider>
);
}
export default App;