Skip to content

Commit d623bbd

Browse files
authored
feat(hub): adding revision property to modelInfo, datasetInfo, spaceInfo (#999)
When getting modelInfo, datasetInfo, spaceInfo, we might need to get the details for a specific revision. This PR adds an optional `revision` property to those methods
1 parent 8a2079b commit d623bbd

File tree

6 files changed

+124
-3
lines changed

6 files changed

+124
-3
lines changed

packages/hub/src/lib/dataset-info.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, it } from "vitest";
22
import { datasetInfo } from "./dataset-info";
3+
import type { DatasetEntry } from "./list-datasets";
4+
import type { ApiDatasetInfo } from "../types/api/api-dataset";
35

46
describe("datasetInfo", () => {
57
it("should return the dataset info", async () => {
@@ -16,4 +18,39 @@ describe("datasetInfo", () => {
1618
private: false,
1719
});
1820
});
21+
22+
it("should return the dataset info with author", async () => {
23+
const info: DatasetEntry & Pick<ApiDatasetInfo, 'author'> = await datasetInfo({
24+
name: "nyu-mll/glue",
25+
additionalFields: ['author'],
26+
});
27+
expect(info).toEqual({
28+
id: "621ffdd236468d709f181e3f",
29+
downloads: expect.any(Number),
30+
gated: false,
31+
name: "nyu-mll/glue",
32+
updatedAt: expect.any(Date),
33+
likes: expect.any(Number),
34+
private: false,
35+
author: 'nyu-mll'
36+
});
37+
});
38+
39+
it("should return the dataset info for a specific revision", async () => {
40+
const info: DatasetEntry & Pick<ApiDatasetInfo, 'sha'> = await datasetInfo({
41+
name: "nyu-mll/glue",
42+
revision: "cb2099c76426ff97da7aa591cbd317d91fb5fcb7",
43+
additionalFields: ["sha"],
44+
});
45+
expect(info).toEqual({
46+
id: "621ffdd236468d709f181e3f",
47+
downloads: expect.any(Number),
48+
gated: false,
49+
name: "nyu-mll/glue",
50+
updatedAt: expect.any(Date),
51+
likes: expect.any(Number),
52+
private: false,
53+
sha: 'cb2099c76426ff97da7aa591cbd317d91fb5fcb7'
54+
});
55+
});
1956
});

packages/hub/src/lib/dataset-info.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export async function datasetInfo<
1313
name: string;
1414
hubUrl?: string;
1515
additionalFields?: T[];
16+
/**
17+
* An optional Git revision id which can be a branch name, a tag, or a commit hash.
18+
*/
19+
revision?: string;
1620
/**
1721
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
1822
*/
@@ -27,7 +31,7 @@ export async function datasetInfo<
2731
]).toString();
2832

