Spaces:
Runtime error
Runtime error
File size: 10,296 Bytes
8a6f58d c50afc1 8a6f58d 5647449 8a6f58d | 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 342 343 344 | /**
* Frontend JavaScript for AI Voice Detection
* Handles file upload, API communication, and result display
*/
// Configuration
const API_BASE_URL = '';
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
// Global state
let selectedFile = null;
// Initialize
function init() {
console.log('Script initialized, Build v5.2');
initializeUploadPage();
if (window.location.pathname.includes('result.html')) {
const storedResults = sessionStorage.getItem('detectionResults');
if (storedResults) {
const data = JSON.parse(storedResults);
showResult(data, sessionStorage.getItem('lastFilename') || 'Audio File');
}
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
/**
* Initialize upload page functionality
*/
function initializeUploadPage() {
console.log('Initializing upload page...');
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
if (!dropZone || !fileInput) {
console.error('Upload elements not found!');
return;
}
// Drag and drop events
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.add('bg-blue-50', 'border-blue-400');
});
dropZone.addEventListener('dragleave', (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.remove('bg-blue-50', 'border-blue-400');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.remove('bg-blue-50', 'border-blue-400');
const files = e.dataTransfer.files;
if (files.length > 0) {
processFile(files[0]);
}
});
// File input change
fileInput.addEventListener('change', (e) => {
const files = e.target.files;
if (files.length > 0) {
processFile(files[0]);
}
});
}
/**
* Process and validate selected file
*/
function processFile(file) {
console.log('Processing file:', file.name, file.type, file.size);
// Clear previous errors
hideError();
// Validate file type
const validTypes = ['audio/mpeg', 'audio/mp3', 'audio/x-mpeg', 'audio/x-mp3'];
const isMp3 = validTypes.includes(file.type) || file.name.toLowerCase().endsWith('.mp3');
if (!isMp3) {
showError('Invalid file type. Please upload an MP3 file.');
return;
}
// Validate file size
if (file.size > MAX_FILE_SIZE) {
showError(`File too large. Maximum size is ${MAX_FILE_SIZE / 1024 / 1024}MB.`);
return;
}
// Store file
selectedFile = file;
// Display file info
displayFileInfo(file);
// Enable analyze button
enableAnalyzeButton();
}
/**
* Display file information
*/
function displayFileInfo(file) {
const fileInfo = document.getElementById('fileInfo');
const fileName = document.getElementById('fileName');
const fileSize = document.getElementById('fileSize');
const previewContainer = document.getElementById('audioPreviewContainer') || createAudioPreviewContainer();
fileName.textContent = file.name;
fileSize.textContent = formatFileSize(file.size);
// Add audio preview
const audioUrl = URL.createObjectURL(file);
let audioPlayer = previewContainer.querySelector('audio');
if (!audioPlayer) {
audioPlayer = document.createElement('audio');
audioPlayer.controls = true;
audioPlayer.className = 'w-full mt-4 h-10';
previewContainer.appendChild(audioPlayer);
}
audioPlayer.src = audioUrl;
fileInfo.classList.remove('hidden');
}
function createAudioPreviewContainer() {
const fileInfo = document.getElementById('fileInfo');
const container = document.createElement('div');
container.id = 'audioPreviewContainer';
container.className = 'mt-4 pt-4 border-t border-blue-100';
fileInfo.appendChild(container);
return container;
}
/**
* Format file size for display
*/
function formatFileSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB';
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
}
/**
* Clear selected file
*/
function clearFile() {
selectedFile = null;
const fileInput = document.getElementById('fileInput');
if (fileInput) fileInput.value = '';
const fileInfo = document.getElementById('fileInfo');
if (fileInfo) fileInfo.classList.add('hidden');
disableAnalyzeButton();
hideError();
}
/**
* Enable analyze button
*/
function enableAnalyzeButton() {
const btn = document.getElementById('analyzeBtn');
if (!btn) return;
btn.disabled = false;
btn.className = 'w-full mt-6 bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 rounded-lg transition cursor-pointer shadow-lg hover:shadow-xl';
}
/**
* Disable analyze button
*/
function disableAnalyzeButton() {
const btn = document.getElementById('analyzeBtn');
if (!btn) return;
btn.disabled = true;
btn.className = 'w-full mt-6 bg-gray-300 text-gray-500 font-semibold py-3 rounded-lg cursor-not-allowed transition';
}
/**
* Analyze audio file
* Called from upload.html onclick
*/
async function analyzeAudio() {
if (!selectedFile) return;
const analyzeBtn = document.getElementById('analyzeBtn');
const loadingState = document.getElementById('loadingState');
const errorMessage = document.getElementById('errorMessage');
// Show loading
analyzeBtn.classList.add('hidden');
loadingState.classList.remove('hidden');
if (errorMessage) errorMessage.classList.add('hidden');
try {
const base64Audio = await fileToBase64(selectedFile);
const headers = {
'Content-Type': 'application/json'
};
const token = localStorage.getItem('access_token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
} else {
// Fallback to generic API key if no token
headers['api-key'] = 'your-secret-api-key-here';
}
const response = await fetch(`${API_BASE_URL}detect-voice`, {
method: 'POST',
headers: headers,
body: JSON.stringify({
audio_base64: base64Audio
})
});
const data = await response.json();
if (!response.ok) {
if (response.status === 401) {
throw new Error('Your session has expired. Please logout and login again.');
}
throw new Error(data.detail || data.message || 'Analysis failed. Please try again.');
}
// Store results and redirect
sessionStorage.setItem('detectionResults', JSON.stringify(data));
sessionStorage.setItem('lastFilename', selectedFile.name);
window.location.href = 'result.html';
} catch (error) {
console.error('Error during analysis:', error);
showError(error.message);
// Reset button
analyzeBtn.classList.remove('hidden');
loadingState.classList.add('hidden');
}
}
/**
* Show error message
*/
function showError(message) {
const errorEl = document.getElementById('errorMessage');
const errorText = document.getElementById('errorText');
if (errorEl && errorText) {
errorText.textContent = message;
errorEl.classList.remove('hidden');
errorEl.scrollIntoView({ behavior: 'smooth', block: 'center' });
} else {
alert(message);
}
}
/**
* Hide error message
*/
function hideError() {
const errorEl = document.getElementById('errorMessage');
if (errorEl) errorEl.classList.add('hidden');
}
/**
* Convert file to Base64
*/
function fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
const base64String = reader.result.split(',')[1];
resolve(base64String);
};
reader.onerror = error => reject(error);
});
}
/**
* Show results on page (used in result.html)
*/
function showResult(data, filename) {
const predictionEl = document.getElementById('predictionResult');
const confidenceEl = document.getElementById('confidenceScore');
const confidenceBarFill = document.getElementById('confidenceBarFill');
const jsonOutput = document.getElementById('jsonOutput');
if (predictionEl) {
if (data.prediction === 'AI_GENERATED') {
predictionEl.textContent = 'AI Generated Voice Detected';
predictionEl.className = 'text-2xl font-bold mb-2 text-red-600';
if (confidenceBarFill) confidenceBarFill.className = 'confidence-fill bg-red-500';
} else {
predictionEl.textContent = 'Human Voice Verified';
predictionEl.className = 'text-2xl font-bold mb-2 text-green-600';
if (confidenceBarFill) confidenceBarFill.className = 'confidence-fill bg-green-500';
}
}
if (confidenceEl) {
const confidence = (data.confidence * 100).toFixed(1) + '%';
confidenceEl.textContent = confidence;
if (confidenceBarFill) confidenceBarFill.style.width = confidence;
}
// Update explanations
if (data.explanation) {
const pitchEl = document.getElementById('pitchExplanation');
const spectralEl = document.getElementById('spectralExplanation');
const energyEl = document.getElementById('energyExplanation');
if (pitchEl) pitchEl.textContent = data.explanation.pitch_variance || 'N/A';
if (spectralEl) spectralEl.textContent = data.explanation.spectral_smoothness || 'N/A';
if (energyEl) energyEl.textContent = data.explanation.micro_variations || 'N/A';
}
if (jsonOutput) {
jsonOutput.textContent = JSON.stringify(data, null, 2);
}
}
/**
* Logout functionality
*/
function logout() {
localStorage.removeItem('access_token');
localStorage.removeItem('user_email');
localStorage.removeItem('api_key');
window.location.href = 'index.html';
}
|