gradio-pr-bot commited on
Commit
9e97ffb
·
verified ·
1 Parent(s): 400a719

Upload folder using huggingface_hub

Browse files
6.0.2/utils/package.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/utils",
3
+ "version": "0.10.4",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "main": "./src/index.ts",
7
+ "author": "",
8
+ "license": "ISC",
9
+ "dependencies": {
10
+ "@gradio/theme": "workspace:^"
11
+ },
12
+ "main_changeset": true,
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/gradio-app/gradio.git",
16
+ "directory": "js/utils"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "gradio": "./src/index.ts",
21
+ "types": "./dist/src/index.d.ts",
22
+ "import": "./dist/src/index.js"
23
+ },
24
+ "./package.json": "./package.json"
25
+ },
26
+ "scripts": {
27
+ "package": "svelte-package --input=. --tsconfig=../../tsconfig.json"
28
+ }
29
+ }
6.0.2/utils/src/color.ts ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import { colors, ordered_colors } from "@gradio/theme";
2
+
3
+ export const get_next_color = (index: number): keyof typeof colors => {
4
+ return ordered_colors[index % ordered_colors.length];
5
+ };
6.0.2/utils/src/index.ts ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ export * from "./color.js";
2
+ export * from "./utils.svelte.js";
6.0.2/utils/src/utils.svelte.ts ADDED
@@ -0,0 +1,431 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { ActionReturn } from "svelte/action";
2
+ import type { Client } from "@gradio/client";
3
+ import type { ComponentType, SvelteComponent } from "svelte";
4
+ import { getContext, tick, untrack } from "svelte";
5
+ import type { Component } from "svelte";
6
+
7
+ export interface SharedProps {
8
+ elem_id?: string;
9
+ elem_classes: string[];
10
+ components?: string[];
11
+ server_fns?: string[];
12
+ interactive: boolean;
13
+ visible: boolean | "hidden";
14
+ id: number;
15
+ container: boolean;
16
+ target: HTMLElement;
17
+ theme_mode: "light" | "dark" | "system";
18
+ version: string;
19
+ root: string;
20
+ autoscroll: boolean;
21
+ max_file_size: number | null;
22
+ formatter: any; //I18nFormatter;
23
+ client: Client;
24
+ scale: number;
25
+ min_width: number;
26
+ padding: number;
27
+ load_component: (
28
+ arg0: string,
29
+ arg1: "base" | "example" | "component"
30
+ ) => LoadingComponent; //component_loader;
31
+ loading_status?: any;
32
+ label: string;
33
+ show_label: boolean;
34
+ validation_error?: string | null;
35
+ theme?: "light" | "dark";
36
+ show_progress: boolean;
37
+ api_prefix: string;
38
+ server: ServerFunctions;
39
+ attached_events?: string[];
40
+ }
41
+
42
+ export type LoadingComponent = Promise<{
43
+ default: Component;
44
+ }>;
45
+
46
+ export const GRADIO_ROOT = "GRADIO_ROOT";
47
+
48
+ export interface ValueData {
49
+ value: any;
50
+ is_value_data: boolean;
51
+ }
52
+
53
+ export interface SelectData {
54
+ row_value?: any[];
55
+ col_value?: any[];
56
+ index: number | [number, number];
57
+ value: any;
58
+ selected?: boolean;
59
+ }
60
+
61
+ export interface LikeData {
62
+ index: number | [number, number];
63
+ value: any;
64
+ liked?: boolean | string;
65
+ }
66
+
67
+ export interface KeyUpData {
68
+ key: string;
69
+ input_value: string;
70
+ }
71
+
72
+ export interface ShareData {
73
+ description: string;
74
+ title?: string;
75
+ }
76
+
77
+ export interface CopyData {
78
+ value: string;
79
+ }
80
+
81
+ export class ShareError extends Error {
82
+ constructor(message: string) {
83
+ super(message);
84
+ this.name = "ShareError";
85
+ }
86
+ }
87
+
88
+ export async function uploadToHuggingFace(
89
+ data: string | { url?: string; path?: string },
90
+ type: "base64" | "url"
91
+ ): Promise<string> {
92
+ if (window.__gradio_space__ == null) {
93
+ throw new ShareError("Must be on Spaces to share.");
94
+ }
95
+ let blob: Blob;
96
+ let contentType: string;
97
+ let filename: string;
98
+ if (type === "url") {
99
+ let url: string;
100
+
101
+ if (typeof data === "object" && data.url) {
102
+ url = data.url;
103
+ } else if (typeof data === "string") {
104
+ url = data;
105
+ } else {
106
+ throw new Error("Invalid data format for URL type");
107
+ }
108
+
109
+ const response = await fetch(url);
110
+ blob = await response.blob();
111
+ contentType = response.headers.get("content-type") || "";
112
+ filename = response.headers.get("content-disposition") || "";
113
+ } else {
114
+ let dataurl: string;
115
+
116
+ if (typeof data === "object" && data.path) {
117
+ dataurl = data.path;
118
+ } else if (typeof data === "string") {
119
+ dataurl = data;
120
+ } else {
121
+ throw new Error("Invalid data format for base64 type");
122
+ }
123
+
124
+ blob = dataURLtoBlob(dataurl);
125
+ contentType = dataurl.split(";")[0].split(":")[1];
126
+ filename = "file." + contentType.split("/")[1];
127
+ }
128
+
129
+ const file = new File([blob], filename, { type: contentType });
130
+
131
+ // Send file to endpoint
132
+ const uploadResponse = await fetch("https://huggingface.co/uploads", {
133
+ method: "POST",
134
+ body: file,
135
+ headers: {
136
+ "Content-Type": file.type,
137
+ "X-Requested-With": "XMLHttpRequest"
138
+ }
139
+ });
140
+
141
+ // Check status of response
142
+ if (!uploadResponse.ok) {
143
+ if (
144
+ uploadResponse.headers.get("content-type")?.includes("application/json")
145
+ ) {
146
+ const error = await uploadResponse.json();
147
+ throw new ShareError(`Upload failed: ${error.error}`);
148
+ }
149
+ throw new ShareError(`Upload failed.`);
150
+ }
151
+
152
+ // Return response if needed
153
+ const result = await uploadResponse.text();
154
+ return result;
155
+ }
156
+
157
+ function dataURLtoBlob(dataurl: string): Blob {
158
+ var arr = dataurl.split(","),
159
+ mime = (arr[0].match(/:(.*?);/) as RegExpMatchArray)[1],
160
+ bstr = atob(arr[1]),
161
+ n = bstr.length,
162
+ u8arr = new Uint8Array(n);
163
+ while (n--) {
164
+ u8arr[n] = bstr.charCodeAt(n);
165
+ }
166
+ return new Blob([u8arr], { type: mime });
167
+ }
168
+
169
+ export function copy(node: HTMLDivElement): ActionReturn {
170
+ node.addEventListener("click", handle_copy);
171
+
172
+ async function handle_copy(event: MouseEvent): Promise<void> {
173
+ const path = event.composedPath() as HTMLButtonElement[];
174
+
175
+ const [copy_button] = path.filter(
176
+ (e) => e?.tagName === "BUTTON" && e.classList.contains("copy_code_button")
177
+ );
178
+
179
+ if (copy_button) {
180
+ event.stopImmediatePropagation();
181
+
182
+ const copy_text = copy_button.parentElement!.innerText.trim();
183
+ const copy_sucess_button = Array.from(
184
+ copy_button.children
185
+ )[1] as HTMLDivElement;
186
+
187
+ const copied = await copy_to_clipboard(copy_text);
188
+
189
+ if (copied) copy_feedback(copy_sucess_button);
190
+
191
+ function copy_feedback(_copy_sucess_button: HTMLDivElement): void {
192
+ _copy_sucess_button.style.opacity = "1";
193
+ setTimeout(() => {
194
+ _copy_sucess_button.style.opacity = "0";
195
+ }, 2000);
196
+ }
197
+ }
198
+ }
199
+
200
+ return {
201
+ destroy(): void {
202
+ node.removeEventListener("click", handle_copy);
203
+ }
204
+ };
205
+ }
206
+
207
+ async function copy_to_clipboard(value: string): Promise<boolean> {
208
+ let copied = false;
209
+ if ("clipboard" in navigator) {
210
+ await navigator.clipboard.writeText(value);
211
+ copied = true;
212
+ } else {
213
+ const textArea = document.createElement("textarea");
214
+ textArea.value = value;
215
+
216
+ textArea.style.position = "absolute";
217
+ textArea.style.left = "-999999px";
218
+
219
+ document.body.prepend(textArea);
220
+ textArea.select();
221
+
222
+ try {
223
+ document.execCommand("copy");
224
+ copied = true;
225
+ } catch (error) {
226
+ console.error(error);
227
+ copied = false;
228
+ } finally {
229
+ textArea.remove();
230
+ }
231
+ }
232
+
233
+ return copied;
234
+ }
235
+
236
+ export const format_time = (seconds: number): string => {
237
+ const hours = Math.floor(seconds / 3600);
238
+ const minutes = Math.floor((seconds % 3600) / 60);
239
+ const seconds_remainder = Math.round(seconds) % 60;
240
+ const padded_minutes = `${minutes < 10 ? "0" : ""}${minutes}`;
241
+ const padded_seconds = `${
242
+ seconds_remainder < 10 ? "0" : ""
243
+ }${seconds_remainder}`;
244
+
245
+ if (hours > 0) {
246
+ return `${hours}:${padded_minutes}:${padded_seconds}`;
247
+ }
248
+ return `${minutes}:${padded_seconds}`;
249
+ };
250
+
251
+ interface Args {
252
+ api_url: string;
253
+ name: string;
254
+ id?: string;
255
+ variant: "component" | "example" | "base";
256
+ }
257
+
258
+ export type component_loader = (args: Args) => {
259
+ component: {
260
+ default: ComponentType<SvelteComponent>;
261
+ };
262
+ };
263
+
264
+ export type load_component = (
265
+ name: string,
266
+ variant: "component" | "example" | "base"
267
+ ) => LoadingComponent;
268
+
269
+ const is_browser = typeof window !== "undefined";
270
+
271
+ export type ServerFunctions = Record<string, (...args: any[]) => Promise<any>>;
272
+
273
+ export const allowed_shared_props: (keyof SharedProps)[] = [
274
+ "elem_id",
275
+ "elem_classes",
276
+ "visible",
277
+ "interactive",
278
+ "server_fns",
279
+ "server",
280
+ "id",
281
+ "target",
282
+ "theme_mode",
283
+ "version",
284
+ "root",
285
+ "autoscroll",
286
+ "max_file_size",
287
+ "formatter",
288
+ "client",
289
+ "load_component",
290
+ "scale",
291
+ "min_width",
292
+ "theme",
293
+ "padding",
294
+ "loading_status",
295
+ "label",
296
+ "show_label",
297
+ "validation_error",
298
+ "show_progress",
299
+ "api_prefix",
300
+ "container",
301
+ "attached_events"
302
+ ] as const;
303
+
304
+ export type I18nFormatter = any;
305
+ export class Gradio<T extends object = {}, U extends object = {}> {
306
+ load_component: load_component;
307
+ shared: SharedProps = $state<SharedProps>({} as SharedProps) as SharedProps;
308
+ props = $state<U>({} as U) as U;
309
+ i18n: I18nFormatter = $state<any>({}) as any;
310
+ dispatcher!: Function;
311
+ last_update: ReturnType<typeof tick> | null = null;
312
+ shared_props: (keyof SharedProps)[] = allowed_shared_props;
313
+
314
+ constructor(
315
+ _props: { shared_props: SharedProps; props: U },
316
+ default_values?: Partial<U>
317
+ ) {
318
+ for (const key in _props.shared_props) {
319
+ // @ts-ignore i'm not doing pointless typescript gymanstics
320
+ this.shared[key] = _props.shared_props[key];
321
+ }
322
+ for (const key in _props.props) {
323
+ // @ts-ignore same here
324
+ this.props[key] = _props.props[key];
325
+ }
326
+
327
+ if (default_values) {
328
+ for (const key in default_values) {
329
+ // @ts-ignore same here
330
+
331
+ if (this.props[key as keyof U] === undefined) {
332
+ this.props[key] = default_values[key as keyof U];
333
+ }
334
+ }
335
+ }
336
+ // @ts-ignore same here
337
+ this.i18n = this.props.i18n;
338
+
339
+ this.load_component = this.shared.load_component;
340
+
341
+ if (!is_browser || _props.props?.__GRADIO_BROWSER_TEST__) return;
342
+ const { register, dispatcher } = getContext<{
343
+ register: (
344
+ id: number,
345
+ set_data: (data: U & SharedProps) => void,
346
+ get_data: Function
347
+ ) => void;
348
+ dispatcher: Function;
349
+ }>(GRADIO_ROOT);
350
+
351
+ register(
352
+ _props.shared_props.id,
353
+ this.set_data.bind(this),
354
+ this.get_data.bind(this)
355
+ );
356
+
357
+ this.dispatcher = dispatcher;
358
+
359
+ $effect(() => {
360
+ // Need to update the props here
361
+ // otherwise UI won't reflect latest state from render
362
+ for (const key in _props.shared_props) {
363
+ // @ts-ignore i'm not doing pointless typescript gymanstics
364
+ this.shared[key] = _props.shared_props[key];
365
+ }
366
+ for (const key in _props.props) {
367
+ // @ts-ignore same here
368
+ this.props[key] = _props.props[key];
369
+ }
370
+ register(
371
+ _props.shared_props.id,
372
+ this.set_data.bind(this),
373
+ this.get_data.bind(this)
374
+ );
375
+ untrack(() => {
376
+ this.shared.id = _props.shared_props.id;
377
+ });
378
+ });
379
+ }
380
+
381
+ dispatch<E extends keyof T>(event_name: E, data?: T[E]): void {
382
+ this.dispatcher(this.shared.id, event_name, data);
383
+ }
384
+
385
+ async get_data() {
386
+ return $state.snapshot(this.props);
387
+ }
388
+
389
+ update(data: Partial<U & SharedProps>): void {
390
+ this.set_data(data as U & SharedProps);
391
+ }
392
+
393
+ set_data(data: Partial<U & SharedProps>): void {
394
+ for (const key in data) {
395
+ if (this.shared_props.includes(key as keyof SharedProps)) {
396
+ const _key = key as keyof SharedProps;
397
+ // @ts-ignore i'm not doing pointless typescript gymanstics
398
+
399
+ this.shared[_key] = data[_key];
400
+
401
+ continue;
402
+
403
+ // @ts-ignore same here
404
+ } else {
405
+ // @ts-ignore same here
406
+ this.props[key] = data[key];
407
+ }
408
+ }
409
+ }
410
+ }
411
+
412
+ // function _load_component(
413
+ // this: Gradio,
414
+ // name: string,
415
+ // variant: "component" | "example" | "base" = "component"
416
+ // ): ReturnType<component_loader> {
417
+ // return this._load_component!({
418
+ // name,
419
+ // api_url: this.shared.client.config?.root!,
420
+ // variant
421
+ // });
422
+ // }
423
+
424
+ export const css_units = (dimension_value: string | number): string => {
425
+ return typeof dimension_value === "number"
426
+ ? dimension_value + "px"
427
+ : dimension_value;
428
+ };
429
+
430
+ type MappedProps<T> = { [K in keyof T]: T[K] };
431
+ type MappedProp<T, K extends keyof T> = { [P in K]: T[P] };