Spaces:
Sleeping
Sleeping
Create script.js
Browse files
script.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Main function to generate audio
|
| 2 |
+
async function generateAudio() {
|
| 3 |
+
const taskId = document.getElementById('taskId').value.trim();
|
| 4 |
+
const audioId = document.getElementById('audioId').value.trim();
|
| 5 |
+
const generateBtn = document.getElementById('generateBtn');
|
| 6 |
+
const loading = document.getElementById('loading');
|
| 7 |
+
const status = document.getElementById('status');
|
| 8 |
+
const result = document.getElementById('result');
|
| 9 |
+
const response = document.getElementById('response');
|
| 10 |
+
|
| 11 |
+
// Validate inputs
|
| 12 |
+
if (!taskId || !audioId) {
|
| 13 |
+
showStatus('Please fill in both Task ID and Audio ID', 'error');
|
| 14 |
+
return;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
// Validate API key
|
| 18 |
+
if (!window.config.apiKey || window.config.apiKey === '') {
|
| 19 |
+
showStatus('API Key is not configured. Please set SUNO_API_KEY in Space secrets.', 'error');
|
| 20 |
+
return;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// Validate callback URL
|
| 24 |
+
if (!window.config.callbackUrl || window.config.callbackUrl === '') {
|
| 25 |
+
showStatus('Callback URL is not configured. Please set CALLBACK_URL in Space secrets.', 'error');
|
| 26 |
+
return;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
// Prepare request
|
| 30 |
+
const requestData = {
|
| 31 |
+
taskId: taskId,
|
| 32 |
+
audioId: audioId,
|
| 33 |
+
callBackUrl: window.config.callbackUrl
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
const options = {
|
| 37 |
+
method: 'POST',
|
| 38 |
+
headers: {
|
| 39 |
+
'Authorization': `Bearer ${window.config.apiKey}`,
|
| 40 |
+
'Content-Type': 'application/json'
|
| 41 |
+
},
|
| 42 |
+
body: JSON.stringify(requestData)
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
// Show loading state
|
| 46 |
+
generateBtn.disabled = true;
|
| 47 |
+
loading.classList.add('show');
|
| 48 |
+
status.classList.remove('show', 'success', 'error');
|
| 49 |
+
result.classList.remove('show');
|
| 50 |
+
|
| 51 |
+
try {
|
| 52 |
+
// Make API call
|
| 53 |
+
const apiResponse = await fetch('https://api.sunoapi.org/api/v1/wav/generate', options);
|
| 54 |
+
const data = await apiResponse.json();
|
| 55 |
+
|
| 56 |
+
// Display response
|
| 57 |
+
response.textContent = JSON.stringify(data, null, 2);
|
| 58 |
+
result.classList.add('show');
|
| 59 |
+
|
| 60 |
+
// Show success status
|
| 61 |
+
if (apiResponse.ok) {
|
| 62 |
+
showStatus('✅ Audio generation request sent successfully!', 'success');
|
| 63 |
+
} else {
|
| 64 |
+
showStatus(`❌ Error: ${data.message || 'Unknown error'}`, 'error');
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
} catch (error) {
|
| 68 |
+
// Handle errors
|