@gradio/client 0.1.3 → 0.2.1

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/src/globals.d.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  declare global {
2
2
  interface Window {
3
3
  __gradio_mode__: "app" | "website";
4
- launchGradio: Function;
5
- launchGradioFromSpaces: Function;
6
4
  gradio_config: Config;
7
- scoped_css_attach: (link: HTMLLinkElement) => void;
8
5
  __is_colab__: boolean;
6
+ __gradio_space__: string | null;
9
7
  }
10
8
  }
11
9
 
@@ -23,7 +21,7 @@ export interface Config {
23
21
  theme: string;
24
22
  title: string;
25
23
  version: string;
26
- is_space: boolean;
24
+ space_id: string | null;
27
25
  is_colab: boolean;
28
26
  show_api: boolean;
29
27
  stylesheets: string[];
package/src/index.ts CHANGED
@@ -1,2 +1,8 @@
1
- export { client, post_data, upload_files, duplicate } from "./client.js";
1
+ export {
2
+ client,
3
+ post_data,
4
+ upload_files,
5
+ duplicate,
6
+ api_factory
7
+ } from "./client.js";
2
8
  export type { SpaceStatus } from "./types.js";
package/src/types.ts CHANGED
@@ -13,7 +13,7 @@ export interface Config {
13
13
  theme: string;
14
14
  title: string;
15
15
  version: string;
16
- is_space: boolean;
16
+ space_id: string | null;
17
17
  is_colab: boolean;
18
18
  show_api: boolean;
19
19
  stylesheets: string[];
@@ -21,7 +21,7 @@ export interface Config {
21
21
  }
22
22
 
23
23
  export interface Payload {
24
- data: Array<unknown>;
24
+ data: unknown[];
25
25
  fn_index?: number;
26
26
  event_data?: unknown;
27
27
  time?: Date;
@@ -33,7 +33,7 @@ export interface PostResponse {
33
33
  }
34
34
  export interface UploadResponse {
35
35
  error?: string;
36
- files?: Array<string>;
36
+ files?: string[];
37
37
  }
38
38
 
39
39
  export interface Status {
@@ -41,20 +41,26 @@ export interface Status {
41
41
  code?: string;
42
42
  success?: boolean;
43
43
  stage: "pending" | "error" | "complete" | "generating";
44
+ broken?: boolean;
44
45
  size?: number;
45
46
  position?: number;
46
47
  eta?: number;
47
48
  message?: string;
48
- progress_data?: Array<{
49
+ progress_data?: {
49
50
  progress: number | null;
50
51
  index: number | null;
51
52
  length: number | null;
52
53
  unit: string | null;
53
54
  desc: string | null;
54
- }>;
55
+ }[];
55
56
  time?: Date;
56
57
  }
57
58
 
59
+ export interface LogMessage {
60
+ log: string;
61
+ level: "warning" | "info";
62
+ }
63
+
58
64
  export interface SpaceStatusNormal {
59
65
  status: "sleeping" | "running" | "building" | "error" | "stopped";
60
66
  detail:
@@ -83,11 +89,12 @@ export type SpaceStatus = SpaceStatusNormal | SpaceStatusError;
83
89
  export type status_callback_function = (a: Status) => void;
84
90
  export type SpaceStatusCallback = (a: SpaceStatus) => void;
85
91
 
86
- export type EventType = "data" | "status";
92
+ export type EventType = "data" | "status" | "log";
87
93
 
88
94
  export interface EventMap {
89
95
  data: Payload;
90
96
  status: Status;
97
+ log: LogMessage;
91
98
  }
92
99
 
93
100
  export type Event<K extends EventType> = {
package/src/utils.ts CHANGED
@@ -14,13 +14,12 @@ export function determine_protocol(endpoint: string): {
14
14
  host: host,
15
15
  http_protocol: protocol as "http:" | "https:"
16
16
  };
17
- } else {
18
- return {
19
- ws_protocol: protocol === "https:" ? "wss" : "ws",
20
- http_protocol: protocol as "http:" | "https:",
21
- host
22
- };
23
17
  }
18
+ return {
19
+ ws_protocol: protocol === "https:" ? "wss" : "ws",
20
+ http_protocol: protocol as "http:" | "https:",
21
+ host
22
+ };
24
23
  }
25
24
 
26
25
  // default to secure if no protocol is provided
@@ -87,7 +86,9 @@ export async function process_endpoint(
87
86
  };
88
87
  }
89
88
 
90
- export function map_names_to_ids(fns: Config["dependencies"]) {
89
+ export function map_names_to_ids(
90
+ fns: Config["dependencies"]
91
+ ): Record<string, number> {
91
92
  let apis: Record<string, number> = {};
92
93
 
93
94
  fns.forEach(({ api_name }, i) => {
@@ -99,7 +100,7 @@ export function map_names_to_ids(fns: Config["dependencies"]) {
99
100
 
100
101
  const RE_DISABLED_DISCUSSION =
101
102
  /^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/;
102
- export async function discussions_enabled(space_id: string) {
103
+ export async function discussions_enabled(space_id: string): Promise<boolean> {
103
104
  try {
104
105
  const r = await fetch(
105
106
  `https://huggingface.co/api/spaces/${space_id}/discussions`,
@@ -110,7 +111,7 @@ export async function discussions_enabled(space_id: string) {
110
111
  const error = r.headers.get("x-error-message");
111
112
 
112
113
  if (error && RE_DISABLED_DISCUSSION.test(error)) return false;
113
- else return true;
114
+ return true;
114
115
  } catch (e) {
115
116
  return false;
116
117
  }
@@ -119,7 +120,7 @@ export async function discussions_enabled(space_id: string) {
119
120
  export async function get_space_hardware(
120
121
  space_id: string,
121
122
  token: `hf_${string}`
122
- ) {
123
+ ): Promise<(typeof hardware_types)[number]> {
123
124
  const headers: { Authorization?: string } = {};
124
125
  if (token) {
125
126
  headers.Authorization = `Bearer ${token}`;
@@ -144,9 +145,9 @@ export async function get_space_hardware(
144
145
 
145
146
  export async function set_space_hardware(
146
147
  space_id: string,
147
- new_hardware: typeof hardware_types[number],
148
+ new_hardware: (typeof hardware_types)[number],
148
149
  token: `hf_${string}`
149
- ) {
150
+ ): Promise<(typeof hardware_types)[number]> {
150
151
  const headers: { Authorization?: string } = {};
151
152
  if (token) {
152
153
  headers.Authorization = `Bearer ${token}`;
@@ -175,7 +176,7 @@ export async function set_space_timeout(
175
176
  space_id: string,
176
177
  timeout: number,
177
178
  token: `hf_${string}`
178
- ) {
179
+ ): Promise<number> {
179
180
  const headers: { Authorization?: string } = {};
180
181
  if (token) {
181
182
  headers.Authorization = `Bearer ${token}`;