@gradio/client 0.15.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/README.md +49 -43
- package/dist/client.d.ts +52 -70
- package/dist/client.d.ts.map +1 -1
- package/dist/constants.d.ts +17 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/helpers/api_info.d.ts +25 -0
- package/dist/helpers/api_info.d.ts.map +1 -0
- package/dist/helpers/data.d.ts +8 -0
- package/dist/helpers/data.d.ts.map +1 -0
- package/dist/{utils.d.ts → helpers/init_helpers.d.ts} +6 -17
- package/dist/helpers/init_helpers.d.ts.map +1 -0
- package/dist/helpers/spaces.d.ts +7 -0
- package/dist/helpers/spaces.d.ts.map +1 -0
- package/dist/index.d.ts +8 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1587 -1359
- package/dist/types.d.ts +152 -49
- package/dist/types.d.ts.map +1 -1
- package/dist/upload.d.ts +2 -2
- package/dist/upload.d.ts.map +1 -1
- package/dist/utils/duplicate.d.ts +4 -0
- package/dist/utils/duplicate.d.ts.map +1 -0
- package/dist/utils/handle_blob.d.ts +4 -0
- package/dist/utils/handle_blob.d.ts.map +1 -0
- package/dist/utils/post_data.d.ts +4 -0
- package/dist/utils/post_data.d.ts.map +1 -0
- package/dist/utils/predict.d.ts +4 -0
- package/dist/utils/predict.d.ts.map +1 -0
- package/dist/utils/stream.d.ts +8 -0
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/submit.d.ts +4 -0
- package/dist/utils/submit.d.ts.map +1 -0
- package/dist/utils/upload_files.d.ts +4 -0
- package/dist/utils/upload_files.d.ts.map +1 -0
- package/dist/utils/view_api.d.ts +3 -0
- package/dist/utils/view_api.d.ts.map +1 -0
- package/dist/{wrapper-6f348d45.js → wrapper-CviSselG.js} +259 -17
- package/package.json +3 -2
- package/src/client.ts +290 -1652
- package/src/constants.ts +20 -0
- package/src/globals.d.ts +2 -21
- package/src/helpers/api_info.ts +300 -0
- package/src/helpers/data.ts +115 -0
- package/src/helpers/init_helpers.ts +134 -0
- package/src/helpers/spaces.ts +192 -0
- package/src/index.ts +16 -10
- package/src/types.ts +200 -59
- package/src/upload.ts +23 -15
- package/src/{client.node-test.ts → utils/client.node-test.ts} +11 -10
- package/src/utils/duplicate.ts +87 -0
- package/src/utils/handle_blob.ts +47 -0
- package/src/utils/post_data.ts +37 -0
- package/src/utils/predict.ts +56 -0
- package/src/utils/stream.ts +166 -0
- package/src/utils/submit.ts +689 -0
- package/src/utils/upload_files.ts +46 -0
- package/src/utils/view_api.ts +70 -0
- package/src/vite-env.d.ts +1 -0
- package/tsconfig.json +15 -2
- package/vite.config.js +4 -17
- package/dist/utils.d.ts.map +0 -1
- package/src/utils.ts +0 -300
@@ -0,0 +1,46 @@
|
|
1
|
+
import type { Client } from "..";
|
2
|
+
import { BROKEN_CONNECTION_MSG } from "../constants";
|
3
|
+
import type { UploadResponse } from "../types";
|
4
|
+
|
5
|
+
export async function upload_files(
|
6
|
+
this: Client,
|
7
|
+
root_url: string,
|
8
|
+
files: (Blob | File)[],
|
9
|
+
upload_id?: string
|
10
|
+
): Promise<UploadResponse> {
|
11
|
+
const headers: {
|
12
|
+
Authorization?: string;
|
13
|
+
} = {};
|
14
|
+
if (this.options.hf_token) {
|
15
|
+
headers.Authorization = `Bearer ${this.options.hf_token}`;
|
16
|
+
}
|
17
|
+
const chunkSize = 1000;
|
18
|
+
const uploadResponses = [];
|
19
|
+
for (let i = 0; i < files.length; i += chunkSize) {
|
20
|
+
const chunk = files.slice(i, i + chunkSize);
|
21
|
+
const formData = new FormData();
|
22
|
+
chunk.forEach((file) => {
|
23
|
+
formData.append("files", file);
|
24
|
+
});
|
25
|
+
try {
|
26
|
+
const upload_url = upload_id
|
27
|
+
? `${root_url}/upload?upload_id=${upload_id}`
|
28
|
+
: `${root_url}/upload`;
|
29
|
+
var response = await this.fetch_implementation(upload_url, {
|
30
|
+
method: "POST",
|
31
|
+
body: formData,
|
32
|
+
headers
|
33
|
+
});
|
34
|
+
} catch (e) {
|
35
|
+
return { error: BROKEN_CONNECTION_MSG };
|
36
|
+
}
|
37
|
+
if (!response.ok) {
|
38
|
+
return { error: await response.text() };
|
39
|
+
}
|
40
|
+
const output: UploadResponse["files"] = await response.json();
|
41
|
+
if (output) {
|
42
|
+
uploadResponses.push(...output);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
return { files: uploadResponses };
|
46
|
+
}
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import type { ApiInfo, ApiData } from "../types";
|
2
|
+
import semiver from "semiver";
|
3
|
+
import { API_INFO_URL, BROKEN_CONNECTION_MSG } from "../constants";
|
4
|
+
import { Client } from "../client";
|
5
|
+
import { SPACE_FETCHER_URL } from "../constants";
|
6
|
+
import { transform_api_info } from "../helpers/api_info";
|
7
|
+
|
8
|
+
export async function view_api(this: Client): Promise<any> {
|
9
|
+
if (this.api_info) return this.api_info;
|
10
|
+
|
11
|
+
const { hf_token } = this.options;
|
12
|
+
const { config } = this;
|
13
|
+
|
14
|
+
const headers: {
|
15
|
+
Authorization?: string;
|
16
|
+
"Content-Type": "application/json";
|
17
|
+
} = { "Content-Type": "application/json" };
|
18
|
+
|
19
|
+
if (hf_token) {
|
20
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
21
|
+
}
|
22
|
+
|
23
|
+
if (!config) {
|
24
|
+
return;
|
25
|
+
}
|
26
|
+
|
27
|
+
try {
|
28
|
+
let response: Response;
|
29
|
+
|
30
|
+
if (semiver(config?.version || "2.0.0", "3.30") < 0) {
|
31
|
+
response = await this.fetch_implementation(SPACE_FETCHER_URL, {
|
32
|
+
method: "POST",
|
33
|
+
body: JSON.stringify({
|
34
|
+
serialize: false,
|
35
|
+
config: JSON.stringify(config)
|
36
|
+
}),
|
37
|
+
headers
|
38
|
+
});
|
39
|
+
} else {
|
40
|
+
response = await this.fetch_implementation(
|
41
|
+
`${config?.root}/${API_INFO_URL}`,
|
42
|
+
{
|
43
|
+
headers
|
44
|
+
}
|
45
|
+
);
|
46
|
+
}
|
47
|
+
|
48
|
+
if (!response.ok) {
|
49
|
+
throw new Error(BROKEN_CONNECTION_MSG);
|
50
|
+
}
|
51
|
+
|
52
|
+
let api_info = (await response.json()) as
|
53
|
+
| ApiInfo<ApiData>
|
54
|
+
| { api: ApiInfo<ApiData> };
|
55
|
+
if ("api" in api_info) {
|
56
|
+
api_info = api_info.api;
|
57
|
+
}
|
58
|
+
|
59
|
+
if (
|
60
|
+
api_info.named_endpoints["/predict"] &&
|
61
|
+
!api_info.unnamed_endpoints["0"]
|
62
|
+
) {
|
63
|
+
api_info.unnamed_endpoints[0] = api_info.named_endpoints["/predict"];
|
64
|
+
}
|
65
|
+
|
66
|
+
return transform_api_info(api_info, config, this.api_map);
|
67
|
+
} catch (e) {
|
68
|
+
"Could not get API info. " + (e as Error).message;
|
69
|
+
}
|
70
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
/// <reference types="vite/client" />
|
package/tsconfig.json
CHANGED
@@ -7,8 +7,21 @@
|
|
7
7
|
"emitDeclarationOnly": true,
|
8
8
|
"outDir": "dist",
|
9
9
|
"declarationMap": true,
|
10
|
-
"module": "
|
10
|
+
"module": "ESNext",
|
11
|
+
"target": "ES2020",
|
12
|
+
"useDefineForClassFields": true,
|
13
|
+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
14
|
+
"skipLibCheck": true,
|
15
|
+
|
16
|
+
/* Bundler */
|
11
17
|
"moduleResolution": "bundler",
|
12
|
-
"skipDefaultLibCheck": true
|
18
|
+
"skipDefaultLibCheck": true,
|
19
|
+
"allowImportingTsExtensions": true,
|
20
|
+
"esModuleInterop": true,
|
21
|
+
"resolveJsonModule": true,
|
22
|
+
"isolatedModules": true,
|
23
|
+
|
24
|
+
/* Linting */
|
25
|
+
"strict": true
|
13
26
|
}
|
14
27
|
}
|
package/vite.config.js
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
import { defineConfig } from "vite";
|
2
2
|
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
3
|
-
import { fileURLToPath } from "url";
|
4
|
-
import path from "path";
|
5
|
-
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
6
3
|
|
7
4
|
export default defineConfig({
|
8
5
|
build: {
|
9
6
|
lib: {
|
10
7
|
entry: "src/index.ts",
|
11
|
-
formats: ["es"]
|
8
|
+
formats: ["es"],
|
9
|
+
fileName: (format) => `index.${format}.js`
|
12
10
|
},
|
13
11
|
rollupOptions: {
|
14
12
|
input: "src/index.ts",
|
@@ -17,22 +15,11 @@ export default defineConfig({
|
|
17
15
|
}
|
18
16
|
}
|
19
17
|
},
|
20
|
-
plugins: [
|
21
|
-
svelte()
|
22
|
-
// {
|
23
|
-
// name: "resolve-gradio-client",
|
24
|
-
// enforce: "pre",
|
25
|
-
// resolveId(id) {
|
26
|
-
// if (id === "@gradio/client") {
|
27
|
-
// return path.join(__dirname, "src", "index.ts");
|
28
|
-
// }
|
29
|
-
// }
|
30
|
-
// }
|
31
|
-
],
|
18
|
+
plugins: [svelte()],
|
32
19
|
|
33
20
|
ssr: {
|
34
21
|
target: "node",
|
35
22
|
format: "esm",
|
36
|
-
noExternal: ["ws", "semiver", "@gradio/upload"]
|
23
|
+
noExternal: ["ws", "semiver", "bufferutil", "@gradio/upload"]
|
37
24
|
}
|
38
25
|
});
|
package/dist/utils.d.ts.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC3B,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,eAAe,EAAE,OAAO,GACtB,MAAM,CAKR;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG;IACrD,WAAW,EAAE,IAAI,GAAG,KAAK,CAAC;IAC1B,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;IAClC,IAAI,EAAE,MAAM,CAAC;CACb,CAgCA;AAED,eAAO,MAAM,aAAa,QAAqB,CAAC;AAChD,eAAO,MAAM,eAAe,QAAwB,CAAC;AACrD,wBAAsB,gBAAgB,CACrC,aAAa,EAAE,MAAM,EACrB,KAAK,CAAC,EAAE,MAAM,MAAM,EAAE,GACpB,OAAO,CAAC;IACV,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,IAAI,GAAG,KAAK,CAAC;IAC1B,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;CAClC,CAAC,CA4CD;AAED,wBAAgB,gBAAgB,CAC/B,GAAG,EAAE,MAAM,CAAC,cAAc,CAAC,GACzB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQxB;AAID,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAe5E;AAED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,CAqB1C;AAED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,EAC7C,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,CAuB1C;AAED,wBAAsB,iBAAiB,CACtC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,MAAM,CAAC,CAuBjB;AAED,eAAO,MAAM,cAAc,0GAQjB,CAAC;AAkDX,wBAAgB,UAAU,CACzB,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,CAAC,MAAM,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,GACxC,GAAG,CAML"}
|
package/src/utils.ts
DELETED
@@ -1,300 +0,0 @@
|
|
1
|
-
import type { Config } from "./types.js";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* This function is used to resolve the URL for making requests when the app has a root path.
|
5
|
-
* The root path could be a path suffix like "/app" which is appended to the end of the base URL. Or
|
6
|
-
* it could be a full URL like "https://abidlabs-test-client-replica--gqf2x.hf.space" which is used when hosting
|
7
|
-
* Gradio apps on Hugging Face Spaces.
|
8
|
-
* @param {string} base_url The base URL at which the Gradio server is hosted
|
9
|
-
* @param {string} root_path The root path, which could be a path suffix (e.g. mounted in FastAPI app) or a full URL (e.g. hosted on Hugging Face Spaces)
|
10
|
-
* @param {boolean} prioritize_base Whether to prioritize the base URL over the root path. This is used when both the base path and root paths are full URLs. For example, for fetching files the root path should be prioritized, but for making requests, the base URL should be prioritized.
|
11
|
-
* @returns {string} the resolved URL
|
12
|
-
*/
|
13
|
-
export function resolve_root(
|
14
|
-
base_url: string,
|
15
|
-
root_path: string,
|
16
|
-
prioritize_base: boolean
|
17
|
-
): string {
|
18
|
-
if (root_path.startsWith("http://") || root_path.startsWith("https://")) {
|
19
|
-
return prioritize_base ? base_url : root_path;
|
20
|
-
}
|
21
|
-
return base_url + root_path;
|
22
|
-
}
|
23
|
-
|
24
|
-
export function determine_protocol(endpoint: string): {
|
25
|
-
ws_protocol: "ws" | "wss";
|
26
|
-
http_protocol: "http:" | "https:";
|
27
|
-
host: string;
|
28
|
-
} {
|
29
|
-
if (endpoint.startsWith("http")) {
|
30
|
-
const { protocol, host } = new URL(endpoint);
|
31
|
-
|
32
|
-
if (host.endsWith("hf.space")) {
|
33
|
-
return {
|
34
|
-
ws_protocol: "wss",
|
35
|
-
host: host,
|
36
|
-
http_protocol: protocol as "http:" | "https:"
|
37
|
-
};
|
38
|
-
}
|
39
|
-
return {
|
40
|
-
ws_protocol: protocol === "https:" ? "wss" : "ws",
|
41
|
-
http_protocol: protocol as "http:" | "https:",
|
42
|
-
host
|
43
|
-
};
|
44
|
-
} else if (endpoint.startsWith("file:")) {
|
45
|
-
// This case is only expected to be used for the Wasm mode (Gradio-lite),
|
46
|
-
// where users can create a local HTML file using it and open the page in a browser directly via the `file:` protocol.
|
47
|
-
return {
|
48
|
-
ws_protocol: "ws",
|
49
|
-
http_protocol: "http:",
|
50
|
-
host: "lite.local" // Special fake hostname only used for this case. This matches the hostname allowed in `is_self_host()` in `js/wasm/network/host.ts`.
|
51
|
-
};
|
52
|
-
}
|
53
|
-
|
54
|
-
// default to secure if no protocol is provided
|
55
|
-
return {
|
56
|
-
ws_protocol: "wss",
|
57
|
-
http_protocol: "https:",
|
58
|
-
host: endpoint
|
59
|
-
};
|
60
|
-
}
|
61
|
-
|
62
|
-
export const RE_SPACE_NAME = /^[^\/]*\/[^\/]*$/;
|
63
|
-
export const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}$/;
|
64
|
-
export async function process_endpoint(
|
65
|
-
app_reference: string,
|
66
|
-
token?: `hf_${string}`
|
67
|
-
): Promise<{
|
68
|
-
space_id: string | false;
|
69
|
-
host: string;
|
70
|
-
ws_protocol: "ws" | "wss";
|
71
|
-
http_protocol: "http:" | "https:";
|
72
|
-
}> {
|
73
|
-
const headers: { Authorization?: string } = {};
|
74
|
-
if (token) {
|
75
|
-
headers.Authorization = `Bearer ${token}`;
|
76
|
-
}
|
77
|
-
|
78
|
-
const _app_reference = app_reference.trim();
|
79
|
-
|
80
|
-
if (RE_SPACE_NAME.test(_app_reference)) {
|
81
|
-
try {
|
82
|
-
const res = await fetch(
|
83
|
-
`https://huggingface.co/api/spaces/${_app_reference}/host`,
|
84
|
-
{ headers }
|
85
|
-
);
|
86
|
-
|
87
|
-
if (res.status !== 200)
|
88
|
-
throw new Error("Space metadata could not be loaded.");
|
89
|
-
const _host = (await res.json()).host;
|
90
|
-
|
91
|
-
return {
|
92
|
-
space_id: app_reference,
|
93
|
-
...determine_protocol(_host)
|
94
|
-
};
|
95
|
-
} catch (e: any) {
|
96
|
-
throw new Error("Space metadata could not be loaded." + e.message);
|
97
|
-
}
|
98
|
-
}
|
99
|
-
|
100
|
-
if (RE_SPACE_DOMAIN.test(_app_reference)) {
|
101
|
-
const { ws_protocol, http_protocol, host } =
|
102
|
-
determine_protocol(_app_reference);
|
103
|
-
|
104
|
-
return {
|
105
|
-
space_id: host.replace(".hf.space", ""),
|
106
|
-
ws_protocol,
|
107
|
-
http_protocol,
|
108
|
-
host
|
109
|
-
};
|
110
|
-
}
|
111
|
-
|
112
|
-
return {
|
113
|
-
space_id: false,
|
114
|
-
...determine_protocol(_app_reference)
|
115
|
-
};
|
116
|
-
}
|
117
|
-
|
118
|
-
export function map_names_to_ids(
|
119
|
-
fns: Config["dependencies"]
|
120
|
-
): Record<string, number> {
|
121
|
-
let apis: Record<string, number> = {};
|
122
|
-
|
123
|
-
fns.forEach(({ api_name }, i) => {
|
124
|
-
if (api_name) apis[api_name] = i;
|
125
|
-
});
|
126
|
-
|
127
|
-
return apis;
|
128
|
-
}
|
129
|
-
|
130
|
-
const RE_DISABLED_DISCUSSION =
|
131
|
-
/^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/;
|
132
|
-
export async function discussions_enabled(space_id: string): Promise<boolean> {
|
133
|
-
try {
|
134
|
-
const r = await fetch(
|
135
|
-
`https://huggingface.co/api/spaces/${space_id}/discussions`,
|
136
|
-
{
|
137
|
-
method: "HEAD"
|
138
|
-
}
|
139
|
-
);
|
140
|
-
const error = r.headers.get("x-error-message");
|
141
|
-
|
142
|
-
if (error && RE_DISABLED_DISCUSSION.test(error)) return false;
|
143
|
-
return true;
|
144
|
-
} catch (e) {
|
145
|
-
return false;
|
146
|
-
}
|
147
|
-
}
|
148
|
-
|
149
|
-
export async function get_space_hardware(
|
150
|
-
space_id: string,
|
151
|
-
token: `hf_${string}`
|
152
|
-
): Promise<(typeof hardware_types)[number]> {
|
153
|
-
const headers: { Authorization?: string } = {};
|
154
|
-
if (token) {
|
155
|
-
headers.Authorization = `Bearer ${token}`;
|
156
|
-
}
|
157
|
-
|
158
|
-
try {
|
159
|
-
const res = await fetch(
|
160
|
-
`https://huggingface.co/api/spaces/${space_id}/runtime`,
|
161
|
-
{ headers }
|
162
|
-
);
|
163
|
-
|
164
|
-
if (res.status !== 200)
|
165
|
-
throw new Error("Space hardware could not be obtained.");
|
166
|
-
|
167
|
-
const { hardware } = await res.json();
|
168
|
-
|
169
|
-
return hardware;
|
170
|
-
} catch (e: any) {
|
171
|
-
throw new Error(e.message);
|
172
|
-
}
|
173
|
-
}
|
174
|
-
|
175
|
-
export async function set_space_hardware(
|
176
|
-
space_id: string,
|
177
|
-
new_hardware: (typeof hardware_types)[number],
|
178
|
-
token: `hf_${string}`
|
179
|
-
): Promise<(typeof hardware_types)[number]> {
|
180
|
-
const headers: { Authorization?: string } = {};
|
181
|
-
if (token) {
|
182
|
-
headers.Authorization = `Bearer ${token}`;
|
183
|
-
}
|
184
|
-
|
185
|
-
try {
|
186
|
-
const res = await fetch(
|
187
|
-
`https://huggingface.co/api/spaces/${space_id}/hardware`,
|
188
|
-
{ headers, body: JSON.stringify(new_hardware) }
|
189
|
-
);
|
190
|
-
|
191
|
-
if (res.status !== 200)
|
192
|
-
throw new Error(
|
193
|
-
"Space hardware could not be set. Please ensure the space hardware provided is valid and that a Hugging Face token is passed in."
|
194
|
-
);
|
195
|
-
|
196
|
-
const { hardware } = await res.json();
|
197
|
-
|
198
|
-
return hardware;
|
199
|
-
} catch (e: any) {
|
200
|
-
throw new Error(e.message);
|
201
|
-
}
|
202
|
-
}
|
203
|
-
|
204
|
-
export async function set_space_timeout(
|
205
|
-
space_id: string,
|
206
|
-
timeout: number,
|
207
|
-
token: `hf_${string}`
|
208
|
-
): Promise<number> {
|
209
|
-
const headers: { Authorization?: string } = {};
|
210
|
-
if (token) {
|
211
|
-
headers.Authorization = `Bearer ${token}`;
|
212
|
-
}
|
213
|
-
|
214
|
-
try {
|
215
|
-
const res = await fetch(
|
216
|
-
`https://huggingface.co/api/spaces/${space_id}/hardware`,
|
217
|
-
{ headers, body: JSON.stringify({ seconds: timeout }) }
|
218
|
-
);
|
219
|
-
|
220
|
-
if (res.status !== 200)
|
221
|
-
throw new Error(
|
222
|
-
"Space hardware could not be set. Please ensure the space hardware provided is valid and that a Hugging Face token is passed in."
|
223
|
-
);
|
224
|
-
|
225
|
-
const { hardware } = await res.json();
|
226
|
-
|
227
|
-
return hardware;
|
228
|
-
} catch (e: any) {
|
229
|
-
throw new Error(e.message);
|
230
|
-
}
|
231
|
-
}
|
232
|
-
|
233
|
-
export const hardware_types = [
|
234
|
-
"cpu-basic",
|
235
|
-
"cpu-upgrade",
|
236
|
-
"t4-small",
|
237
|
-
"t4-medium",
|
238
|
-
"a10g-small",
|
239
|
-
"a10g-large",
|
240
|
-
"a100-large"
|
241
|
-
] as const;
|
242
|
-
|
243
|
-
function apply_edit(
|
244
|
-
target: any,
|
245
|
-
path: (number | string)[],
|
246
|
-
action: string,
|
247
|
-
value: any
|
248
|
-
): any {
|
249
|
-
if (path.length === 0) {
|
250
|
-
if (action === "replace") {
|
251
|
-
return value;
|
252
|
-
} else if (action === "append") {
|
253
|
-
return target + value;
|
254
|
-
}
|
255
|
-
throw new Error(`Unsupported action: ${action}`);
|
256
|
-
}
|
257
|
-
|
258
|
-
let current = target;
|
259
|
-
for (let i = 0; i < path.length - 1; i++) {
|
260
|
-
current = current[path[i]];
|
261
|
-
}
|
262
|
-
|
263
|
-
const last_path = path[path.length - 1];
|
264
|
-
switch (action) {
|
265
|
-
case "replace":
|
266
|
-
current[last_path] = value;
|
267
|
-
break;
|
268
|
-
case "append":
|
269
|
-
current[last_path] += value;
|
270
|
-
break;
|
271
|
-
case "add":
|
272
|
-
if (Array.isArray(current)) {
|
273
|
-
current.splice(Number(last_path), 0, value);
|
274
|
-
} else {
|
275
|
-
current[last_path] = value;
|
276
|
-
}
|
277
|
-
break;
|
278
|
-
case "delete":
|
279
|
-
if (Array.isArray(current)) {
|
280
|
-
current.splice(Number(last_path), 1);
|
281
|
-
} else {
|
282
|
-
delete current[last_path];
|
283
|
-
}
|
284
|
-
break;
|
285
|
-
default:
|
286
|
-
throw new Error(`Unknown action: ${action}`);
|
287
|
-
}
|
288
|
-
return target;
|
289
|
-
}
|
290
|
-
|
291
|
-
export function apply_diff(
|
292
|
-
obj: any,
|
293
|
-
diff: [string, (number | string)[], any][]
|
294
|
-
): any {
|
295
|
-
diff.forEach(([action, path, value]) => {
|
296
|
-
obj = apply_edit(obj, path, action, value);
|
297
|
-
});
|
298
|
-
|
299
|
-
return obj;
|
300
|
-
}
|