Spaces:
Running
Running
File size: 3,273 Bytes
36079e7 | 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 | /**
* 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();
}
};
|