@gradio/client 1.19.0 → 2.0.0-dev.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.
@@ -12,13 +12,11 @@ import type {
12
12
  } from "../types";
13
13
 
14
14
  import { skip_queue, post_message, handle_payload } from "../helpers/data";
15
- import { resolve_root } from "../helpers/init_helpers";
16
15
  import {
17
16
  handle_message,
18
17
  map_data_to_params,
19
18
  process_endpoint
20
19
  } from "../helpers/api_info";
21
- import semiver from "semiver";
22
20
  import {
23
21
  BROKEN_CONNECTION_MSG,
24
22
  QUEUE_FULL_MSG,
@@ -39,7 +37,7 @@ export function submit(
39
37
  all_events?: boolean
40
38
  ): SubmitIterable<GradioEvent> {
41
39
  try {
42
- const { hf_token } = this.options;
40
+ const { token } = this.options;
43
41
  const {
44
42
  fetch,
45
43
  app_reference,
@@ -71,9 +69,11 @@ export function submit(
71
69
 
72
70
  let resolved_data = map_data_to_params(data, endpoint_info);
73
71
 
74
- let websocket: WebSocket;
75
72
  let stream: EventSource | null;
76
73
  let protocol = config.protocol ?? "ws";
74
+ if (protocol === "ws") {
75
+ throw new Error("WebSocket protocol is not supported in this version");
76
+ }
77
77
  let event_id_final = "";
78
78
  let event_id_cb: () => string = () => event_id_final;
79
79
 
@@ -106,19 +106,8 @@ export function submit(
106
106
  async function cancel(): Promise<void> {
107
107
  let reset_request = {};
108
108
  let cancel_request = {};
109
- if (protocol === "ws") {
110
- if (websocket && websocket.readyState === 0) {
111
- websocket.addEventListener("open", () => {
112
- websocket.close();
113
- });
114
- } else {
115
- websocket.close();
116
- }
117
- reset_request = { fn_index, session_hash };
118
- } else {
119
- reset_request = { event_id };
120
- cancel_request = { event_id, session_hash, fn_index };
121
- }
109
+ reset_request = { event_id };
110
+ cancel_request = { event_id, session_hash, fn_index };
122
111
 
123
112
  try {
124
113
  if (!config) {
@@ -262,137 +251,6 @@ export function submit(
262
251
  time: new Date()
263
252
  });
264
253
  });
265
- } else if (protocol == "ws") {
266
- const { ws_protocol, host } = await process_endpoint(
267
- app_reference,
268
- hf_token
269
- );
270
-
271
- fire_event({
272
- type: "status",
273
- stage: "pending",
274
- queue: true,
275
- endpoint: _endpoint,
276
- fn_index,
277
- time: new Date()
278
- });
279
-
280
- let url = new URL(
281
- `${ws_protocol}://${resolve_root(
282
- host,
283
- config.root as string,
284
- true
285
- )}/queue/join${url_params ? "?" + url_params : ""}`
286
- );
287
-
288
- if (this.jwt) {
289
- url.searchParams.set("__sign", this.jwt);
290
- }
291
-
292
- websocket = new WebSocket(url);
293
-
294
- websocket.onclose = (evt) => {
295
- if (!evt.wasClean) {
296
- fire_event({
297
- type: "status",
298
- stage: "error",
299
- broken: true,
300
- message: BROKEN_CONNECTION_MSG,
301
- queue: true,
302
- endpoint: _endpoint,
303
- fn_index,
304
- time: new Date()
305
- });
306
- }
307
- };
308
-
309
- websocket.onmessage = function (event) {
310
- const _data = JSON.parse(event.data);
311
- const { type, status, data } = handle_message(
312
- _data,
313
- last_status[fn_index]
314
- );
315
-
316
- if (type === "update" && status && !complete) {
317
- // call 'status' listeners
318
- fire_event({
319
- type: "status",
320
- endpoint: _endpoint,
321
- fn_index,
322
- time: new Date(),
323
- ...status
324
- });
325
- if (status.stage === "error") {
326
- websocket.close();
327
- }
328
- } else if (type === "hash") {
329
- websocket.send(JSON.stringify({ fn_index, session_hash }));
330
- return;
331
- } else if (type === "data") {
332
- websocket.send(JSON.stringify({ ...payload, session_hash }));
333
- } else if (type === "complete") {
334
- complete = status;
335
- } else if (type === "log") {
336
- fire_event({
337
- type: "log",
338
- title: data.title,
339
- log: data.log,
340
- level: data.level,
341
- endpoint: _endpoint,
342
- duration: data.duration,
343
- visible: data.visible,
344
- fn_index
345
- });
346
- } else if (type === "generating") {
347
- fire_event({
348
- type: "status",
349
- time: new Date(),
350
- ...status,
351
- stage: status?.stage!,
352
- queue: true,
353
- endpoint: _endpoint,
354
- fn_index
355
- });
356
- }
357
- if (data) {
358
- fire_event({
359
- type: "data",
360
- time: new Date(),
361
- data: handle_payload(
362
- data.data,
363
- dependency,
364
- config.components,
365
- "output",
366
- options.with_null_state
367
- ),
368
- endpoint: _endpoint,
369
- fn_index,
370
- event_data,
371
- trigger_id
372
- });
373
-
374
- if (complete) {
375
- fire_event({
376
- type: "status",
377
- time: new Date(),
378
- ...complete,
379
- stage: status?.stage!,
380
- queue: true,
381
- endpoint: _endpoint,
382
- fn_index
383
- });
384
- websocket.close();
385
- }
386
- }
387
- };
388
-
389
- // different ws contract for gradio versions older than 3.6.0
390
- //@ts-ignore
391
- if (semiver(config.version || "2.0.0", "3.6") < 0) {
392
- addEventListener("open", () =>
393
- websocket.send(JSON.stringify({ hash: session_hash }))
394
- );
395
- }
396
254
  } else if (protocol == "sse") {
397
255
  fire_event({
398
256
  type: "status",
@@ -11,8 +11,8 @@ export async function upload_files(
11
11
  const headers: {
12
12
  Authorization?: string;
13
13
  } = {};
14
- if (this?.options?.hf_token) {
15
- headers.Authorization = `Bearer ${this.options.hf_token}`;
14
+ if (this?.options?.token) {
15
+ headers.Authorization = `Bearer ${this.options.token}`;
16
16
  }
17
17
 
18
18
  const chunkSize = 1000;
@@ -1,5 +1,4 @@
1
1
  import type { ApiInfo, ApiData } from "../types";
2
- import semiver from "semiver";
3
2
  import { API_INFO_URL, BROKEN_CONNECTION_MSG } from "../constants";
4
3
  import { Client } from "../client";
5
4
  import { SPACE_FETCHER_URL } from "../constants";
@@ -8,7 +7,7 @@ import { join_urls, transform_api_info } from "../helpers/api_info";
8
7
  export async function view_api(this: Client): Promise<any> {
9
8
  if (this.api_info) return this.api_info;
10
9
 
11
- const { hf_token } = this.options;
10
+ const { token } = this.options;
12
11
  const { config } = this;
13
12
 
14
13
  const headers: {
@@ -16,8 +15,8 @@ export async function view_api(this: Client): Promise<any> {
16
15
  "Content-Type": "application/json";
17
16
  } = { "Content-Type": "application/json" };
18
17
 
19
- if (hf_token) {
20
- headers.Authorization = `Bearer ${hf_token}`;
18
+ if (token) {
19
+ headers.Authorization = `Bearer ${token}`;
21
20
  }
22
21
 
23
22
  if (!config) {
@@ -30,24 +29,11 @@ export async function view_api(this: Client): Promise<any> {
30
29
  if (typeof window !== "undefined" && window.gradio_api_info) {
31
30
  api_info = window.gradio_api_info;
32
31
  } else {
33
- if (semiver(config?.version || "2.0.0", "3.30") < 0) {
34
- response = await this.fetch(SPACE_FETCHER_URL, {
35
- method: "POST",
36
- body: JSON.stringify({
37
- serialize: false,
38
- config: JSON.stringify(config)
39
- }),
40
- headers,
41
- credentials: "include"
42
- });
43
- } else {
44
- const url = join_urls(config.root, this.api_prefix, API_INFO_URL);
45
- response = await this.fetch(url, {
46
- headers,
47
- credentials: "include"
48
- });
49
- }
50
-
32
+ const url = join_urls(config.root, this.api_prefix, API_INFO_URL);
33
+ response = await this.fetch(url, {
34
+ headers,
35
+ credentials: "include"
36
+ });
51
37
  if (!response.ok) {
52
38
  throw new Error(BROKEN_CONNECTION_MSG);
53
39
  }