opensi·si

Tags: CLIENT, API, SPACES

Getting Started with the Gradio JavaScript Client

Tags: CLIENT, API, SPACES

The Gradio JavaScript Client makes it very easy to use any Gradio app as an API. As an example, consider this Hugging Face Space that transcribes audio files that are recorded from the microphone.

Using the @gradio/client library, we can easily use the Gradio as an API to transcribe audio files programmatically.

Here's the entire code to do it:

import { Client, handle_file } from "@gradio/client";

const response = await fetch(
	"https://github.com/audio-samples/audio-samples.github.io/raw/master/samples/wav/ted_speakers/SalmanKhan/sample-1.wav"
);
const audio_file = await response.blob();

const app = await Client.connect("abidlabs/whisper");
const transcription = await app.predict("/predict", [handle_file(audio_file)]);

console.log(transcription.data);
// [ "I said the same phrase 30 times." ]

The Gradio Client works with any hosted Gradio app, whether it be an image generator, a text summarizer, a stateful chatbot, a tax calculator, or anything else! The Gradio Client is mostly used with apps hosted on Hugging Face Spaces, but your app can be hosted anywhere, such as your own server.

Prequisites: To use the Gradio client, you do not need to know the gradio library in great detail. However, it is helpful to have general familiarity with Gradio's concepts of input and output components.

Installation via npm

Install the @gradio/client package to interact with Gradio APIs using Node.js version >=18.0.0 or in browser-based projects. Use npm or any compatible package manager:

npm i @gradio/client

This command adds @gradio/client to your project dependencies, allowing you to import it in your JavaScript or TypeScript files.

Installation via CDN

For quick addition to your web project, you can use the jsDelivr CDN to load the latest version of @gradio/client directly into your HTML:

Be sure to add this to the <head> of your HTML. This will install the latest version but we advise hardcoding the version in production. You can find all available versions here. This approach is ideal for experimental or prototying purposes, though has some limitations. A complete example would look like this:

<!DOCTYPE html>
<html lang="en">
<head>

</head>
</html>

Connecting to a running Gradio App

Start by connecting instantiating a client instance and connecting it to a Gradio app that is running on Hugging Face Spaces or generally anywhere on the web.

Connecting to a Hugging Face Space

import { Client } from "@gradio/client";

const app = await Client.connect("abidlabs/en2fr"); // a Space that translates from English to French

You can also connect to private Spaces by passing in your HF token with the token property of the options parameter. You can get your HF token here:

import { Client } from "@gradio/client";

const app = await Client.connect("abidlabs/my-private-space", { token: "hf_..." })

Duplicating a Space for private use

While you can use any public Space as an API, you may get rate limited by Hugging Face if you make too many requests. For unlimited usage of a Space, simply duplicate the Space to create a private Space, and then use it to make as many requests as you'd like! You'll need to pass in your Hugging Face token).

Client.duplicate is almost identical to Client.connect, the only difference is under the hood:

import { Client, handle_file } from "@gradio/client";

const response = await fetch(
	"https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3"
);
const audio_file = await response.blob();

const app = await Client.duplicate("abidlabs/whisper", { token: "hf_..." });
const transcription = await app.predict("/predict", [handle_file(audio_file)]);

If you have previously duplicated a Space, re-running Client.duplicate will not create a new Space. Instead, the client will attach to the previously-created Space. So it is safe to re-run the Client.duplicate method multiple times with the same space.

Note: if the original Space uses GPUs, your private Space will as well, and your Hugging Face account will get billed based on the price of the GPU. To minimize charges, your Space will automatically go to sleep after 5 minutes of inactivity. You can also set the hardware using the hardware and timeout properties of duplicate's options object like this:

import { Client } from "@gradio/client";

const app = await Client.duplicate("abidlabs/whisper", {
	token: "hf_...",
	timeout: 60,
	hardware: "a10g-small"
});

Connecting a general Gradio app

If your app is running somewhere else, just provide the full URL instead, including the "http://" or "https://". Here's an example of making predictions to a Gradio app that is running on a share URL:

import { Client } from "@gradio/client";

const app = Client.connect("https://bec81a83-5b5c-471e.gradio.live");

Connecting to a Gradio app with auth

If the Gradio application you are connecting to requires a username and password, then provide them as a tuple to the auth argument of the Client class:

import { Client } from "@gradio/client";

Client.connect(
  space_name,
  { auth: [username, password] }
)