Spaces:
Running
Running
| /** | |
| * API client for Model Inspector backend | |
| */ | |
| const API = { | |
| baseUrl: '', // Same origin | |
| /** | |
| * Inspect a model by HuggingFace model ID | |
| * @param {string} modelId - HuggingFace model ID | |
| * @returns {Promise<{tree: Object, metadata: Object}>} | |
| */ | |
| async inspectModel(modelId) { | |
| const response = await fetch(`${this.baseUrl}/api/inspect`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ model_id: modelId }), | |
| }); | |
| if (!response.ok) { | |
| const error = await response.json(); | |
| const detail = error.detail || 'Failed to inspect model'; | |
| // Check for common auth/access errors | |
| const isAuthError = detail.includes('401') || | |
| detail.includes('403') || | |
| detail.includes('gated') || | |
| detail.includes('access') || | |
| detail.includes('token') || | |
| detail.includes('authorization') || | |
| detail.includes('authenticate'); | |
| if (isAuthError) { | |
| const err = new Error(detail); | |
| err.isAuthError = true; | |
| throw err; | |
| } | |
| throw new Error(detail); | |
| } | |
| return response.json(); | |
| }, | |
| /** | |
| * Inspect a model by config object | |
| * @param {Object} config - Model config.json object | |
| * @returns {Promise<{tree: Object, metadata: Object}>} | |
| */ | |
| async inspectConfig(config) { | |
| const response = await fetch(`${this.baseUrl}/api/inspect`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ config }), | |
| }); | |
| if (!response.ok) { | |
| const error = await response.json(); | |
| throw new Error(error.detail || 'Failed to inspect config'); | |
| } | |
| return response.json(); | |
| }, | |
| /** | |
| * Upload a config.json file | |
| * @param {File} file - The config.json file | |
| * @returns {Promise<{tree: Object, metadata: Object}>} | |
| */ | |
| async uploadConfig(file) { | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| const response = await fetch(`${this.baseUrl}/api/upload`, { | |
| method: 'POST', | |
| body: formData, | |
| }); | |
| if (!response.ok) { | |
| const error = await response.json(); | |
| throw new Error(error.detail || 'Failed to upload config'); | |
| } | |
| return response.json(); | |
| }, | |
| /** | |
| * Format parameter count for display | |
| * @param {number} count - Parameter count | |
| * @returns {string} Formatted string | |
| */ | |
| formatParams(count) { | |
| if (count >= 1e12) { | |
| return `${(count / 1e12).toFixed(2)}T`; | |
| } else if (count >= 1e9) { | |
| return `${(count / 1e9).toFixed(2)}B`; | |
| } else if (count >= 1e6) { | |
| return `${(count / 1e6).toFixed(2)}M`; | |
| } else if (count >= 1e3) { | |
| return `${(count / 1e3).toFixed(2)}K`; | |
| } | |
| return count.toLocaleString(); | |
| } | |
| }; | |