File size: 2,009 Bytes
25c63d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export interface HuggingFaceModelResponse {
	_id: string;
	id: string;
	inferenceProviderMapping: InferenceProviderMapping;
}

export type InferenceProviderMapping = {
	[k: string]: Provider;
};

export interface Provider {
	status: string;
	providerId: string;
	task: string;
}
/**
 * Error thrown when the Hugging Face API request fails
 */
export class HuggingFaceApiError extends Error {
	status: number;
	details: string;

	constructor(message: string, status: number, details: string) {
		super(message);
		this.name = "HuggingFaceApiError";
		this.status = status;
		this.details = details;
	}
}

/**
 * Fetches model data from the Hugging Face API
 *
 * @param modelId - The Hugging Face model ID (can include namespace like "username/model-name")
 * @param token - Optional Hugging Face API token for authentication
 * @returns Promise resolving to the model data
 * @throws {HuggingFaceApiError} When the API request fails
 */
export async function fetchHuggingFaceModel(modelId: string, token?: string): Promise<HuggingFaceModelResponse> {
	if (!modelId) {
		throw new Error("Model ID is required");
	}

	// Construct the API URL
	const apiUrl = `https://huggingface.co/api/models/${modelId}?expand%5B%5D=inferenceProviderMapping`;

	// Prepare headers for the request
	const headers: HeadersInit = {};
	if (token) {
		headers["Authorization"] = `Bearer ${token}`;
	}

	try {
		// Make the request to Hugging Face API
		const response = await fetch(apiUrl, {
			method: "GET",
			headers,
		});

		if (!response.ok) {
			const errorText = await response.text();
			throw new HuggingFaceApiError("Failed to fetch data from Hugging Face API", response.status, errorText);
		}

		return (await response.json()) as HuggingFaceModelResponse;
	} catch (error) {
		if (error instanceof HuggingFaceApiError) {
			throw error;
		}

		// Handle other errors (network, etc.)
		throw new Error(
			`Error fetching Hugging Face model data: ${error instanceof Error ? error.message : "Unknown error"}`
		);
	}
}