-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add modelInfo, spaceInfo, datasetInfo
- Loading branch information
1 parent
22e6d4d
commit 70ae58f
Showing
11 changed files
with
260 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { datasetInfo } from "./dataset-info"; | ||
|
||
describe("datasetInfo", () => { | ||
it("should return the dataset info", async () => { | ||
const info = await datasetInfo({ | ||
name: "nyu-mll/glue", | ||
}); | ||
expect(info).toEqual({ | ||
id: "621ffdd236468d709f181e3f", | ||
downloads: expect.any(Number), | ||
gated: false, | ||
name: "nyu-mll/glue", | ||
updatedAt: expect.any(Date), | ||
likes: expect.any(Number), | ||
private: false, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { HUB_URL } from "../consts"; | ||
import { createApiError } from "../error"; | ||
import type { ApiDatasetInfo } from "../types/api/api-dataset"; | ||
import type { CredentialsParams } from "../types/public"; | ||
import { checkCredentials } from "../utils/checkCredentials"; | ||
import { pick } from "../utils/pick"; | ||
import { type DATASET_EXPANDABLE_KEYS, DATASET_EXPAND_KEYS, type DatasetEntry } from "./list-datasets"; | ||
|
||
export async function datasetInfo< | ||
const T extends Exclude<(typeof DATASET_EXPANDABLE_KEYS)[number], (typeof DATASET_EXPAND_KEYS)[number]> = never, | ||
>( | ||
params: { | ||
name: string; | ||
hubUrl?: string; | ||
additionalFields?: T[]; | ||
/** | ||
* Set to limit the number of models returned. | ||
*/ | ||
limit?: number; | ||
/** | ||
* 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<DatasetEntry & Pick<ApiDatasetInfo, T>> { | ||
const accessToken = params && checkCredentials(params); | ||
|
||
const search = new URLSearchParams([ | ||
...DATASET_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/datasets/${params.name}?${search.toString()}`, | ||
{ | ||
headers: { | ||
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}), | ||
Accepts: "application/json", | ||
}, | ||
} | ||
); | ||
|
||
if (!response.ok) { | ||
createApiError(response); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
return { | ||
...(params?.additionalFields && pick(data, params.additionalFields)), | ||
id: data._id, | ||
name: data.id, | ||
private: data.private, | ||
downloads: data.downloads, | ||
likes: data.likes, | ||
gated: data.gated, | ||
updatedAt: new Date(data.lastModified), | ||
} as DatasetEntry & Pick<ApiDatasetInfo, T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { modelInfo } from "./model-info"; | ||
|
||
describe("modelInfo", () => { | ||
it("should return the model info", async () => { | ||
const info = await modelInfo({ | ||
name: "openai-community/gpt2", | ||
}); | ||
expect(info).toEqual({ | ||
id: "621ffdc036468d709f17434d", | ||
downloads: expect.any(Number), | ||
gated: false, | ||
name: "openai-community/gpt2", | ||
updatedAt: expect.any(Date), | ||
likes: expect.any(Number), | ||
task: "text-generation", | ||
private: false, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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_EXPANDABLE_KEYS)[number]> = never, | ||
>( | ||
params: { | ||
name: string; | ||
hubUrl?: string; | ||
additionalFields?: T[]; | ||
/** | ||
* Set to limit the number of models returned. | ||
*/ | ||
limit?: number; | ||
/** | ||
* 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}?${search.toString()}`, | ||
{ | ||
headers: { | ||
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}), | ||
Accepts: "application/json", | ||
}, | ||
} | ||
); | ||
|
||
if (!response.ok) { | ||
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { spaceInfo } from "./space-info"; | ||
|
||
describe("spaceInfo", () => { | ||
it("should return the space info", async () => { | ||
const info = await spaceInfo({ | ||
name: "huggingfacejs/client-side-oauth", | ||
}); | ||
expect(info).toEqual({ | ||
id: "659835e689010f9c7aed608d", | ||
name: "huggingfacejs/client-side-oauth", | ||
updatedAt: expect.any(Date), | ||
likes: expect.any(Number), | ||
private: false, | ||
sdk: "static", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { HUB_URL } from "../consts"; | ||
import { createApiError } from "../error"; | ||
import type { ApiSpaceInfo } from "../types/api/api-space"; | ||
import type { CredentialsParams } from "../types/public"; | ||
import { checkCredentials } from "../utils/checkCredentials"; | ||
import { pick } from "../utils/pick"; | ||
import type { SPACE_EXPANDABLE_KEYS, SpaceEntry } from "./list-spaces"; | ||
import { SPACE_EXPAND_KEYS } from "./list-spaces"; | ||
|
||
export async function spaceInfo< | ||
const T extends Exclude<(typeof SPACE_EXPANDABLE_KEYS)[number], (typeof SPACE_EXPAND_KEYS)[number]> = never, | ||
>( | ||
params: { | ||
name: string; | ||
hubUrl?: string; | ||
additionalFields?: T[]; | ||
/** | ||
* Set to limit the number of models returned. | ||
*/ | ||
limit?: number; | ||
/** | ||
* 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<SpaceEntry & Pick<ApiSpaceInfo, T>> { | ||
const accessToken = params && checkCredentials(params); | ||
|
||
const search = new URLSearchParams([ | ||
...SPACE_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/spaces/${params.name}?${search.toString()}`, | ||
{ | ||
headers: { | ||
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}), | ||
Accepts: "application/json", | ||
}, | ||
} | ||
); | ||
|
||
if (!response.ok) { | ||
createApiError(response); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
return { | ||
...(params?.additionalFields && pick(data, params.additionalFields)), | ||
id: data._id, | ||
name: data.id, | ||
sdk: data.sdk, | ||
likes: data.likes, | ||
private: data.private, | ||
updatedAt: new Date(data.lastModified), | ||
} as SpaceEntry & Pick<ApiSpaceInfo, T>; | ||
} |