2933
const response = await (params.fetch || fetch)(
30-
`${params?.hubUrl || HUB_URL}/api/datasets/${params.name}?${search.toString()}`,
34+
`${params?.hubUrl || HUB_URL}/api/datasets/${params.name}/revision/${encodeURIComponent(params.revision ?? "HEAD")}?${search.toString()}`,
3135
{
3236
headers: {
3337
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),

packages/hub/src/lib/model-info.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
import { describe, expect, it } from "vitest";
22
import { modelInfo } from "./model-info";
3+
import type { ModelEntry } from "./list-models";
4+
import type { ApiModelInfo } from "../types/api/api-model";
35

46
describe("modelInfo", () => {
57
it("should return the model info", async () => {
68
const info = await modelInfo({
79
name: "openai-community/gpt2",
10+
});
11+
expect(info).toEqual({
12+
id: "621ffdc036468d709f17434d",
13+
downloads: expect.any(Number),
14+
gated: false,
15+
name: "openai-community/gpt2",
16+
updatedAt: expect.any(Date),
17+
likes: expect.any(Number),
18+
task: "text-generation",
19+
private: false,
20+
});
21+
});
22+
23+
it("should return the model info with author", async () => {
24+
const info: ModelEntry & Pick<ApiModelInfo, 'author'> = await modelInfo({
25+
name: "openai-community/gpt2",
826
additionalFields: ["author"],
927
});
1028
expect(info).toEqual({
@@ -19,4 +37,23 @@ describe("modelInfo", () => {
1937
private: false,
2038
});
2139
});
40+
41+
it("should return the model info for a specific revision", async () => {
42+
const info: ModelEntry & Pick<ApiModelInfo, 'sha'> = await modelInfo({
43+
name: "openai-community/gpt2",
44+
additionalFields: ["sha"],
45+
revision: 'f27b190eeac4c2302d24068eabf5e9d6044389ae',
46+
});
47+
expect(info).toEqual({
48+
id: "621ffdc036468d709f17434d",
49+
downloads: expect.any(Number),
50+
gated: false,
51+
name: "openai-community/gpt2",
52+
updatedAt: expect.any(Date),
53+
likes: expect.any(Number),
54+
task: "text-generation",
55+
private: false,
56+
sha: "f27b190eeac4c2302d24068eabf5e9d6044389ae",
57+
});
58+
});
2259
});

packages/hub/src/lib/model-info.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export async function modelInfo<
1313
name: string;
1414
hubUrl?: string;
1515
additionalFields?: T[];
16+
/**
17+
* An optional Git revision id which can be a branch name, a tag, or a commit hash.
18+
*/
19+
revision?: string;
1620
/**
1721
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
1822
*/
@@ -27,7 +31,7 @@ export async function modelInfo<
2731
]).toString();
2832

2933
const response = await (params.fetch || fetch)(
30-
`${params?.hubUrl || HUB_URL}/api/models/${params.name}?${search.toString()}`,
34+
`${params?.hubUrl || HUB_URL}/api/models/${params.name}/revision/${encodeURIComponent(params.revision ?? "HEAD")}?${search.toString()}`,
3135
{
3236
headers: {
3337
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),

packages/hub/src/lib/space-info.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, it } from "vitest";
22
import { spaceInfo } from "./space-info";
3+
import type { SpaceEntry } from "./list-spaces";
4+
import type { ApiSpaceInfo } from "../types/api/api-space";
35

46
describe("spaceInfo", () => {
57
it("should return the space info", async () => {
@@ -15,4 +17,37 @@ describe("spaceInfo", () => {
1517
sdk: "static",
1618
});
1719
});
20+
21+
it("should return the space info with author", async () => {
22+
const info: SpaceEntry & Pick<ApiSpaceInfo, 'author'> = await spaceInfo({
23+
name: "huggingfacejs/client-side-oauth",
24+
additionalFields: ['author'],
25+
});
26+
expect(info).toEqual({
27+
id: "659835e689010f9c7aed608d",
28+
name: "huggingfacejs/client-side-oauth",
29+
updatedAt: expect.any(Date),
30+
likes: expect.any(Number),
31+
private: false,
32+
sdk: "static",
33+
author: 'huggingfacejs',
34+
});
35+
});
36+
37+
it("should return the space info for a given revision", async () => {
38+
const info: SpaceEntry & Pick<ApiSpaceInfo, 'sha'> = await spaceInfo({
39+
name: "huggingfacejs/client-side-oauth",
40+
additionalFields: ['sha'],
41+
revision: 'e410a9ff348e6bed393b847711e793282d7c672e'
42+
});
43+
expect(info).toEqual({
44+
id: "659835e689010f9c7aed608d",
45+
name: "huggingfacejs/client-side-oauth",
46+
updatedAt: expect.any(Date),
47+
likes: expect.any(Number),
48+
private: false,
49+
sdk: "static",
50+
sha: 'e410a9ff348e6bed393b847711e793282d7c672e',
51+
});
52+
});
1853
});

packages/hub/src/lib/space-info.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export async function spaceInfo<
1414
name: string;
1515
hubUrl?: string;
1616
additionalFields?: T[];
17+
/**
18+
* An optional Git revision id which can be a branch name, a tag, or a commit hash.
19+
*/
20+
revision?: string;
1721
/**
1822
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
1923
*/
@@ -28,7 +32,7 @@ export async function spaceInfo<
2832
]).toString();
2933

3034
const response = await (params.fetch || fetch)(
31-
`${params?.hubUrl || HUB_URL}/api/spaces/${params.name}?${search.toString()}`,
35+
`${params?.hubUrl || HUB_URL}/api/spaces/${params.name}/revision/${encodeURIComponent(params.revision ?? "HEAD")}?${search.toString()}`,
3236
{
3337
headers: {
3438
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),

0 commit comments

Comments
 (0)