Update index.html
Browse files- index.html +43 -241
index.html
CHANGED
|
@@ -59,8 +59,9 @@
|
|
| 59 |
<span id="statusText" class="text-sm font-medium text-gray-300 uppercase">Disconnected</span>
|
| 60 |
</div>
|
| 61 |
<div id="creditArea" class="hidden text-right">
|
| 62 |
-
<p class="text-xs text-gray-500 mb-1">
|
| 63 |
-
<div class="credit-badge" id="creditBadge">Loading...</div>
|
|
|
|
| 64 |
</div>
|
| 65 |
</div>
|
| 66 |
</div>
|
|
@@ -328,250 +329,51 @@
|
|
| 328 |
voiceSelect.appendChild(option);
|
| 329 |
});
|
| 330 |
|
| 331 |
-
// Fetch credits
|
| 332 |
try {
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
}
|
| 338 |
-
}
|
| 339 |
|
| 340 |
-
if (
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
|
|
|
| 344 |
creditArea.classList.remove('hidden');
|
| 345 |
}
|
| 346 |
} catch (e) {
|
| 347 |
-
console.log("
|
| 348 |
}
|
| 349 |
|
| 350 |
-
|
| 351 |
-
statusText.textContent = "CONNECTED";
|
| 352 |
-
updateLog("Connected! Voices loaded!", "success");
|
| 353 |
-
showSavedIndicator();
|
| 354 |
-
|
| 355 |
-
} catch (err) {
|
| 356 |
-
updateLog(err.message, "error");
|
| 357 |
-
statusDot.className = "h-3 w-3 rounded-full bg-red-500 pulse-red";
|
| 358 |
-
statusText.textContent = "ERROR";
|
| 359 |
-
} finally {
|
| 360 |
-
connectBtn.disabled = false;
|
| 361 |
-
connectBtn.textContent = "Connect Account";
|
| 362 |
-
}
|
| 363 |
-
}
|
| 364 |
-
|
| 365 |
-
connectBtn.addEventListener('click', fetchVoices);
|
| 366 |
-
|
| 367 |
-
// File upload
|
| 368 |
-
uploadArea.addEventListener('click', () => audioFile.click());
|
| 369 |
-
uploadArea.addEventListener('dragover', (e) => {
|
| 370 |
-
e.preventDefault();
|
| 371 |
-
uploadArea.classList.add('dragover');
|
| 372 |
-
});
|
| 373 |
-
uploadArea.addEventListener('dragleave', () => {
|
| 374 |
-
uploadArea.classList.remove('dragover');
|
| 375 |
-
});
|
| 376 |
-
uploadArea.addEventListener('drop', (e) => {
|
| 377 |
-
e.preventDefault();
|
| 378 |
-
uploadArea.classList.remove('dragover');
|
| 379 |
-
audioFile.files = e.dataTransfer.files;
|
| 380 |
-
if (audioFile.files.length) {
|
| 381 |
-
uploadedFileName.classList.remove('hidden');
|
| 382 |
-
uploadedFileName.textContent = `✓ ${audioFile.files[0].name}`;
|
| 383 |
-
}
|
| 384 |
-
});
|
| 385 |
-
|
| 386 |
-
audioFile.addEventListener('change', () => {
|
| 387 |
-
if (audioFile.files.length) {
|
| 388 |
-
uploadedFileName.classList.remove('hidden');
|
| 389 |
-
uploadedFileName.textContent = `✓ ${audioFile.files[0].name}`;
|
| 390 |
-
}
|
| 391 |
-
});
|
| 392 |
-
|
| 393 |
-
// Clone voice
|
| 394 |
-
cloneBtn.addEventListener('click', async () => {
|
| 395 |
-
const key = apiKeyInput.value.trim();
|
| 396 |
-
const name = voiceName.value.trim();
|
| 397 |
-
const file = audioFile.files[0];
|
| 398 |
-
|
| 399 |
-
if (!key) return updateLog("API Key missing!", "error");
|
| 400 |
-
if (!file) return updateLog("Audio file select karo", "error");
|
| 401 |
-
if (!name) return updateLog("Voice ko name do", "error");
|
| 402 |
-
|
| 403 |
-
cloneBtn.disabled = true;
|
| 404 |
-
cloneBtn.textContent = "⏳ Creating...";
|
| 405 |
-
|
| 406 |
-
try {
|
| 407 |
-
const reader = new FileReader();
|
| 408 |
-
reader.onload = async (e) => {
|
| 409 |
-
const base64Audio = btoa(String.fromCharCode.apply(null, new Uint8Array(e.target.result)));
|
| 410 |
-
|
| 411 |
-
const body = {
|
| 412 |
-
name: name,
|
| 413 |
-
samples: [{
|
| 414 |
-
text: "This is a voice sample",
|
| 415 |
-
audio: base64Audio
|
| 416 |
-
}]
|
| 417 |
-
};
|
| 418 |
-
|
| 419 |
-
if (removeNoise.checked) {
|
| 420 |
-
body.enhance_audio = true;
|
| 421 |
-
}
|
| 422 |
-
|
| 423 |
-
const response = await fetch(`${CARTESIA_API_URL}/voices/clone`, {
|
| 424 |
-
method: 'POST',
|
| 425 |
-
headers: {
|
| 426 |
-
"X-API-Key": key,
|
| 427 |
-
"Cartesia-Version": "2024-06-10",
|
| 428 |
-
"Content-Type": "application/json"
|
| 429 |
-
},
|
| 430 |
-
body: JSON.stringify(body)
|
| 431 |
-
});
|
| 432 |
-
|
| 433 |
-
if (!response.ok) {
|
| 434 |
-
const error = await response.json();
|
| 435 |
-
throw new Error(error.message || "Clone failed");
|
| 436 |
-
}
|
| 437 |
-
|
| 438 |
-
const voiceData = await response.json();
|
| 439 |
-
currentClonedVoiceId = voiceData.id;
|
| 440 |
-
clonedVoiceId.textContent = voiceData.id;
|
| 441 |
-
voiceDetailsArea.classList.remove('hidden');
|
| 442 |
-
customVoiceId.value = voiceData.id;
|
| 443 |
-
updateLog("✓ Voice cloned! Test kar sakte ho!", "success");
|
| 444 |
-
};
|
| 445 |
-
reader.readAsArrayBuffer(file);
|
| 446 |
-
|
| 447 |
-
} catch (err) {
|
| 448 |
-
updateLog(err.message, "error");
|
| 449 |
-
} finally {
|
| 450 |
-
cloneBtn.disabled = false;
|
| 451 |
-
cloneBtn.textContent = "🎤 Create Voice";
|
| 452 |
-
}
|
| 453 |
-
});
|
| 454 |
-
|
| 455 |
-
// Copy voice ID
|
| 456 |
-
copyVoiceIdBtn.addEventListener('click', () => {
|
| 457 |
-
navigator.clipboard.writeText(currentClonedVoiceId);
|
| 458 |
-
copyVoiceIdBtn.textContent = "✓ Copied!";
|
| 459 |
-
setTimeout(() => copyVoiceIdBtn.textContent = "📋 Copy ID", 2000);
|
| 460 |
-
});
|
| 461 |
-
|
| 462 |
-
// Test voice
|
| 463 |
-
testVoiceBtn.addEventListener('click', async () => {
|
| 464 |
-
const key = apiKeyInput.value.trim();
|
| 465 |
-
const voiceId = customVoiceId.value.trim() || voiceSelect.value;
|
| 466 |
-
const text = testText.value.trim();
|
| 467 |
-
|
| 468 |
-
if (!key) return updateLog("API Key missing!", "error");
|
| 469 |
-
if (!voiceId) return updateLog("Voice select karo", "error");
|
| 470 |
-
if (!text) return updateLog("Text likho", "error");
|
| 471 |
-
|
| 472 |
-
testVoiceBtn.disabled = true;
|
| 473 |
-
testVoiceBtn.textContent = "⏳ Processing...";
|
| 474 |
-
|
| 475 |
-
try {
|
| 476 |
-
const response = await fetch(`${CARTESIA_API_URL}/tts/bytes`, {
|
| 477 |
-
method: 'POST',
|
| 478 |
-
headers: {
|
| 479 |
-
"X-API-Key": key,
|
| 480 |
-
"Cartesia-Version": "2024-06-10",
|
| 481 |
-
"Content-Type": "application/json"
|
| 482 |
-
},
|
| 483 |
-
body: JSON.stringify({
|
| 484 |
-
model_id: "sonic-english",
|
| 485 |
-
transcript: text,
|
| 486 |
-
voice: { mode: "id", id: voiceId },
|
| 487 |
-
output_format: { container: "wav", encoding: "pcm_f32le", sample_rate: 44100 }
|
| 488 |
-
})
|
| 489 |
-
});
|
| 490 |
-
|
| 491 |
-
if (!response.ok) throw new Error("TTS failed");
|
| 492 |
-
|
| 493 |
-
const blob = await response.blob();
|
| 494 |
-
testAudioPlayer.src = URL.createObjectURL(blob);
|
| 495 |
-
testAudioArea.classList.remove('hidden');
|
| 496 |
-
updateLog("Test audio ready!", "success");
|
| 497 |
-
testAudioPlayer.play();
|
| 498 |
-
|
| 499 |
-
} catch (err) {
|
| 500 |
-
updateLog(err.message, "error");
|
| 501 |
-
} finally {
|
| 502 |
-
testVoiceBtn.disabled = false;
|
| 503 |
-
testVoiceBtn.textContent = "▶️ Test Voice";
|
| 504 |
-
}
|
| 505 |
-
});
|
| 506 |
-
|
| 507 |
-
// TTS Generation
|
| 508 |
-
generateBtn.addEventListener('click', async () => {
|
| 509 |
-
const key = apiKeyInput.value.trim();
|
| 510 |
-
const voiceId = customVoiceId.value.trim() || voiceSelect.value;
|
| 511 |
-
const text = textInput.value.trim();
|
| 512 |
-
|
| 513 |
-
if (!key) return updateLog("API Key missing!", "error");
|
| 514 |
-
if (!voiceId) return updateLog("Voice select karo", "error");
|
| 515 |
-
if (!text) return updateLog("Text likho", "error");
|
| 516 |
-
|
| 517 |
-
generateBtn.disabled = true;
|
| 518 |
-
generateBtn.textContent = "⌛ Processing...";
|
| 519 |
-
audioArea.classList.add('hidden');
|
| 520 |
-
|
| 521 |
-
try {
|
| 522 |
-
const response = await fetch(`${CARTESIA_API_URL}/tts/bytes`, {
|
| 523 |
-
method: 'POST',
|
| 524 |
-
headers: {
|
| 525 |
-
"X-API-Key": key,
|
| 526 |
-
"Cartesia-Version": "2024-06-10",
|
| 527 |
-
"Content-Type": "application/json"
|
| 528 |
-
},
|
| 529 |
-
body: JSON.stringify({
|
| 530 |
-
model_id: "sonic-english",
|
| 531 |
-
transcript: text,
|
| 532 |
-
voice: { mode: "id", id: voiceId },
|
| 533 |
-
output_format: { container: "wav", encoding: "pcm_f32le", sample_rate: 44100 }
|
| 534 |
-
})
|
| 535 |
-
});
|
| 536 |
-
|
| 537 |
-
if (!response.ok) throw new Error("TTS failed");
|
| 538 |
-
|
| 539 |
-
const blob = await response.blob();
|
| 540 |
-
audioPlayer.src = URL.createObjectURL(blob);
|
| 541 |
-
audioArea.classList.remove('hidden');
|
| 542 |
-
updateLog("Audio ready!", "success");
|
| 543 |
-
audioPlayer.play();
|
| 544 |
-
|
| 545 |
-
} catch (err) {
|
| 546 |
-
updateLog(err.message, "error");
|
| 547 |
-
} finally {
|
| 548 |
-
generateBtn.disabled = false;
|
| 549 |
-
generateBtn.textContent = "⚡ Convert to Speech";
|
| 550 |
-
}
|
| 551 |
-
});
|
| 552 |
-
|
| 553 |
-
// Download
|
| 554 |
-
downloadBtn.addEventListener('click', () => {
|
| 555 |
-
const src = audioPlayer.src;
|
| 556 |
-
if (!src) return;
|
| 557 |
-
const a = document.createElement('a');
|
| 558 |
-
a.href = src;
|
| 559 |
-
a.download = `audio-${Date.now()}.wav`;
|
| 560 |
-
a.click();
|
| 561 |
-
updateLog("Download started!", "success");
|
| 562 |
-
});
|
| 563 |
-
|
| 564 |
-
// Share
|
| 565 |
-
shareBtn.addEventListener('click', () => {
|
| 566 |
-
const src = audioPlayer.src;
|
| 567 |
-
if (!src) return;
|
| 568 |
-
navigator.clipboard.writeText(src);
|
| 569 |
-
updateLog("Link copied!", "success");
|
| 570 |
-
shareBtn.textContent = "✓ Copied!";
|
| 571 |
-
setTimeout(() => shareBtn.textContent = "📋 Copy", 2000);
|
| 572 |
-
});
|
| 573 |
-
|
| 574 |
-
window.addEventListener('load', initializeApp);
|
| 575 |
-
</script>
|
| 576 |
-
</body>
|
| 577 |
-
</html>
|
|
|
|
| 59 |
<span id="statusText" class="text-sm font-medium text-gray-300 uppercase">Disconnected</span>
|
| 60 |
</div>
|
| 61 |
<div id="creditArea" class="hidden text-right">
|
| 62 |
+
<p class="text-xs text-gray-500 mb-1">Available:</p>
|
| 63 |
+
<div class="credit-badge" id="creditBadge">💰 Loading...</div>
|
| 64 |
+
<button id="refreshCreditsBtn" class="text-xs text-gray-500 hover:text-gray-300 mt-1">🔄 Refresh</button>
|
| 65 |
</div>
|
| 66 |
</div>
|
| 67 |
</div>
|
|
|
|
| 329 |
voiceSelect.appendChild(option);
|
| 330 |
});
|
| 331 |
|
| 332 |
+
// Fetch credits - Try multiple endpoints
|
| 333 |
try {
|
| 334 |
+
let credits = null;
|
| 335 |
+
|
| 336 |
+
// Try endpoint 1
|
| 337 |
+
try {
|
| 338 |
+
const creditsResponse = await fetch(`${CARTESIA_API_URL}/account/billing/credits`, {
|
| 339 |
+
headers: {
|
| 340 |
+
"X-API-Key": key,
|
| 341 |
+
"Cartesia-Version": "2024-06-10"
|
| 342 |
+
}
|
| 343 |
+
});
|
| 344 |
+
|
| 345 |
+
if (creditsResponse.ok) {
|
| 346 |
+
const creditsData = await creditsResponse.json();
|
| 347 |
+
credits = creditsData.credits || creditsData.remaining_credits || creditsData.balance || 0;
|
| 348 |
+
}
|
| 349 |
+
} catch (e1) {
|
| 350 |
+
// Try endpoint 2
|
| 351 |
+
try {
|
| 352 |
+
const accountResponse = await fetch(`${CARTESIA_API_URL}/account`, {
|
| 353 |
+
headers: {
|
| 354 |
+
"X-API-Key": key,
|
| 355 |
+
"Cartesia-Version": "2024-06-10"
|
| 356 |
+
}
|
| 357 |
+
});
|
| 358 |
+
|
| 359 |
+
if (accountResponse.ok) {
|
| 360 |
+
const accountData = await accountResponse.json();
|
| 361 |
+
credits = accountData.credits || accountData.remaining_credits || accountData.balance || 0;
|
| 362 |
+
}
|
| 363 |
+
} catch (e2) {
|
| 364 |
+
console.log("Credits endpoints failed");
|
| 365 |
}
|
| 366 |
+
}
|
| 367 |
|
| 368 |
+
if (credits !== null && credits > 0) {
|
| 369 |
+
creditBadge.textContent = `💰 ${(credits).toFixed(0)} Credits`;
|
| 370 |
+
creditArea.classList.remove('hidden');
|
| 371 |
+
} else if (credits !== null) {
|
| 372 |
+
creditBadge.textContent = `💰 ${(credits).toFixed(0)} Credits`;
|
| 373 |
creditArea.classList.remove('hidden');
|
| 374 |
}
|
| 375 |
} catch (e) {
|
| 376 |
+
console.log("Credit fetch error:", e);
|
| 377 |
}
|
| 378 |
|
| 379 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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