Created
April 29, 2023 15:15
-
-
Save dueka/6637d599280f3a67b020c792801e98b7 to your computer and use it in GitHub Desktop.
App.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const startTranscribe = async () => { | |
setIsTranscribing(true); | |
const recordingPath = `${RNFS.DocumentDirectoryPath}/audioRecordings`; | |
const fileName = 'recording.wav'; | |
const filePath = `${recordingPath}/${fileName}`; | |
// Read the contents of the recorded file | |
if (!(await RNFS.exists(recordingPath))) { | |
await RNFS.mkdir(recordingPath); | |
} | |
console.log('start transcribing'); | |
RNFetchBlob.fs | |
.readFile(filePath, 'base64') | |
.then(data => { | |
const formData = new FormData(); | |
formData.append('model', 'whisper-1'); | |
formData.append('file', { | |
uri: Platform.OS === 'android' ? `file://${filePath}` : filePath, | |
type: 'audio/wav', | |
name: fileName, | |
}); | |
axios({ | |
url: 'https://api.openai.com/v1/audio/transcriptions', | |
method: 'POST', | |
data: formData, | |
headers: { | |
Accept: 'application/json', | |
'Content-Type': 'multipart/form-data', | |
Authorization: `Bearer ${OPEN_API_KEY}`, | |
}, | |
}) | |
.then(function (response) { | |
console.log('response :', response); | |
setTranscribedData((oldData: any) => [ | |
...oldData, | |
response.data?.text, | |
]); | |
setIsTranscribing(false); | |
intervalRef.current = setInterval( | |
transcribeInterim, | |
transcribeTimeout * 1000, | |
); | |
// Delete the recorded file after successful transcription | |
RNFS.unlink(filePath) | |
.then(() => { | |
console.log('File deleted:', filePath); | |
}) | |
.catch(error => { | |
console.log('Error deleting file:', error); | |
}); | |
}) | |
.catch(function (error) { | |
console.log('error :', error); | |
}); | |
}) | |
.catch(error => { | |
console.log('Error reading file:', error); | |
}); | |
}; | |
const resetTranscribedData = () => { | |
setTranscribedData([]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment