Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
38 lines (35 loc) · 1.38 KB
/
Copy pathscript.js
File metadata and controls
38 lines (35 loc) · 1.38 KB
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
document.getElementById('convert-btn').addEventListener('click', function() {
const apiKey = document.getElementById('api-key-input').value;
const text = document.getElementById('text-input').value;
const model = document.getElementById('model-selector').value;
const voice = document.getElementById('voice-selector').value;
const format = document.getElementById('format-selector').value;
if (!apiKey) {
alert('Please enter your OpenAI API key.');
return;
}
fetch('https://api.openai.com/v1/audio/speech', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: model,
voice: voice,
input: text,
response_format: format
})
})
.then(response => response.blob())
.then(blob => {
const url = URL.createObjectURL(blob);
document.getElementById('audio-player').src = url;
document.getElementById('audio-player').style.display = 'block';
const downloadBtn = document.getElementById('download-btn');
downloadBtn.href = url;
downloadBtn.download = `speech.${format}`;
downloadBtn.style.display = 'block';
})
.catch(error => console.error('Error:', error));
});