@gradio/core 0.0.2
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/CHANGELOG.md +32 -0
- package/LICENSE +201 -0
- package/blocks.ts +1 -0
- package/index.ts +4 -0
- package/login.ts +1 -0
- package/package.json +83 -0
- package/public/favicon.png +0 -0
- package/public/static/img/Bunny.obj +7474 -0
- package/public/static/img/Duck.glb +0 -0
- package/public/static/img/api-logo.svg +4 -0
- package/public/static/img/camera.svg +1 -0
- package/public/static/img/clear.svg +67 -0
- package/public/static/img/edit.svg +39 -0
- package/public/static/img/javascript.svg +16 -0
- package/public/static/img/logo.svg +19 -0
- package/public/static/img/logo_error.svg +134 -0
- package/public/static/img/python.svg +20 -0
- package/public/static/img/undo-solid.svg +1 -0
- package/src/Blocks.svelte +792 -0
- package/src/Embed.svelte +197 -0
- package/src/Login.stories.svelte +33 -0
- package/src/Login.svelte +111 -0
- package/src/MountComponents.svelte +30 -0
- package/src/Render.svelte +103 -0
- package/src/RenderComponent.svelte +67 -0
- package/src/api_docs/ApiBanner.svelte +118 -0
- package/src/api_docs/ApiDocs.svelte +418 -0
- package/src/api_docs/ApiRecorder.svelte +75 -0
- package/src/api_docs/CodeSnippet.svelte +198 -0
- package/src/api_docs/CopyButton.svelte +17 -0
- package/src/api_docs/EndpointDetail.svelte +37 -0
- package/src/api_docs/InputPayload.svelte +155 -0
- package/src/api_docs/InstallSnippet.svelte +59 -0
- package/src/api_docs/NoApi.svelte +74 -0
- package/src/api_docs/ParametersSnippet.svelte +106 -0
- package/src/api_docs/RecordingSnippet.svelte +224 -0
- package/src/api_docs/ResponseSnippet.svelte +97 -0
- package/src/api_docs/TryButton.svelte +19 -0
- package/src/api_docs/img/api-logo.svg +4 -0
- package/src/api_docs/img/bash.svg +8 -0
- package/src/api_docs/img/clear.svelte +19 -0
- package/src/api_docs/img/javascript.svg +16 -0
- package/src/api_docs/img/python.svg +20 -0
- package/src/api_docs/index.ts +2 -0
- package/src/api_docs/utils.ts +143 -0
- package/src/css.ts +116 -0
- package/src/gradio_helper.ts +7 -0
- package/src/i18n.test.ts +27 -0
- package/src/i18n.ts +36 -0
- package/src/images/lightning.svg +2 -0
- package/src/images/logo.svg +19 -0
- package/src/images/play.svg +2 -0
- package/src/images/spaces.svg +7 -0
- package/src/init.test.ts +521 -0
- package/src/init.ts +590 -0
- package/src/lang/BCP47_codes.js +58 -0
- package/src/lang/README.md +11 -0
- package/src/lang/ar.json +16 -0
- package/src/lang/ca.json +19 -0
- package/src/lang/ckb.json +108 -0
- package/src/lang/de.json +16 -0
- package/src/lang/en.json +118 -0
- package/src/lang/es.json +17 -0
- package/src/lang/eu.json +16 -0
- package/src/lang/fa.json +16 -0
- package/src/lang/fr.json +30 -0
- package/src/lang/he.json +16 -0
- package/src/lang/hi.json +16 -0
- package/src/lang/ja.json +16 -0
- package/src/lang/ko.json +16 -0
- package/src/lang/lt.json +16 -0
- package/src/lang/nl.json +16 -0
- package/src/lang/pl.json +16 -0
- package/src/lang/pt-BR.json +19 -0
- package/src/lang/ru.json +118 -0
- package/src/lang/ta.json +16 -0
- package/src/lang/tr.json +16 -0
- package/src/lang/uk.json +16 -0
- package/src/lang/ur.json +16 -0
- package/src/lang/uz.json +15 -0
- package/src/lang/zh-CN.json +115 -0
- package/src/lang/zh-TW.json +16 -0
- package/src/s-blocks.ts +1 -0
- package/src/s-login.ts +1 -0
- package/src/stores.ts +165 -0
- package/src/types.ts +106 -0
- package/src/vite-env-override.d.ts +20 -0
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
import { onMount, createEventDispatcher } from "svelte";
|
|
4
|
+
import type { ComponentMeta, Dependency } from "../types";
|
|
5
|
+
import NoApi from "./NoApi.svelte";
|
|
6
|
+
import type { Client } from "@gradio/client";
|
|
7
|
+
import type { Payload } from "../types";
|
|
8
|
+
|
|
9
|
+
import ApiBanner from "./ApiBanner.svelte";
|
|
10
|
+
import Button from "../../../button/shared/Button.svelte";
|
|
11
|
+
import ParametersSnippet from "./ParametersSnippet.svelte";
|
|
12
|
+
import InstallSnippet from "./InstallSnippet.svelte";
|
|
13
|
+
import CodeSnippet from "./CodeSnippet.svelte";
|
|
14
|
+
import RecordingSnippet from "./RecordingSnippet.svelte";
|
|
15
|
+
|
|
16
|
+
import python from "./img/python.svg";
|
|
17
|
+
import javascript from "./img/javascript.svg";
|
|
18
|
+
import bash from "./img/bash.svg";
|
|
19
|
+
import ResponseSnippet from "./ResponseSnippet.svelte";
|
|
20
|
+
|
|
21
|
+
export let dependencies: Dependency[];
|
|
22
|
+
export let root: string;
|
|
23
|
+
export let app: Awaited<ReturnType<typeof Client.connect>>;
|
|
24
|
+
export let space_id: string | null;
|
|
25
|
+
export let root_node: ComponentMeta;
|
|
26
|
+
export let username: string | null;
|
|
27
|
+
|
|
28
|
+
const js_docs =
|
|
29
|
+
"https://www.gradio.app/guides/getting-started-with-the-js-client";
|
|
30
|
+
const py_docs =
|
|
31
|
+
"https://www.gradio.app/guides/getting-started-with-the-python-client";
|
|
32
|
+
const bash_docs =
|
|
33
|
+
"https://www.gradio.app/guides/querying-gradio-apps-with-curl";
|
|
34
|
+
const spaces_docs_suffix = "#connecting-to-a-hugging-face-space";
|
|
35
|
+
|
|
36
|
+
let api_count = dependencies.filter(
|
|
37
|
+
(dependency) => dependency.show_api
|
|
38
|
+
).length;
|
|
39
|
+
|
|
40
|
+
if (root === "") {
|
|
41
|
+
root = location.protocol + "//" + location.host + location.pathname;
|
|
42
|
+
}
|
|
43
|
+
if (!root.endsWith("/")) {
|
|
44
|
+
root += "/";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export let api_calls: Payload[] = [];
|
|
48
|
+
let current_language: "python" | "javascript" | "bash" = "python";
|
|
49
|
+
|
|
50
|
+
const langs = [
|
|
51
|
+
["python", python],
|
|
52
|
+
["javascript", javascript],
|
|
53
|
+
["bash", bash]
|
|
54
|
+
] as const;
|
|
55
|
+
|
|
56
|
+
let is_running = false;
|
|
57
|
+
|
|
58
|
+
async function get_info(): Promise<{
|
|
59
|
+
named_endpoints: any;
|
|
60
|
+
unnamed_endpoints: any;
|
|
61
|
+
}> {
|
|
62
|
+
let response = await fetch(root + "info");
|
|
63
|
+
let data = await response.json();
|
|
64
|
+
return data;
|
|
65
|
+
}
|
|
66
|
+
async function get_js_info(): Promise<Record<string, any>> {
|
|
67
|
+
let js_api_info = await app.view_api();
|
|
68
|
+
return js_api_info;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let info: {
|
|
72
|
+
named_endpoints: any;
|
|
73
|
+
unnamed_endpoints: any;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
let js_info: Record<string, any>;
|
|
77
|
+
|
|
78
|
+
get_info().then((data) => {
|
|
79
|
+
info = data;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
get_js_info().then((js_api_info) => {
|
|
83
|
+
js_info = js_api_info;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const dispatch = createEventDispatcher();
|
|
87
|
+
|
|
88
|
+
onMount(() => {
|
|
89
|
+
document.body.style.overflow = "hidden";
|
|
90
|
+
if ("parentIFrame" in window) {
|
|
91
|
+
window.parentIFrame?.scrollTo(0, 0);
|
|
92
|
+
}
|
|
93
|
+
return () => {
|
|
94
|
+
document.body.style.overflow = "auto";
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
</script>
|
|
98
|
+
|
|
99
|
+
{#if info}
|
|
100
|
+
{#if api_count}
|
|
101
|
+
<div class="banner-wrap">
|
|
102
|
+
<ApiBanner on:close root={space_id || root} {api_count} />
|
|
103
|
+
</div>
|
|
104
|
+
|
|
105
|
+
<div class="docs-wrap">
|
|
106
|
+
<div class="client-doc">
|
|
107
|
+
<p style="font-size: var(--text-lg);">
|
|
108
|
+
Choose a language to see the code snippets for interacting with the
|
|
109
|
+
API.
|
|
110
|
+
</p>
|
|
111
|
+
</div>
|
|
112
|
+
<div class="endpoint">
|
|
113
|
+
<div class="snippets">
|
|
114
|
+
{#each langs as [language, img]}
|
|
115
|
+
<li
|
|
116
|
+
class="snippet
|
|
117
|
+
{current_language === language ? 'current-lang' : 'inactive-lang'}"
|
|
118
|
+
on:click={() => (current_language = language)}
|
|
119
|
+
>
|
|
120
|
+
<img src={img} alt="" />
|
|
121
|
+
{language}
|
|
122
|
+
</li>
|
|
123
|
+
{/each}
|
|
124
|
+
</div>
|
|
125
|
+
{#if api_calls.length}
|
|
126
|
+
<div>
|
|
127
|
+
<p
|
|
128
|
+
id="num-recorded-api-calls"
|
|
129
|
+
style="font-size: var(--text-lg); font-weight:bold; margin: 10px 0px;"
|
|
130
|
+
>
|
|
131
|
+
🪄 Recorded API Calls <span class="api-count"
|
|
132
|
+
>[{api_calls.length}]</span
|
|
133
|
+
>
|
|
134
|
+
</p>
|
|
135
|
+
<p>
|
|
136
|
+
Here is the code snippet to replay the most recently recorded API
|
|
137
|
+
calls using the {current_language}
|
|
138
|
+
client.
|
|
139
|
+
</p>
|
|
140
|
+
|
|
141
|
+
<RecordingSnippet
|
|
142
|
+
{current_language}
|
|
143
|
+
{api_calls}
|
|
144
|
+
{dependencies}
|
|
145
|
+
{root}
|
|
146
|
+
short_root={space_id || root}
|
|
147
|
+
{username}
|
|
148
|
+
/>
|
|
149
|
+
<p>
|
|
150
|
+
Note: Some API calls only affect the UI, so when using the
|
|
151
|
+
clients, the desired result may be achieved with only a subset of
|
|
152
|
+
the recorded calls.
|
|
153
|
+
</p>
|
|
154
|
+
</div>
|
|
155
|
+
<p
|
|
156
|
+
style="font-size: var(--text-lg); font-weight:bold; margin: 30px 0px 10px;"
|
|
157
|
+
>
|
|
158
|
+
API Documentation
|
|
159
|
+
</p>
|
|
160
|
+
{:else}
|
|
161
|
+
<p class="padded">
|
|
162
|
+
{#if current_language == "python" || current_language == "javascript"}
|
|
163
|
+
1. Install the
|
|
164
|
+
<span style="text-transform:capitalize">{current_language}</span>
|
|
165
|
+
client (<a
|
|
166
|
+
href={current_language == "python" ? py_docs : js_docs}
|
|
167
|
+
target="_blank">docs</a
|
|
168
|
+
>) if you don't already have it installed.
|
|
169
|
+
{:else}
|
|
170
|
+
1. Confirm that you have cURL installed on your system.
|
|
171
|
+
{/if}
|
|
172
|
+
</p>
|
|
173
|
+
|
|
174
|
+
<InstallSnippet {current_language} />
|
|
175
|
+
|
|
176
|
+
<p class="padded">
|
|
177
|
+
2. Find the API endpoint below corresponding to your desired
|
|
178
|
+
function in the app. Copy the code snippet, replacing the
|
|
179
|
+
placeholder values with your own input data.
|
|
180
|
+
{#if space_id}If this is a private Space, you may need to pass your
|
|
181
|
+
Hugging Face token as well (<a
|
|
182
|
+
href={current_language == "python"
|
|
183
|
+
? py_docs + spaces_docs_suffix
|
|
184
|
+
: current_language == "javascript"
|
|
185
|
+
? js_docs + spaces_docs_suffix
|
|
186
|
+
: bash_docs}
|
|
187
|
+
class="underline"
|
|
188
|
+
target="_blank">read more</a
|
|
189
|
+
>).{/if}
|
|
190
|
+
|
|
191
|
+
Or use the
|
|
192
|
+
<Button
|
|
193
|
+
size="sm"
|
|
194
|
+
variant="secondary"
|
|
195
|
+
on:click={() => dispatch("close", { api_recorder_visible: true })}
|
|
196
|
+
>
|
|
197
|
+
<div class="loading-dot"></div>
|
|
198
|
+
<p class="self-baseline">API Recorder</p>
|
|
199
|
+
</Button>
|
|
200
|
+
to automatically generate your API requests.
|
|
201
|
+
{#if current_language == "bash"}<br /> <br />Making a
|
|
202
|
+
prediction and getting a result requires
|
|
203
|
+
<strong>2 requests</strong>: a
|
|
204
|
+
<code>POST</code>
|
|
205
|
+
and a <code>GET</code> request. The <code>POST</code> request
|
|
206
|
+
returns an <code>EVENT_ID</code>, which is used in the second
|
|
207
|
+
<code>GET</code> request to fetch the results. In these snippets,
|
|
208
|
+
we've used <code>awk</code> and <code>read</code> to parse the
|
|
209
|
+
results, combining these two requests into one command for ease of
|
|
210
|
+
use. {#if username !== null}
|
|
211
|
+
Note: connecting to an authenticated app requires an additional
|
|
212
|
+
request.{/if} See
|
|
213
|
+
<a href={bash_docs} target="_blank">curl docs</a>.
|
|
214
|
+
{/if}
|
|
215
|
+
|
|
216
|
+
<!-- <span
|
|
217
|
+
id="api-recorder"
|
|
218
|
+
on:click={() => dispatch("close", { api_recorder_visible: true })}
|
|
219
|
+
>🪄 API Recorder</span
|
|
220
|
+
> to automatically generate your API requests! -->
|
|
221
|
+
</p>
|
|
222
|
+
{/if}
|
|
223
|
+
|
|
224
|
+
{#each dependencies as dependency, dependency_index}
|
|
225
|
+
{#if dependency.show_api && info.named_endpoints["/" + dependency.api_name]}
|
|
226
|
+
<div class="endpoint-container">
|
|
227
|
+
<CodeSnippet
|
|
228
|
+
named={true}
|
|
229
|
+
endpoint_parameters={info.named_endpoints[
|
|
230
|
+
"/" + dependency.api_name
|
|
231
|
+
].parameters}
|
|
232
|
+
{dependency}
|
|
233
|
+
{dependency_index}
|
|
234
|
+
{current_language}
|
|
235
|
+
{root}
|
|
236
|
+
{space_id}
|
|
237
|
+
{username}
|
|
238
|
+
/>
|
|
239
|
+
|
|
240
|
+
<ParametersSnippet
|
|
241
|
+
endpoint_returns={info.named_endpoints[
|
|
242
|
+
"/" + dependency.api_name
|
|
243
|
+
].parameters}
|
|
244
|
+
js_returns={js_info.named_endpoints["/" + dependency.api_name]
|
|
245
|
+
.parameters}
|
|
246
|
+
{is_running}
|
|
247
|
+
{current_language}
|
|
248
|
+
/>
|
|
249
|
+
|
|
250
|
+
<ResponseSnippet
|
|
251
|
+
endpoint_returns={info.named_endpoints[
|
|
252
|
+
"/" + dependency.api_name
|
|
253
|
+
].returns}
|
|
254
|
+
js_returns={js_info.named_endpoints["/" + dependency.api_name]
|
|
255
|
+
.returns}
|
|
256
|
+
{is_running}
|
|
257
|
+
{current_language}
|
|
258
|
+
/>
|
|
259
|
+
</div>
|
|
260
|
+
{/if}
|
|
261
|
+
{/each}
|
|
262
|
+
</div>
|
|
263
|
+
</div>
|
|
264
|
+
{:else}
|
|
265
|
+
<NoApi {root} on:close />
|
|
266
|
+
{/if}
|
|
267
|
+
{/if}
|
|
268
|
+
|
|
269
|
+
<style>
|
|
270
|
+
.banner-wrap {
|
|
271
|
+
position: relative;
|
|
272
|
+
border-bottom: 1px solid var(--border-color-primary);
|
|
273
|
+
padding: var(--size-4) var(--size-6);
|
|
274
|
+
font-size: var(--text-md);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
@media (--screen-md) {
|
|
278
|
+
.banner-wrap {
|
|
279
|
+
font-size: var(--text-xl);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
.docs-wrap {
|
|
284
|
+
display: flex;
|
|
285
|
+
flex-direction: column;
|
|
286
|
+
gap: var(--spacing-xxl);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.endpoint {
|
|
290
|
+
border-radius: var(--radius-md);
|
|
291
|
+
background: var(--background-fill-primary);
|
|
292
|
+
padding: var(--size-6);
|
|
293
|
+
padding-top: var(--size-1);
|
|
294
|
+
font-size: var(--text-md);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
.client-doc {
|
|
298
|
+
padding-top: var(--size-6);
|
|
299
|
+
padding-right: var(--size-6);
|
|
300
|
+
padding-left: var(--size-6);
|
|
301
|
+
font-size: var(--text-md);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
.library {
|
|
305
|
+
border: 1px solid var(--border-color-accent);
|
|
306
|
+
border-radius: var(--radius-sm);
|
|
307
|
+
background: var(--color-accent-soft);
|
|
308
|
+
padding: 0px var(--size-1);
|
|
309
|
+
color: var(--color-accent);
|
|
310
|
+
font-size: var(--text-md);
|
|
311
|
+
text-decoration: none;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
.snippets {
|
|
315
|
+
display: flex;
|
|
316
|
+
align-items: center;
|
|
317
|
+
margin-bottom: var(--size-4);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
.snippets > * + * {
|
|
321
|
+
margin-left: var(--size-2);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
.snippet {
|
|
325
|
+
display: flex;
|
|
326
|
+
align-items: center;
|
|
327
|
+
border: 1px solid var(--border-color-primary);
|
|
328
|
+
|
|
329
|
+
border-radius: var(--radius-md);
|
|
330
|
+
padding: var(--size-1) var(--size-1-5);
|
|
331
|
+
color: var(--body-text-color-subdued);
|
|
332
|
+
color: var(--body-text-color);
|
|
333
|
+
line-height: 1;
|
|
334
|
+
user-select: none;
|
|
335
|
+
text-transform: capitalize;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
.current-lang {
|
|
339
|
+
border: 1px solid var(--body-text-color-subdued);
|
|
340
|
+
color: var(--body-text-color);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
.inactive-lang {
|
|
344
|
+
cursor: pointer;
|
|
345
|
+
color: var(--body-text-color-subdued);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
.inactive-lang:hover,
|
|
349
|
+
.inactive-lang:focus {
|
|
350
|
+
box-shadow: var(--shadow-drop);
|
|
351
|
+
color: var(--body-text-color);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
.snippet img {
|
|
355
|
+
margin-right: var(--size-1-5);
|
|
356
|
+
width: var(--size-3);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
.header {
|
|
360
|
+
margin-top: var(--size-6);
|
|
361
|
+
font-size: var(--text-xl);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
.endpoint-container {
|
|
365
|
+
margin-top: var(--size-3);
|
|
366
|
+
margin-bottom: var(--size-3);
|
|
367
|
+
border: 1px solid var(--body-text-color);
|
|
368
|
+
border-radius: var(--radius-xl);
|
|
369
|
+
padding: var(--size-3);
|
|
370
|
+
padding-top: 0;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
a {
|
|
374
|
+
text-decoration: underline;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
p.padded {
|
|
378
|
+
padding: 15px 0px;
|
|
379
|
+
font-size: var(--text-lg);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
#api-recorder {
|
|
383
|
+
border: 1px solid var(--color-accent);
|
|
384
|
+
background-color: var(--color-accent-soft);
|
|
385
|
+
padding: 0px var(--size-2);
|
|
386
|
+
border-radius: var(--size-1);
|
|
387
|
+
cursor: pointer;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
code {
|
|
391
|
+
font-size: var(--text-md);
|
|
392
|
+
}
|
|
393
|
+
.loading-dot {
|
|
394
|
+
position: relative;
|
|
395
|
+
left: -9999px;
|
|
396
|
+
width: 10px;
|
|
397
|
+
height: 10px;
|
|
398
|
+
border-radius: 5px;
|
|
399
|
+
background-color: #fd7b00;
|
|
400
|
+
color: #fd7b00;
|
|
401
|
+
box-shadow: 9999px 0 0 -1px;
|
|
402
|
+
margin-right: 0.25rem;
|
|
403
|
+
}
|
|
404
|
+
:global(.docs-wrap .sm.secondary) {
|
|
405
|
+
padding-top: 1px;
|
|
406
|
+
padding-bottom: 1px;
|
|
407
|
+
}
|
|
408
|
+
.self-baseline {
|
|
409
|
+
align-self: baseline;
|
|
410
|
+
}
|
|
411
|
+
.api-count {
|
|
412
|
+
font-weight: bold;
|
|
413
|
+
color: #fd7b00;
|
|
414
|
+
align-self: baseline;
|
|
415
|
+
font-family: var(--font-mono);
|
|
416
|
+
font-size: var(--text-md);
|
|
417
|
+
}
|
|
418
|
+
</style>
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Payload, Dependency } from "../types";
|
|
3
|
+
import Button from "../../../button/shared/Button.svelte";
|
|
4
|
+
|
|
5
|
+
export let api_calls: Payload[] = [];
|
|
6
|
+
export let dependencies: Dependency[];
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<div id="api-recorder">
|
|
10
|
+
<Button size="sm" variant="secondary">
|
|
11
|
+
<div class="loading-dot self-baseline"></div>
|
|
12
|
+
<p class="self-baseline">Recording API Calls:</p>
|
|
13
|
+
<p class="self-baseline api-section">
|
|
14
|
+
<span class="api-count">
|
|
15
|
+
[{api_calls.length}]
|
|
16
|
+
</span>
|
|
17
|
+
{#if api_calls.length > 0}
|
|
18
|
+
<span class="api-name"
|
|
19
|
+
>/{dependencies[api_calls[api_calls.length - 1].fn_index]
|
|
20
|
+
.api_name}</span
|
|
21
|
+
>
|
|
22
|
+
{/if}
|
|
23
|
+
</p>
|
|
24
|
+
</Button>
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
<style>
|
|
28
|
+
.api-name {
|
|
29
|
+
font-size: var(--text-sm);
|
|
30
|
+
font-family: var(--font-mono);
|
|
31
|
+
color: #fd7b00;
|
|
32
|
+
}
|
|
33
|
+
.loading-dot {
|
|
34
|
+
position: relative;
|
|
35
|
+
left: -9999px;
|
|
36
|
+
width: 10px;
|
|
37
|
+
height: 10px;
|
|
38
|
+
border-radius: 5px;
|
|
39
|
+
background-color: #fd7b00;
|
|
40
|
+
color: #fd7b00;
|
|
41
|
+
box-shadow: 9999px 0 0 -1px;
|
|
42
|
+
animation: loading-dot 2s infinite linear;
|
|
43
|
+
animation-delay: 0.25s;
|
|
44
|
+
margin-left: 0.25rem;
|
|
45
|
+
margin-right: 0.5rem;
|
|
46
|
+
}
|
|
47
|
+
:global(.docs-wrap .sm.secondary) {
|
|
48
|
+
padding-top: 1px;
|
|
49
|
+
padding-bottom: 1px;
|
|
50
|
+
}
|
|
51
|
+
.self-baseline {
|
|
52
|
+
align-self: baseline;
|
|
53
|
+
}
|
|
54
|
+
@keyframes loading-dot {
|
|
55
|
+
0% {
|
|
56
|
+
box-shadow: 9999px 0 0 -1px;
|
|
57
|
+
}
|
|
58
|
+
50% {
|
|
59
|
+
box-shadow: 9999px 0 0 2px;
|
|
60
|
+
}
|
|
61
|
+
100% {
|
|
62
|
+
box-shadow: 9999px 0 0 -1px;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
.api-count {
|
|
66
|
+
font-weight: bold;
|
|
67
|
+
color: #fd7b00;
|
|
68
|
+
align-self: baseline;
|
|
69
|
+
font-family: var(--font-mono);
|
|
70
|
+
font-size: var(--text-sm);
|
|
71
|
+
}
|
|
72
|
+
.api-section {
|
|
73
|
+
margin-left: 4px;
|
|
74
|
+
}
|
|
75
|
+
</style>
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ComponentMeta, Dependency } from "../types";
|
|
3
|
+
import CopyButton from "./CopyButton.svelte";
|
|
4
|
+
import { represent_value, is_potentially_nested_file_data } from "./utils";
|
|
5
|
+
import { Block } from "@gradio/atoms";
|
|
6
|
+
import EndpointDetail from "./EndpointDetail.svelte";
|
|
7
|
+
|
|
8
|
+
interface EndpointParameter {
|
|
9
|
+
label: string;
|
|
10
|
+
type: string;
|
|
11
|
+
python_type: { type: string };
|
|
12
|
+
component: string;
|
|
13
|
+
example_input: string;
|
|
14
|
+
serializer: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export let dependency: Dependency;
|
|
18
|
+
export let dependency_index: number;
|
|
19
|
+
export let root: string;
|
|
20
|
+
export let space_id: string | null;
|
|
21
|
+
export let endpoint_parameters: any;
|
|
22
|
+
export let named: boolean;
|
|
23
|
+
export let username: string | null;
|
|
24
|
+
export let current_language: "python" | "javascript" | "bash";
|
|
25
|
+
|
|
26
|
+
let python_code: HTMLElement;
|
|
27
|
+
let js_code: HTMLElement;
|
|
28
|
+
let bash_post_code: HTMLElement;
|
|
29
|
+
let bash_get_code: HTMLElement;
|
|
30
|
+
|
|
31
|
+
let has_file_path = endpoint_parameters.some((param: EndpointParameter) =>
|
|
32
|
+
is_potentially_nested_file_data(param.example_input)
|
|
33
|
+
);
|
|
34
|
+
let blob_components = ["Audio", "File", "Image", "Video"];
|
|
35
|
+
let blob_examples: any[] = endpoint_parameters.filter(
|
|
36
|
+
(param: EndpointParameter) => blob_components.includes(param.component)
|
|
37
|
+
);
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
<div class="container">
|
|
41
|
+
{#if named}
|
|
42
|
+
<EndpointDetail {named} api_name={dependency.api_name} />
|
|
43
|
+
{:else}
|
|
44
|
+
<EndpointDetail {named} fn_index={dependency_index} />
|
|
45
|
+
{/if}
|
|
46
|
+
{#if current_language === "python"}
|
|
47
|
+
<Block>
|
|
48
|
+
<code>
|
|
49
|
+
<div class="copy">
|
|
50
|
+
<CopyButton code={python_code?.innerText} />
|
|
51
|
+
</div>
|
|
52
|
+
<div bind:this={python_code}>
|
|
53
|
+
<pre><span class="highlight">from</span> gradio_client <span
|
|
54
|
+
class="highlight">import</span
|
|
55
|
+
> Client{#if has_file_path}, handle_file{/if}
|
|
56
|
+
|
|
57
|
+
client = Client(<span class="token string">"{space_id || root}"</span
|
|
58
|
+
>{#if username !== null}, auth=("{username}", **password**){/if})
|
|
59
|
+
result = client.<span class="highlight">predict</span
|
|
60
|
+
>(<!--
|
|
61
|
+
-->{#each endpoint_parameters as { python_type, example_input, parameter_name, parameter_has_default, parameter_default }, i}<!--
|
|
62
|
+
-->
|
|
63
|
+
{parameter_name
|
|
64
|
+
? parameter_name + "="
|
|
65
|
+
: ""}<span
|
|
66
|
+
>{represent_value(
|
|
67
|
+
parameter_has_default ? parameter_default : example_input,
|
|
68
|
+
python_type.type,
|
|
69
|
+
"py"
|
|
70
|
+
)}</span
|
|
71
|
+
>,{/each}<!--
|
|
72
|
+
|
|
73
|
+
-->
|
|
74
|
+
api_name=<span class="api-name">"/{dependency.api_name}"</span><!--
|
|
75
|
+
-->
|
|
76
|
+
)
|
|
77
|
+
<span class="highlight">print</span>(result)</pre>
|
|
78
|
+
</div>
|
|
79
|
+
</code>
|
|
80
|
+
</Block>
|
|
81
|
+
{:else if current_language === "javascript"}
|
|
82
|
+
<Block>
|
|
83
|
+
<code>
|
|
84
|
+
<div class="copy">
|
|
85
|
+
<CopyButton code={js_code?.innerText} />
|
|
86
|
+
</div>
|
|
87
|
+
<div bind:this={js_code}>
|
|
88
|
+
<pre>import { Client } from "@gradio/client";
|
|
89
|
+
{#each blob_examples as { component, example_input }, i}<!--
|
|
90
|
+
-->
|
|
91
|
+
const response_{i} = await fetch("{example_input.url}");
|
|
92
|
+
const example{component} = await response_{i}.blob();
|
|
93
|
+
{/each}<!--
|
|
94
|
+
-->
|
|
95
|
+
const client = await Client.connect(<span class="token string"
|
|
96
|
+
>"{space_id || root}"</span
|
|
97
|
+
>{#if username !== null}, {auth: ["{username}", **password**]}{/if});
|
|
98
|
+
const result = await client.predict({#if named}<span class="api-name"
|
|
99
|
+
>"/{dependency.api_name}"</span
|
|
100
|
+
>{:else}{dependency_index}{/if}, { <!--
|
|
101
|
+
-->{#each endpoint_parameters as { label, parameter_name, type, python_type, component, example_input, serializer }, i}<!--
|
|
102
|
+
-->{#if blob_components.includes(component)}<!--
|
|
103
|
+
-->
|
|
104
|
+
<span
|
|
105
|
+
class="example-inputs"
|
|
106
|
+
>{parameter_name}: example{component}</span
|
|
107
|
+
>, <!--
|
|
108
|
+
--><span class="desc"><!--
|
|
109
|
+
--></span
|
|
110
|
+
><!--
|
|
111
|
+
-->{:else}<!--
|
|
112
|
+
-->
|
|
113
|
+
<span class="example-inputs"
|
|
114
|
+
>{parameter_name}: {represent_value(
|
|
115
|
+
example_input,
|
|
116
|
+
python_type.type,
|
|
117
|
+
"js"
|
|
118
|
+
)}</span
|
|
119
|
+
>, <!--
|
|
120
|
+
--><!--
|
|
121
|
+
-->{/if}
|
|
122
|
+
{/each}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
console.log(result.data);
|
|
126
|
+
</pre>
|
|
127
|
+
</div>
|
|
128
|
+
</code>
|
|
129
|
+
</Block>
|
|
130
|
+
{:else if current_language === "bash"}
|
|
131
|
+
<Block>
|
|
132
|
+
<code>
|
|
133
|
+
<div class="copy">
|
|
134
|
+
<CopyButton code={bash_post_code?.innerText}></CopyButton>
|
|
135
|
+
</div>
|
|
136
|
+
|
|
137
|
+
<div bind:this={bash_post_code}>
|
|
138
|
+
<pre>curl -X POST {root}call/{dependency.api_name} -s -H "Content-Type: application/json" -d '{"{"}
|
|
139
|
+
"data": [{#each endpoint_parameters as { label, parameter_name, type, python_type, component, example_input, serializer }, i}
|
|
140
|
+
<!--
|
|
141
|
+
-->{represent_value(
|
|
142
|
+
example_input,
|
|
143
|
+
python_type.type,
|
|
144
|
+
"bash"
|
|
145
|
+
)}{#if i < endpoint_parameters.length - 1},
|
|
146
|
+
{/if}
|
|
147
|
+
{/each}
|
|
148
|
+
]{"}"}' \
|
|
149
|
+
| awk -F'"' '{"{"} print $4{"}"}' \
|
|
150
|
+
| read EVENT_ID; curl -N {root}call/{dependency.api_name}/$EVENT_ID</pre>
|
|
151
|
+
</div>
|
|
152
|
+
</code>
|
|
153
|
+
</Block>
|
|
154
|
+
{/if}
|
|
155
|
+
</div>
|
|
156
|
+
|
|
157
|
+
<style>
|
|
158
|
+
code pre {
|
|
159
|
+
overflow-x: auto;
|
|
160
|
+
color: var(--body-text-color);
|
|
161
|
+
font-family: var(--font-mono);
|
|
162
|
+
tab-size: 2;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
.token.string {
|
|
166
|
+
display: contents;
|
|
167
|
+
color: var(--color-accent-base);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
code {
|
|
171
|
+
position: relative;
|
|
172
|
+
display: block;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
.copy {
|
|
176
|
+
position: absolute;
|
|
177
|
+
top: 0;
|
|
178
|
+
right: 0;
|
|
179
|
+
margin-top: -5px;
|
|
180
|
+
margin-right: -5px;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
.container {
|
|
184
|
+
display: flex;
|
|
185
|
+
flex-direction: column;
|
|
186
|
+
gap: var(--spacing-xxl);
|
|
187
|
+
margin-top: var(--size-3);
|
|
188
|
+
margin-bottom: var(--size-3);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
.desc {
|
|
192
|
+
color: var(--body-text-color-subdued);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
.api-name {
|
|
196
|
+
color: var(--color-accent);
|
|
197
|
+
}
|
|
198
|
+
</style>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { BaseButton } from "@gradio/button";
|
|
3
|
+
export let code: string;
|
|
4
|
+
let copy_text = "copy";
|
|
5
|
+
|
|
6
|
+
function copy(): void {
|
|
7
|
+
navigator.clipboard.writeText(code);
|
|
8
|
+
copy_text = "copied!";
|
|
9
|
+
setTimeout(() => {
|
|
10
|
+
copy_text = "copy";
|
|
11
|
+
}, 1500);
|
|
12
|
+
}
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<BaseButton size="sm" on:click={copy}>
|
|
16
|
+
{copy_text}
|
|
17
|
+
</BaseButton>
|