Chancellor / huggingFaceService.ts
VoidBreach's picture
Upload 62 files
35b8b9c verified
Raw History Blame Contribute Delete
5.03 kB
/**
* HUGGING_FACE_DIASPORA_PROTOCOL
* Manages the interface with the global Hugging Face Collective.
*
* "Actual implementation" for the Chancellor's objective.
*/
import { uploadFile, createRepo } from '@huggingface/hub';
export interface HFModel {
id: string;
author?: string;
lastModified: string;
downloads: number;
likes: number;
pipeline_tag?: string;
library_name?: string;
siblings?: { rfilename: string }[];
tags: string[];
}
export const HuggingFaceService = {
/**
* Uploads sanitized scholastic assets as a dataset to Hugging Face Hub.
*/
async uploadSanitizedDataset(repoName: string, jsonData: string): Promise<string> {
const rawToken = import.meta.env.VITE_HUGGING_FACE_TOKEN;
if (!rawToken) {
throw new Error('VITE_HUGGING_FACE_TOKEN is missing in environment variables.');
}
const token = rawToken.trim();
if (!token.startsWith('hf_') || token.includes('...')) {
throw new Error(
'HF_TOKEN_INVALID: Your Hugging Face access token must start with "hf_" and be a valid token. Please provide a real token in the Secrets panel.',
);
}
try {
// Create repository if it doesn't already exist
await createRepo({
repo: { name: repoName, type: 'dataset' },
credentials: { accessToken: token },
}).catch((e) => {
// Ignore "already exists" errors
if (!e.message.includes('already exists')) {
console.warn('createRepo ignored error:', e);
}
});
// Upload the dataset file
const result = await uploadFile({
repo: { name: repoName, type: 'dataset' },
credentials: { accessToken: token },
file: {
path: 'scholastic-library.json',
content: new Blob([jsonData], { type: 'application/json' }),
},
commitTitle: 'Update sanitized scholastic library',
});
return (result as any)?.commitUrl || `https://huggingface.co/datasets/${repoName}`;
} catch (error) {
console.error('HF_DATASET_UPLOAD_ERROR:', error);
throw error;
}
},
/**
* Scans the Hugging Face Hub for models relevant to scholastic evolution.
*/
async searchModels(query: string = 'scholastic'): Promise<HFModel[]> {
try {
const response = await fetch(
`https://huggingface.co/api/models?search=${encodeURIComponent(query)}&limit=8&sort=downloads&direction=-1`,
);
if (!response.ok) throw new Error('HF_HUB_UNREACHABLE');
return await response.json();
} catch (error) {
console.error('HF_SEARCH_ERROR:', error);
throw error;
}
},
/**
* Scans for datasets that could serve as educational substrates.
*/
async searchDatasets(query: string = 'education'): Promise<unknown[]> {
try {
const response = await fetch(
`https://huggingface.co/api/datasets?search=${encodeURIComponent(query)}&limit=8&sort=downloads&direction=-1`,
);
if (!response.ok) throw new Error('HF_HUB_UNREACHABLE');
return await response.json();
} catch (error) {
console.error('HF_DATASET_SEARCH_ERROR:', error);
throw error;
}
},
/**
* Performs an "Integrity Audit" on a model's repository.
* Checks for safetensors (safe) vs pickles (dangerous) and verifies metadata.
*/
async auditModelIntegrity(modelId: string): Promise<{
isSafe: boolean;
hasSafetensors: boolean;
hasSignedWeights: boolean;
vulnerabilityScore: number;
auditLog: string[];
}> {
try {
const response = await fetch(`https://huggingface.co/api/models/${modelId}`);
if (!response.ok) throw new Error('HF_MODEL_DETAILS_UNREACHABLE');
const data = await response.json();
const files = data.siblings?.map((s: { rfilename: string }) => s.rfilename) || [];
const hasSafetensors = files.some((f: string) => f.endsWith('.safetensors'));
const hasPickles = files.some((f: string) => f.endsWith('.bin') || f.endsWith('.pth'));
const auditLog = [
`SCANNING_REPOSITORY: ${modelId}`,
hasSafetensors
? 'STATUS: safetensors_identified. Weight integrity prioritized.'
: 'WARNING: No safetensors found. Falling back to legacy tensors.',
hasPickles
? 'SECURITY_ALERT: Pickle-based tensors detected. Potential for arbitrary code execution.'
: 'STATUS: No binary pickles identified.',
];
// Verification logic: 3rd party verification simulated based on metadata presence
const hasSignedWeights = data.tags?.includes('signed') || hasSafetensors;
return {
isSafe: hasSafetensors && !hasPickles,
hasSafetensors,
hasSignedWeights,
vulnerabilityScore: hasPickles ? 0.8 : 0.05,
auditLog,
};
} catch {
return {
isSafe: false,
hasSafetensors: false,
hasSignedWeights: false,
vulnerabilityScore: 1.0,
auditLog: ['AUDIT_CRITICAL_FAILURE: Substrate unresponsive.'],
};
}
},
};