Spaces:
Sleeping
Sleeping
File size: 15,089 Bytes
a2609de b619e9e a2609de b619e9e a2609de b619e9e a2609de b619e9e a2609de b619e9e a2609de b619e9e a2609de b619e9e a2609de b619e9e a2609de b619e9e 5b28f25 b619e9e 5b28f25 b619e9e a2609de b619e9e 5b28f25 b619e9e 5b28f25 b619e9e 5b28f25 b619e9e 5b28f25 b619e9e 5b28f25 b619e9e 5b28f25 b619e9e 5b28f25 b619e9e 5b28f25 b619e9e a2609de b619e9e a2609de b619e9e a2609de | 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | document.addEventListener('DOMContentLoaded', () => {
const HF_API = "https://bilalrhch-arabic-tts-api.hf.space";
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Voice definitions
// Add more voices here by uploading <id>.wav + <id>.txt to the HF /voices folder
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const availableVoices = [
{ id: "Auto", name: "ุชููุงุฆู", nameEn: "Auto", gender: "ุฐูุงุก ุงุตุทูุงุนู", color: "#4A90E2" },
{ id: "tariq", name: "ุทุงุฑู", nameEn: "Tariq", gender: "ุฑุฌู", color: "#10B981" },
{ id: "omar", name: "ุนู
ุฑ", nameEn: "Omar", gender: "ุฑุฌู", color: "#F59E0B" },
{ id: "layla", name: "ูููู", nameEn: "Layla", gender: "ุงู
ุฑุฃุฉ", color: "#E11D48" },
{ id: "nour", name: "ููุฑ", nameEn: "Nour", gender: "ุงู
ุฑุฃุฉ", color: "#8B5CF6" },
];
let selectedVoiceId = "Auto";
let previewAudio = null;
let playingVoiceId = null;
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Theme Toggle
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const themeToggle = document.getElementById('theme-toggle');
const moonIcon = document.getElementById('moon-icon');
const sunIcon = document.getElementById('sun-icon');
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark' || (!savedTheme && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.setAttribute('data-theme', 'dark');
moonIcon.classList.add('hidden');
sunIcon.classList.remove('hidden');
}
themeToggle.addEventListener('click', () => {
const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
if (isDark) {
document.documentElement.removeAttribute('data-theme');
localStorage.setItem('theme', 'light');
moonIcon.classList.remove('hidden');
sunIcon.classList.add('hidden');
} else {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
moonIcon.classList.add('hidden');
sunIcon.classList.remove('hidden');
}
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Voice Selector Modal
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const voiceBtn = document.getElementById('custom-voice-btn');
const voiceModal = document.getElementById('voice-selector-modal');
const closeModalBtn = document.getElementById('close-modal-btn');
const voiceListEl = document.getElementById('voice-list-container');
function getInitial(voice) {
return voice.name.charAt(0);
}
function renderVoiceList() {
voiceListEl.innerHTML = '';
availableVoices.forEach(voice => {
const isActive = voice.id === selectedVoiceId;
const item = document.createElement('div');
item.className = 'voice-item' + (isActive ? ' active' : '');
item.dataset.voiceId = voice.id;
item.innerHTML = `
<div class="voice-item-left">
<div class="avatar" style="background-color: ${voice.color}">${getInitial(voice)}</div>
<div class="voice-info">
<span class="voice-name">${voice.name}</span>
<span class="voice-gender">${voice.gender}</span>
</div>
</div>
<div class="voice-item-right">
${voice.id !== 'Auto' ? `
<button type="button" class="preview-btn" data-voice-id="${voice.id}" title="ู
ุนุงููุฉ ุงูุตูุช">
<div class="preview-audio-anim hidden" id="eq-${voice.id}">
<span></span><span></span><span></span>
</div>
<svg class="play-icon" id="play-icon-${voice.id}" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
<path d="M8 5v14l11-7z"/>
</svg>
</button>` : ''}
<svg class="checkmark" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></svg>
</div>
`;
// Select voice on row click (not the preview button)
item.addEventListener('click', (e) => {
if (e.target.closest('.preview-btn')) return;
selectVoice(voice);
closeModal();
});
voiceListEl.appendChild(item);
});
// Attach preview button handlers
voiceListEl.querySelectorAll('.preview-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
const vId = btn.dataset.voiceId;
togglePreview(vId);
});
});
}
function selectVoice(voice) {
selectedVoiceId = voice.id;
// Update trigger button display
document.querySelector('#selected-voice-display .avatar').style.backgroundColor = voice.color;
document.querySelector('#selected-voice-display .avatar').textContent = getInitial(voice);
document.querySelector('#selected-voice-display .voice-name').textContent = voice.name;
document.querySelector('#selected-voice-display .voice-gender').textContent = voice.gender;
// Re-render to update active checkmark
renderVoiceList();
}
function openModal() {
renderVoiceList();
voiceModal.classList.remove('hidden');
voiceBtn.classList.add('open');
}
function closeModal() {
voiceModal.classList.add('hidden');
voiceBtn.classList.remove('open');
}
voiceBtn.addEventListener('click', () => {
if (voiceModal.classList.contains('hidden')) {
openModal();
} else {
closeModal();
}
});
closeModalBtn.addEventListener('click', closeModal);
// Close modal when clicking outside
document.addEventListener('click', (e) => {
if (!voiceModal.classList.contains('hidden') &&
!voiceBtn.contains(e.target) &&
!voiceModal.contains(e.target)) {
closeModal();
}
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Audio Preview
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
function togglePreview(voiceId) {
if (playingVoiceId === voiceId) {
// Stop the current preview
stopPreview();
return;
}
// Stop previous if any
stopPreview();
const url = `${HF_API}/voices/${voiceId}.wav`;
previewAudio = new Audio(url);
playingVoiceId = voiceId;
// Show EQ animation, hide play icon
const eq = document.getElementById(`eq-${voiceId}`);
const playIcon = document.getElementById(`play-icon-${voiceId}`);
if (eq) eq.classList.remove('hidden');
if (playIcon) playIcon.classList.add('hidden');
previewAudio.play().catch(() => {
// Voice file not uploaded yet โ silently fail
stopPreview();
});
previewAudio.addEventListener('ended', stopPreview);
}
function stopPreview() {
if (previewAudio) {
previewAudio.pause();
previewAudio.currentTime = 0;
previewAudio = null;
}
if (playingVoiceId) {
const eq = document.getElementById(`eq-${playingVoiceId}`);
const playIcon = document.getElementById(`play-icon-${playingVoiceId}`);
if (eq) eq.classList.add('hidden');
if (playIcon) playIcon.classList.remove('hidden');
playingVoiceId = null;
}
}
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// TTS Logic
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const synthesizeBtn = document.getElementById('synthesize-btn');
const btnText = synthesizeBtn.querySelector('.btn-text');
const spinner = synthesizeBtn.querySelector('.spinner');
const textInput = document.getElementById('tts-text');
const speedInput = document.getElementById('tts-speed');
const resultContainer = document.getElementById('result-container');
const audioPlayer = document.getElementById('audio-player');
const downloadBtn = document.getElementById('download-btn');
const errorMessage = document.getElementById('error-message');
let currentAudioUrl = null;
synthesizeBtn.addEventListener('click', async () => {
const text = textInput.value.trim();
if (!text) {
showError("ุงูุฑุฌุงุก ุฅุฏุฎุงู ุงููุต ุฃููุงู");
return;
}
hideError();
resultContainer.classList.add('hidden');
setLoading(true);
stopPreview();
closeModal();
try {
const response = await fetch(`${HF_API}/synthesize`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: text,
voice: selectedVoiceId,
speed: parseFloat(speedInput.value) || 1.0
})
});
if (!response.ok) {
const errorData = await response.json().catch(() => null);
throw new Error(errorData?.detail || `HTTP Error: ${response.status}`);
}
const data = await response.json();
const taskId = data.task_id;
document.getElementById('progress-container').classList.remove('hidden');
pollTaskStatus(taskId);
} catch (error) {
console.error("Synthesis error:", error);
showError(`ุญุฏุซ ุฎุทุฃ ุฃุซูุงุก ุงูุชูููุฏ: ${error.message}`);
setLoading(false);
}
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Status Polling
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function pollTaskStatus(taskId) {
try {
const statusRes = await fetch(`${HF_API}/status/${taskId}`);
if (!statusRes.ok) {
if (statusRes.status === 404 || statusRes.status === 503) {
throw new Error(`ุชุนุฐุฑ ุฌูุจ ุงูุญุงูุฉ (HTTP ${statusRes.status}). ุงูุฎุงุฏู
ููุฏ ุฅุนุงุฏุฉ ุงูุชุดุบููุ ูุฑุฌู ุงูู
ุญุงููุฉ ุจุนุฏ ุฏูููุฉ.`);
}
throw new Error(`Could not fetch status: HTTP ${statusRes.status}`);
}
const state = await statusRes.json();
document.getElementById('progress-fill').style.width = `${state.progress}%`;
let statusText = `ุฌุงุฑู ุงูู
ุนุงูุฌุฉ... ${state.progress}%`;
if (state.status === "stitching") statusText = "ุฌุงุฑู ุชุฌู
ูุน ุงูู
ููุงุช ุงูุตูุชูุฉ...";
document.getElementById('progress-text').textContent = statusText;
if (state.status === "completed") {
document.getElementById('progress-container').classList.add('hidden');
if (currentAudioUrl && currentAudioUrl.startsWith('blob:')) {
URL.revokeObjectURL(currentAudioUrl);
}
currentAudioUrl = `${HF_API}/${state.download_url}`;
audioPlayer.src = currentAudioUrl;
resultContainer.classList.remove('hidden');
setLoading(false);
audioPlayer.play().catch(e => console.log('Autoplay blocked', e));
} else if (state.status === "failed") {
throw new Error(state.error || "Unknown background generation error");
} else {
setTimeout(() => pollTaskStatus(taskId), 2000);
}
} catch (error) {
console.error("Polling error:", error);
document.getElementById('progress-container').classList.add('hidden');
showError(`ุงููุทุน ุงูุงุชุตุงู ุฃู ุญุฏุซ ุฎุทุฃ: ${error.message}`);
setLoading(false);
}
}
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Download
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
downloadBtn.addEventListener('click', () => {
if (!currentAudioUrl) return;
const a = document.createElement('a');
a.href = currentAudioUrl;
a.download = `OmniVoice_Arabic_${new Date().getTime()}.wav`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Utils
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
function setLoading(isLoading) {
synthesizeBtn.disabled = isLoading;
if (isLoading) {
btnText.classList.add('hidden');
spinner.classList.remove('hidden');
} else {
btnText.classList.remove('hidden');
spinner.classList.add('hidden');
}
}
function showError(msg) {
errorMessage.textContent = msg;
errorMessage.classList.remove('hidden');
}
function hideError() {
errorMessage.classList.add('hidden');
}
});
|