Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3712 js client #3899

Merged
merged 65 commits into from May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
ed29ec1
update api
pngwn Apr 13, 2023
7cbff03
private space support
pngwn Apr 17, 2023
0fa2fd3
cleanup
pngwn Apr 17, 2023
d8301df
add info to js client
pngwn Apr 17, 2023
42ee708
duplication
pngwn Apr 18, 2023
d153890
cleanup
pngwn Apr 18, 2023
8405d71
readme
pngwn Apr 20, 2023
8236b00
update readme
pngwn Apr 24, 2023
a278e99
clean up readme
pngwn Apr 24, 2023
8f65e27
Merge branch 'main' into 3712-js-client
pngwn Apr 24, 2023
97dfd1a
fix formatting
pngwn Apr 24, 2023
888807b
changelog
pngwn Apr 24, 2023
781f77a
fix ts
pngwn Apr 24, 2023
f3104ef
fix Buffer check in node
pngwn Apr 24, 2023
2e780f0
Update Hugging Face style
pngwn Apr 25, 2023
bf174b8
guide start
pngwn Apr 25, 2023
2cd8f63
Merge remote-tracking branch 'origin/3712-js-client' into 3712-js-client
pngwn Apr 25, 2023
dcf3aac
Merge branch 'main' into 3712-js-client
pngwn Apr 25, 2023
69e5deb
Add js client documentation to docs (#3925)
aliabd Apr 25, 2023
386705d
guide
pngwn Apr 26, 2023
6d3f09d
fix api + cancels
pngwn Apr 26, 2023
e53be52
changes?
pngwn Apr 27, 2023
271e5b4
changes
pngwn May 3, 2023
1e77bb7
updates
pngwn May 5, 2023
92d77bc
Merge remote-tracking branch 'origin/3712-js-client' into 3712-js-client
pngwn May 5, 2023
90677d1
format
pngwn May 5, 2023
b46f1ea
changes
pngwn May 5, 2023
33cda9d
fix everything
pngwn May 5, 2023
4008d98
tweak api info
pngwn May 5, 2023
b651a74
Merge branch 'main' into 3712-js-client
pngwn May 6, 2023
101f3cd
allow info endpoint to fail gracefully
pngwn May 6, 2023
80ba866
tweaks
pngwn May 6, 2023
4b54747
handle older version
pngwn May 6, 2023
a5a79b4
things
pngwn May 6, 2023
931a817
update status in app
pngwn May 6, 2023
0fefd21
fix things
pngwn May 9, 2023
e76434a
Merge branch 'main' into 3712-js-client
pngwn May 9, 2023
22467c5
fix status
pngwn May 9, 2023
04df3c3
update guide
pngwn May 9, 2023
f6a2f12
update readme
pngwn May 9, 2023
0a96be1
Merge branch 'main' into 3712-js-client
pngwn May 9, 2023
431aa5f
changes
pngwn May 10, 2023
a9cd96e
support hardware and timeout for duplicate
pngwn May 11, 2023
c21f0e0
be more defensive when closing ws connections via cancel
pngwn May 11, 2023
2b7e2ad
add destroy method, call when prediction is over or cancelled
pngwn May 11, 2023
8643675
tweaks
pngwn May 11, 2023
5be28d0
upadte docs
pngwn May 11, 2023
e2cee78
fix docs, better hardware support
pngwn May 11, 2023
423fb87
Merge branch 'main' into 3712-js-client
pngwn May 11, 2023
ba7174e
tweak
pngwn May 11, 2023
349e8ff
formatting
pngwn May 11, 2023
6abfdf1
formatting
pngwn May 11, 2023
2db0d84
ts
pngwn May 11, 2023
9e34b30
Merge branch 'main' into 3712-js-client
abidlabs May 11, 2023
e20c012
Merge branch 'main' into 3712-js-client
abidlabs May 11, 2023
70f6c8b
Merge branch 'main' into 3712-js-client
pngwn May 11, 2023
6aea68f
fix changelog
pngwn May 11, 2023
32662ff
update version for space fetcher
pngwn May 11, 2023
f906a33
fix
pngwn May 12, 2023
cd38584
fix tests
pngwn May 12, 2023
4a76ae8
update guide
pngwn May 12, 2023
220a24d
format readme
pngwn May 12, 2023
edf2077
format
pngwn May 12, 2023
dca66ad
Merge branch 'main' into 3712-js-client
pngwn May 12, 2023
c1ff015
Add JS to use via api (#3927)
aliabd May 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/.prettierrc.json
Expand Up @@ -3,5 +3,5 @@
"singleQuote": false,
"trailingComma": "none",
"printWidth": 80,
"pluginSearchDirs": ["."]
"pluginSearchDirs": [".."]
}
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -58,4 +58,5 @@ gradio/frpc_*
# js
node_modules
public/build/
test-results
test-results
client/js/test.js
2 changes: 1 addition & 1 deletion .vscode/settings.json
Expand Up @@ -12,5 +12,5 @@
"cssvar.extensions": ["js", "css", "html", "jsx", "tsx", "svelte"],
"python.analysis.extraPaths": ["./gradio/themes/utils"],
"prettier.configPath": ".config/.prettierrc.json",
"prettier.ignorePath": "./config/.prettierignore"
"prettier.ignorePath": ".config/.prettierignore"
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ No changes to highlight.
- Fix issue where requesting for a non-existing file would trigger a 500 error by [@micky2be](https://github.com/micky2be) in `[PR 3895](https://github.com/gradio-app/gradio/pull/3895)`.
- Fix bugs with abspath about symlinks, and unresolvable path on Windows by [@micky2be](https://github.com/micky2be) in `[PR 3895](https://github.com/gradio-app/gradio/pull/3895)`.
- Fixes type in client `Status` enum by [@10zinten](https://github.com/10zinten) in [PR 3931](https://github.com/gradio-app/gradio/pull/3931)
- Update the js client by [@pngwn](https://github.com/pngwn) in [PR 3899](https://github.com/gradio-app/gradio/pull/3899)

## Documentation Changes:

Expand Down
275 changes: 255 additions & 20 deletions client/js/README.md
@@ -1,31 +1,170 @@
# `@gradio/client`
# JavaScript Gradio client

A javascript client to call Gradio APIs.
A javascript (and typescript) client to call Gradio APIs.

**install it**
## Installation

The Gradio JavaScript client is available on npm as `@gradio/client`. You can install it as below:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we've published, it would be really nice to have the CDN link here as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I'll figure that out as soon as possible.

```sh
pnpm add @gradio/client
# or npm i @gradio/client
pngwn marked this conversation as resolved.
Show resolved Hide resolved
```

## Usage

The JavaScript Gradio Client exposes two named imports, `client` and `duplicate`.

### `client`

The client function connects to the API of a hosted Gradio space and returns an object that allows you to make calls to that API.

The simplest example looks like this:

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
const result = await app.predict("/predict");
```

This function accepts two arguments: `source` and `options`:

#### `source`

This is the url or name of the gradio app whose API you wish to connect to. This parameter is required and should always be a string. For example:

```ts
client("user/space-name");
```

#### `options`

The options object can optionally be passed a second parameter. This object has two properties, `hf_token` and `status_callback`.

##### `hf_token`

This should be a hugging face personal access token and is required if you wish to make calls to a private gradio api. This option is optional and should be a string starting with `"hf_"`.

pngwn marked this conversation as resolved.
Show resolved Hide resolved
Example:

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name", { hf_token: "hf_..." });
```

##### `status_callback`

This should be a function which will notify your of the status of a space if it is not running. If the gradio API you are connecting to is awake and running or is not hosted on hugginface space then this function will do nothing.

pngwn marked this conversation as resolved.
Show resolved Hide resolved
**Additional context**

Applications hosted on Hugginface spaces can be in a number of different states. As spaces are a GitOps tool and will rebuild when new changes are pushed to the repository, they have various building, running and error states. If a space is not 'running' then the function passed as the `status_callback` will notify you of the current state of the space and the status of the space as it changes. Spaces that are building or sleeping can take longer than usual to respond, so you can use this information to give users feedback about the progress of their action.

pngwn marked this conversation as resolved.
Show resolved Hide resolved
```ts
import { client, type SpaceStatus } from "@gradio/client";

const app = await client("user/space-name", {
// The space_status parameter does not need to be manually annotated, this is just for illustration.
space_status: (space_status: SpaceStatus) => console.log(space_status)
});
```

```ts
interface SpaceStatusNormal {
status: "sleeping" | "running" | "building" | "error" | "stopped";
detail:
| "SLEEPING"
| "RUNNING"
| "RUNNING_BUILDING"
| "BUILDING"
| "NOT_FOUND";
load_status: "pending" | "error" | "complete" | "generating";
message: string;
}

interface SpaceStatusError {
status: "space_error";
detail: "NO_APP_FILE" | "CONFIG_ERROR" | "BUILD_ERROR" | "RUNTIME_ERROR";
load_status: "error";
message: string;
discussions_enabled: boolean;

type SpaceStatus = SpaceStatusNormal | SpaceStatusError;
```

The gradio client returns an object with a number of methods and properties:

#### `predict`

The `predict` method allows you to call an api endpoint and get a prediction result:

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
const result = await app.predict("/predict");
```

`predict` accepts two parameters, `endpoint` and `payload`. It returns a promise that resolves to the prediction result.

##### `endpoint`

This is the endpoint for an api request and is required. The default endpoint for a `gradio.Interface` is `"/predict"`. Explicitly named endpoints have a custom name. The endpoint names can be found on the "View API" page of a space.

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
const result = await app.predict("/predict");
```

##### `payload`

The `payload` argument is generally optional but this depends on the API itself. If the API endpoint depends on values being passed in then it is required for the API request to succeed. The data that should be passed in is detailed on the "View API" page of a space.

pngwn marked this conversation as resolved.
Show resolved Hide resolved
```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
const result = await app.predict("/predict", [1, "Hello", "friends"]);
```

**usage**
<!-- Types for the different API inputs are provided and the example snippets on the "View API" will provide strong typing for the payload. These types need to be explicitly annotated as there currently no mechanism to automatically infer them due to the varied and dynamic nature of API endpoints. -->

#### `submit`

The `submit` method provides a more flexible way to call an API endpoint, providing you with status updates about the current progress of the prediction as well as supporting more complex endpoint types.

```ts
import { client } from "@gradio/client";

const app = client();
const app = await client("user/space-name");
const submission = app.submit("/predict", payload);
```

The `submit` method accepts the same [`endpoint`](#endpoint) and [`payload`](#payload) arguments as `predict`.

The `submit` method does not return a promise and should not be awaited, instead it returns an object with a `on`, `off`, and `cancel` methods.

##### `on`

The `on` method allows you to subscribe to events related to the submitted API request. There are two types of event that can be subscribed to: `"data"` updates and `"status"` updates.

`"data"` updates are issued when the API computes a value, the callback provided as the second argument will be called when such a value is sent to the client. The shape of the data depends on the way the API itself is constructed. This event may fire more than once if that endpoint supports emmitting new values over time.

const prediction = app.predict(endpoint, payload);
<!-- The type annotations provided by the "View API" page will ensure that the data payload is strongly typed. -->

pngwn marked this conversation as resolved.
Show resolved Hide resolved
// listen for predictions
prediction.on("data", (event: { data: Array<unknown>; type: "data" }) => {});
`"status` updates are issued when the status of a request changes. This information allows you to offer feedback to users when the queue position of the request changes, or when the request changes from queued to processing.

// listen for status updates
prediction.on("status", (event: { data: Status; type: "data" }) => {});
The status payload look like this:

```ts
interface Status {
pngwn marked this conversation as resolved.
Show resolved Hide resolved
queue: boolean;
status: "pending" | "error" | "complete" | "generating";
pngwn marked this conversation as resolved.
Show resolved Hide resolved
size: number;
size?: number;
position?: number;
eta?: number;
message?: string;
Expand All @@ -37,16 +176,112 @@ interface Status {
desc: string | null;
}>;
}
```

Usage of these subscribe callback looks like this:

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
const submission = app
.submit("/predict", payload)
.on("data", (data) => console.log(data))
.on("status", (status: Status) => console.log(status));
```

##### `off`

The `off` method unsubscribes from a specific event of the submitted job and works similarly to `document.removeEventListener`; both the event name and the original callback must be passed in to successfully unsubscribe:

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
const handle_data = (data) => console.log(data);

const submission = app.submit("/predict", payload).on("data", handle_data);

// later
submission.off("/predict", handle_data);
```

##### `cancel`

Certain types of gradio function can run repeatedly and in some cases indefinitely. the `cancel` method will stop such an endpoints and prevent the API from issuing additional updates.

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
const submission = app
.submit("/predict", payload)
.on("data", (data) => console.log(data));

// later

// stop listening
prediction.off("data");
submission.cancel();
```

#### `info`

The `info` method provides details about the API you are connected too. It returns a JavaScript object of all named endpoints, unnamed endpoints and what values they accept and return. This method does not accept arguments.

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
const api_info = await app.info();

console.log(api_info);
```

#### `config`

The `config` property contains the configuration for the gradio application you are connected to. This object may contain useful meta information about the application.

```ts
import { client } from "@gradio/client";

const app = await client("user/space-name");
console.log(app.config);
```

// cancel a prediction if it is a generator
prediction.cancel();
### `duplicate`

The duplicate function will attempt to duplicate the space that is referenced and return an instance of `client` connected to that space. If the space has already been duplicated then it will not create a new duplicate and will instead connect to the existing duplicated space. The huggingface token that is passed in will dictate the user under which the space is created.

`duplicate` accepts the same arguments as `client` with the addition of a `private` options property dictating whether the duplicated space should be private or public. A huggingface token is required for duplication to work.

```ts
import { duplicate } from "@gradio/client";

const app = await duplicate("user/space-name", {
hf_token: "hf_..."
});
```

This function accepts two arguments: `source` and `options`:

#### `source`

The space to duplicate and connect to. [See `client`'s `source` parameter](#source).

#### `options`

Accepts all options that `client` accepts, except `hf_token` is required. [See `client`'s `options` parameter](#source).

`duplicate` also accepts one additional `options` property.

##### `private`

This is an optional parameter specific to `duplicate` and will determine whether the space should be public or private. Spaces duplicated via the `duplicate` method are public by default.

```ts
import { duplicate } from "@gradio/client";

// chainable
const prediction_two = app
.predict(endpoint, payload)
.on("data", data_callback)
.on("status", status_callback);
const app = await duplicate("user/space-name", {
hf_token: "hf_...",
private: true
});
```