@gradio/client 0.0.1 → 0.1.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 +13 -0
- package/LICENSE +201 -0
- package/README.md +323 -30
- package/dist/client.d.ts +35 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1016 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +21 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/wrapper-b7460963.js +3468 -0
- package/package.json +26 -3
- package/src/client.node-test.ts +172 -0
- package/src/client.ts +785 -151
- package/src/globals.d.ts +31 -0
- package/src/index.ts +2 -2
- package/src/types.ts +9 -4
- package/src/utils.ts +121 -11
- package/tsconfig.json +14 -0
- package/vite.config.js +23 -0
package/src/globals.d.ts
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
declare global {
|
2
|
+
interface Window {
|
3
|
+
__gradio_mode__: "app" | "website";
|
4
|
+
launchGradio: Function;
|
5
|
+
launchGradioFromSpaces: Function;
|
6
|
+
gradio_config: Config;
|
7
|
+
scoped_css_attach: (link: HTMLLinkElement) => void;
|
8
|
+
__is_colab__: boolean;
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
export interface Config {
|
13
|
+
auth_required: boolean | undefined;
|
14
|
+
auth_message: string;
|
15
|
+
components: any[];
|
16
|
+
css: string | null;
|
17
|
+
dependencies: any[];
|
18
|
+
dev_mode: boolean;
|
19
|
+
enable_queue: boolean;
|
20
|
+
layout: any;
|
21
|
+
mode: "blocks" | "interface";
|
22
|
+
root: string;
|
23
|
+
theme: string;
|
24
|
+
title: string;
|
25
|
+
version: string;
|
26
|
+
is_space: boolean;
|
27
|
+
is_colab: boolean;
|
28
|
+
show_api: boolean;
|
29
|
+
stylesheets: string[];
|
30
|
+
path: string;
|
31
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
export { client, post_data, upload_files } from "./client";
|
2
|
-
export type { SpaceStatus } from "./types";
|
1
|
+
export { client, post_data, upload_files, duplicate } from "./client.js";
|
2
|
+
export type { SpaceStatus } from "./types.js";
|
package/src/types.ts
CHANGED
@@ -21,7 +21,9 @@ export interface Config {
|
|
21
21
|
|
22
22
|
export interface Payload {
|
23
23
|
data: Array<unknown>;
|
24
|
-
fn_index
|
24
|
+
fn_index?: number;
|
25
|
+
event_data?: unknown;
|
26
|
+
time?: Date;
|
25
27
|
}
|
26
28
|
|
27
29
|
export interface PostResponse {
|
@@ -35,18 +37,21 @@ export interface UploadResponse {
|
|
35
37
|
|
36
38
|
export interface Status {
|
37
39
|
queue: boolean;
|
38
|
-
|
40
|
+
code?: string;
|
41
|
+
success?: boolean;
|
42
|
+
stage: "pending" | "error" | "complete" | "generating";
|
39
43
|
size?: number;
|
40
44
|
position?: number;
|
41
45
|
eta?: number;
|
42
46
|
message?: string;
|
43
|
-
|
47
|
+
progress_data?: Array<{
|
44
48
|
progress: number | null;
|
45
49
|
index: number | null;
|
46
50
|
length: number | null;
|
47
51
|
unit: string | null;
|
48
52
|
desc: string | null;
|
49
53
|
}>;
|
54
|
+
time?: Date;
|
50
55
|
}
|
51
56
|
|
52
57
|
export interface SpaceStatusNormal {
|
@@ -75,7 +80,7 @@ export type SpaceStatusCallback = (a: SpaceStatus) => void;
|
|
75
80
|
export type EventType = "data" | "status";
|
76
81
|
|
77
82
|
export interface EventMap {
|
78
|
-
data:
|
83
|
+
data: Payload;
|
79
84
|
status: Status;
|
80
85
|
}
|
81
86
|
|
package/src/utils.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { Config } from "./types";
|
1
|
+
import type { Config } from "./types.js";
|
2
2
|
|
3
3
|
export function determine_protocol(endpoint: string): {
|
4
4
|
ws_protocol: "ws" | "wss";
|
@@ -33,24 +33,40 @@ export function determine_protocol(endpoint: string): {
|
|
33
33
|
|
34
34
|
export const RE_SPACE_NAME = /^[^\/]*\/[^\/]*$/;
|
35
35
|
export const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}$/;
|
36
|
-
export async function process_endpoint(
|
36
|
+
export async function process_endpoint(
|
37
|
+
app_reference: string,
|
38
|
+
token?: `hf_${string}`
|
39
|
+
): Promise<{
|
37
40
|
space_id: string | false;
|
38
41
|
host: string;
|
39
42
|
ws_protocol: "ws" | "wss";
|
40
43
|
http_protocol: "http:" | "https:";
|
41
44
|
}> {
|
45
|
+
const headers: { Authorization?: string } = {};
|
46
|
+
if (token) {
|
47
|
+
headers.Authorization = `Bearer ${token}`;
|
48
|
+
}
|
49
|
+
|
42
50
|
const _app_reference = app_reference.trim();
|
43
51
|
|
44
52
|
if (RE_SPACE_NAME.test(_app_reference)) {
|
45
|
-
|
46
|
-
await (
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
53
|
+
try {
|
54
|
+
const res = await fetch(
|
55
|
+
`https://huggingface.co/api/spaces/${_app_reference}/host`,
|
56
|
+
{ headers }
|
57
|
+
);
|
58
|
+
|
59
|
+
if (res.status !== 200)
|
60
|
+
throw new Error("Space metadata could not be loaded.");
|
61
|
+
const _host = (await res.json()).host;
|
62
|
+
|
63
|
+
return {
|
64
|
+
space_id: app_reference,
|
65
|
+
...determine_protocol(_host)
|
66
|
+
};
|
67
|
+
} catch (e) {
|
68
|
+
throw new Error("Space metadata could not be loaded." + e.message);
|
69
|
+
}
|
54
70
|
}
|
55
71
|
|
56
72
|
if (RE_SPACE_DOMAIN.test(_app_reference)) {
|
@@ -99,3 +115,97 @@ export async function discussions_enabled(space_id: string) {
|
|
99
115
|
return false;
|
100
116
|
}
|
101
117
|
}
|
118
|
+
|
119
|
+
export async function get_space_hardware(
|
120
|
+
space_id: string,
|
121
|
+
token: `hf_${string}`
|
122
|
+
) {
|
123
|
+
const headers: { Authorization?: string } = {};
|
124
|
+
if (token) {
|
125
|
+
headers.Authorization = `Bearer ${token}`;
|
126
|
+
}
|
127
|
+
|
128
|
+
try {
|
129
|
+
const res = await fetch(
|
130
|
+
`https://huggingface.co/api/spaces/${space_id}/runtime`,
|
131
|
+
{ headers }
|
132
|
+
);
|
133
|
+
|
134
|
+
if (res.status !== 200)
|
135
|
+
throw new Error("Space hardware could not be obtained.");
|
136
|
+
|
137
|
+
const { hardware } = await res.json();
|
138
|
+
|
139
|
+
return hardware;
|
140
|
+
} catch (e) {
|
141
|
+
throw new Error(e.message);
|
142
|
+
}
|
143
|
+
}
|
144
|
+
|
145
|
+
export async function set_space_hardware(
|
146
|
+
space_id: string,
|
147
|
+
new_hardware: typeof hardware_types[number],
|
148
|
+
token: `hf_${string}`
|
149
|
+
) {
|
150
|
+
const headers: { Authorization?: string } = {};
|
151
|
+
if (token) {
|
152
|
+
headers.Authorization = `Bearer ${token}`;
|
153
|
+
}
|
154
|
+
|
155
|
+
try {
|
156
|
+
const res = await fetch(
|
157
|
+
`https://huggingface.co/api/spaces/${space_id}/hardware`,
|
158
|
+
{ headers, body: JSON.stringify(new_hardware) }
|
159
|
+
);
|
160
|
+
|
161
|
+
if (res.status !== 200)
|
162
|
+
throw new Error(
|
163
|
+
"Space hardware could not be set. Please ensure the space hardware provided is valid and that a Hugging Face token is passed in."
|
164
|
+
);
|
165
|
+
|
166
|
+
const { hardware } = await res.json();
|
167
|
+
|
168
|
+
return hardware;
|
169
|
+
} catch (e) {
|
170
|
+
throw new Error(e.message);
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
export async function set_space_timeout(
|
175
|
+
space_id: string,
|
176
|
+
timeout: number,
|
177
|
+
token: `hf_${string}`
|
178
|
+
) {
|
179
|
+
const headers: { Authorization?: string } = {};
|
180
|
+
if (token) {
|
181
|
+
headers.Authorization = `Bearer ${token}`;
|
182
|
+
}
|
183
|
+
|
184
|
+
try {
|
185
|
+
const res = await fetch(
|
186
|
+
`https://huggingface.co/api/spaces/${space_id}/hardware`,
|
187
|
+
{ headers, body: JSON.stringify({ seconds: timeout }) }
|
188
|
+
);
|
189
|
+
|
190
|
+
if (res.status !== 200)
|
191
|
+
throw new Error(
|
192
|
+
"Space hardware could not be set. Please ensure the space hardware provided is valid and that a Hugging Face token is passed in."
|
193
|
+
);
|
194
|
+
|
195
|
+
const { hardware } = await res.json();
|
196
|
+
|
197
|
+
return hardware;
|
198
|
+
} catch (e) {
|
199
|
+
throw new Error(e.message);
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
203
|
+
export const hardware_types = [
|
204
|
+
"cpu-basic",
|
205
|
+
"cpu-upgrade",
|
206
|
+
"t4-small",
|
207
|
+
"t4-medium",
|
208
|
+
"a10g-small",
|
209
|
+
"a10g-large",
|
210
|
+
"a100-large"
|
211
|
+
] as const;
|
package/tsconfig.json
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
{
|
2
|
+
"include": ["src/**/*"],
|
3
|
+
"exclude": ["src/**/*.test.ts", "src/**/*.node-test.ts"],
|
4
|
+
"compilerOptions": {
|
5
|
+
"allowJs": true,
|
6
|
+
"declaration": true,
|
7
|
+
"emitDeclarationOnly": true,
|
8
|
+
"outDir": "dist",
|
9
|
+
"declarationMap": true,
|
10
|
+
"module": "es2020",
|
11
|
+
"moduleResolution": "node16",
|
12
|
+
"skipDefaultLibCheck": true
|
13
|
+
}
|
14
|
+
}
|
package/vite.config.js
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
import { defineConfig } from "vite";
|
2
|
+
|
3
|
+
export default defineConfig({
|
4
|
+
build: {
|
5
|
+
// minify: true,
|
6
|
+
lib: {
|
7
|
+
entry: "src/index.ts",
|
8
|
+
formats: ["es"]
|
9
|
+
},
|
10
|
+
rollupOptions: {
|
11
|
+
input: "src/index.ts",
|
12
|
+
output: {
|
13
|
+
dir: "dist"
|
14
|
+
}
|
15
|
+
}
|
16
|
+
},
|
17
|
+
|
18
|
+
ssr: {
|
19
|
+
target: "node",
|
20
|
+
format: "esm",
|
21
|
+
noExternal: "ws"
|
22
|
+
}
|
23
|
+
});
|