File size: 2,006 Bytes
21dd449 |
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 |
import { HUB_URL } from "../consts";
import { createApiError } from "../error";
import type { ApiModelInfo } from "../types/api/api-model";
import type { CredentialsParams } from "../types/public";
import { checkCredentials } from "../utils/checkCredentials";
import { pick } from "../utils/pick";
import { MODEL_EXPAND_KEYS, type MODEL_EXPANDABLE_KEYS, type ModelEntry } from "./list-models";
export async function modelInfo<
const T extends Exclude<(typeof MODEL_EXPANDABLE_KEYS)[number], (typeof MODEL_EXPAND_KEYS)[number]> = never,
>(
params: {
name: string;
hubUrl?: string;
additionalFields?: T[];
/**
* An optional Git revision id which can be a branch name, a tag, or a commit hash.
*/
revision?: string;
/**
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
*/
fetch?: typeof fetch;
} & Partial<CredentialsParams>
): Promise<ModelEntry & Pick<ApiModelInfo, T>> {
const accessToken = params && checkCredentials(params);
const search = new URLSearchParams([
...MODEL_EXPAND_KEYS.map((val) => ["expand", val] satisfies [string, string]),
...(params?.additionalFields?.map((val) => ["expand", val] satisfies [string, string]) ?? []),
]).toString();
const response = await (params.fetch || fetch)(
`${params?.hubUrl || HUB_URL}/api/models/${params.name}/revision/${encodeURIComponent(
params.revision ?? "HEAD"
)}?${search.toString()}`,
{
headers: {
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
Accepts: "application/json",
},
}
);
if (!response.ok) {
throw await createApiError(response);
}
const data = await response.json();
return {
...(params?.additionalFields && pick(data, params.additionalFields)),
id: data._id,
name: data.id,
private: data.private,
task: data.pipeline_tag,
downloads: data.downloads,
gated: data.gated,
likes: data.likes,
updatedAt: new Date(data.lastModified),
} as ModelEntry & Pick<ApiModelInfo, T>;
}
|