|
export interface HuggingFaceModelResponse { |
|
_id: string; |
|
id: string; |
|
inferenceProviderMapping: InferenceProviderMapping; |
|
} |
|
|
|
export type InferenceProviderMapping = { |
|
[k: string]: Provider; |
|
}; |
|
|
|
export interface Provider { |
|
status: string; |
|
providerId: string; |
|
task: string; |
|
} |
|
|
|
|
|
|
|
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; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function fetchHuggingFaceModel(modelId: string, token?: string): Promise<HuggingFaceModelResponse> { |
|
if (!modelId) { |
|
throw new Error("Model ID is required"); |
|
} |
|
|
|
|
|
const apiUrl = `https://huggingface.co/api/models/${modelId}?expand%5B%5D=inferenceProviderMapping`; |
|
|
|
|
|
const headers: HeadersInit = {}; |
|
if (token) { |
|
headers["Authorization"] = `Bearer ${token}`; |
|
} |
|
|
|
try { |
|
|
|
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; |
|
} |
|
|
|
|
|
throw new Error( |
|
`Error fetching Hugging Face model data: ${error instanceof Error ? error.message : "Unknown error"}` |
|
); |
|
} |
|
} |
|
|