3
import { onMount, createEventDispatcher } from "svelte";
4
import type { ComponentMeta, Dependency } from "../types";
5
import { post_data } from "@gradio/client";
6
import NoApi from "./NoApi.svelte";
7
import type { client } from "@gradio/client";
9
import { represent_value } from "./utils";
11
import ApiBanner from "./ApiBanner.svelte";
12
import ResponseObject from "./ResponseObject.svelte";
13
import InstallSnippet from "./InstallSnippet.svelte";
14
import CodeSnippets from "./CodeSnippets.svelte";
16
import python from "./img/python.svg";
17
import javascript from "./img/javascript.svg";
19
export let instance_map: {
20
[id: number]: ComponentMeta;
22
export let dependencies: Dependency[];
23
export let root: string;
24
export let app: Awaited<ReturnType<typeof client>>;
25
export let space_id: string | null;
28
"https://www.gradio.app/guides/getting-started-with-the-js-client";
30
"https://www.gradio.app/guides/getting-started-with-the-python-client";
31
const spaces_docs_suffix = "#connecting-to-a-hugging-face-space";
33
let api_count = dependencies.filter(
34
(dependency) => dependency.show_api
38
root = location.protocol + "//" + location.host + location.pathname;
40
if (!root.endsWith("/")) {
44
let current_language: "python" | "javascript" = "python";
48
["javascript", javascript]
51
let is_running = false;
53
let dependency_inputs = dependencies.map((dependency) =>
54
dependency.inputs.map((_id) => {
55
let default_data = instance_map[_id].documentation?.example_data;
56
if (default_data === undefined) {
58
} else if (typeof default_data === "object") {
59
default_data = JSON.stringify(default_data);
65
let dependency_outputs: any[][] = dependencies.map(
66
(dependency) => new Array(dependency.outputs.length)
69
let dependency_failures: boolean[][] = dependencies.map((dependency) =>
70
new Array(dependency.inputs.length).fill(false)
73
async function get_info(): Promise<{
75
unnamed_endpoints: any;
77
let response = await fetch(root + "info");
78
let data = await response.json();
81
async function get_js_info(): Promise<Record<string, any>> {
82
let js_api_info = await app.view_api();
88
unnamed_endpoints: any;
91
let js_info: Record<string, any>;
93
get_info().then((data) => {
97
get_js_info().then((js_api_info) => {
98
js_info = js_api_info;
101
async function run(index: number): Promise<void> {
103
let dependency = dependencies[index];
104
let attempted_component_index = 0;
106
var inputs = dependency_inputs[index].map((input_val, i) => {
107
attempted_component_index = i;
108
let component = instance_map[dependency.inputs[i]];
110
input_val = represent_value(
112
component.documentation?.type?.input_payload ||
113
component.documentation?.type?.payload
115
dependency_failures[index][attempted_component_index] = false;
119
dependency_failures[index][attempted_component_index] = true;
123
let [response, status_code] = await post_data(
124
`${root}run/${dependency.api_name}`,
130
if (status_code == 200) {
131
dependency_outputs[index] = response.data.map(
132
(output_val: any, i: number) => {
133
let component = instance_map[dependency.outputs[i]];
135
return represent_value(
137
component.documentation?.type?.response_object ||
138
component.documentation?.type?.payload,
144
dependency_failures[index] = new Array(
145
dependency_failures[index].length
151
document.body.style.overflow = "hidden";
152
if ("parentIFrame" in window) {
153
window.parentIFrame?.scrollTo(0, 0);
156
document.body.style.overflow = "auto";
163
<div class="banner-wrap">
164
<ApiBanner on:close root={space_id || root} {api_count} />
166
<div class="docs-wrap">
167
<div class="client-doc">
169
Use the <code class="library">gradio_client</code>
170
Python library (<a href={py_docs} target="_blank">docs</a>) or the
171
<code class="library">@gradio/client</code>
172
Javascript package (<a href={js_docs} target="_blank">docs</a>) to
173
query the app via API.
176
<div class="endpoint">
177
<div class="snippets">
178
{#each langs as [language, img]}
181
{current_language === language ? 'current-lang' : 'inactive-lang'}"
182
on:click={() => (current_language = language)}
184
<img src={img} alt="" />
191
1. Install the client if you don't already have it installed.
194
<InstallSnippet {current_language} />
197
2. Find the API endpoint below corresponding to your desired function
198
in the app. Copy the code snippet, replacing the placeholder values
199
with your own input data.
200
{#if space_id}If this is a private Space, you may need to pass your
201
Hugging Face token as well (<a
202
href={(current_language == "python" ? py_docs : js_docs) +
205
target="_blank">read more</a
206
>).{/if} Run the code, that's it!
209
{#each dependencies as dependency, dependency_index}
210
{#if dependency.show_api}
211
<div class="endpoint-container">
214
endpoint_parameters={info.named_endpoints[
215
"/" + dependency.api_name
217
js_parameters={js_info.named_endpoints[
218
"/" + dependency.api_name
223
root={space_id || root}
224
{dependency_failures}
234
endpoint_returns={info.named_endpoints[
235
"/" + dependency.api_name
237
js_returns={js_info.named_endpoints["/" + dependency.api_name]
248
<NoApi {root} on:close />
255
border-bottom: 1px solid var(--border-color-primary);
256
padding: var(--size-4) var(--size-6);
257
font-size: var(--text-md);
260
@media (--screen-md) {
262
font-size: var(--text-xl);
268
flex-direction: column;
269
gap: var(--spacing-xxl);
273
border-radius: var(--radius-md);
274
background: var(--background-fill-primary);
275
padding: var(--size-6);
276
padding-top: var(--size-1);
277
font-size: var(--text-md);
281
padding-top: var(--size-6);
282
padding-right: var(--size-6);
283
padding-left: var(--size-6);
284
font-size: var(--text-md);
288
border: 1px solid var(--border-color-accent);
289
border-radius: var(--radius-sm);
290
background: var(--color-accent-soft);
291
padding: var(--size-1);
292
color: var(--color-accent);
293
font-size: var(--text-md);
299
margin-bottom: var(--size-4);
303
margin-left: var(--size-2);
309
border: 1px solid var(--border-color-primary);
311
border-radius: var(--radius-md);
312
padding: var(--size-1) var(--size-1-5);
313
color: var(--body-text-color-subdued);
314
color: var(--body-text-color);
317
text-transform: capitalize;
321
border: 1px solid var(--body-text-color-subdued);
322
color: var(--body-text-color);
327
color: var(--body-text-color-subdued);
330
.inactive-lang:hover,
331
.inactive-lang:focus {
332
box-shadow: var(--shadow-drop);
333
color: var(--body-text-color);
337
margin-right: var(--size-1-5);
338
width: var(--size-3);
342
margin-top: var(--size-6);
343
font-size: var(--text-xl);
346
.endpoint-container {
347
margin-top: var(--size-3);
348
margin-bottom: var(--size-3);
349
border: 1px solid var(--border-color-primary);
350
border-radius: var(--radius-xl);
351
padding: var(--size-3);
356
text-decoration: underline;
361
font-size: var(--text-lg);