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

rework upload to be a class method + pass client into each component #8179

Merged
merged 10 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .changeset/huge-rivers-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@gradio/app": patch
"@gradio/audio": patch
"@gradio/client": patch
"@gradio/dataframe": patch
"@gradio/file": patch
"@gradio/gallery": patch
"@gradio/image": patch
"@gradio/imageeditor": patch
"@gradio/model3d": patch
"@gradio/multimodaltextbox": patch
"@gradio/simpleimage": patch
"@gradio/storybook": patch
"@gradio/tootils": patch
"@gradio/upload": patch
"@gradio/uploadbutton": patch
"@gradio/utils": patch
"@gradio/video": patch
"gradio": patch
---

fix:rework upload to be a class method + pass client into each component
11 changes: 10 additions & 1 deletion client/js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
} from "./types";
import { view_api } from "./utils/view_api";
import { upload_files } from "./utils/upload_files";
import { upload, FileData } from "./upload";
import { handle_blob } from "./utils/handle_blob";
import { post_data } from "./utils/post_data";
import { predict } from "./utils/predict";
Expand Down Expand Up @@ -60,10 +61,11 @@ export class Client {
return fetch(input, init);
}

eventSource_factory(url: URL): EventSource | null {
eventSource_factory(url: URL): EventSource {
if (typeof window !== undefined && typeof EventSource !== "undefined") {
return new EventSource(url.toString());
}
// @ts-ignore
return null; // todo: polyfill eventsource for node envs
}
Comment on lines +65 to 71
Copy link
Member Author

Choose a reason for hiding this comment

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

This tying is temporary but was causing problems for some components. Ignored it until we polyfill.


Expand All @@ -73,6 +75,12 @@ export class Client {
files: (Blob | File)[],
upload_id?: string
) => Promise<UploadResponse>;
upload: (
file_data: FileData[],
root_url: string,
upload_id?: string,
max_file_size?: number
) => Promise<(FileData | null)[] | null>;
handle_blob: (
endpoint: string,
data: unknown[],
Expand Down Expand Up @@ -108,6 +116,7 @@ export class Client {
this.predict = predict.bind(this);
this.open_stream = open_stream.bind(this);
this.resolve_config = resolve_config.bind(this);
this.upload = upload.bind(this);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the main change.

}

private async init(): Promise<void> {
Expand Down
12 changes: 4 additions & 8 deletions client/js/src/upload.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import type { UploadResponse } from "./types";
import { upload_files } from ".";
import type { Client } from "./client";

export async function upload(
this: Client,
file_data: FileData[],
root_url: string,
upload_id?: string,
max_file_size?: number,
upload_fn: (
root_url: string,
files: (Blob | File)[],
upload_id?: string
) => Promise<UploadResponse> = upload_files
max_file_size?: number
): Promise<(FileData | null)[] | null> {
let files = (Array.isArray(file_data) ? file_data : [file_data]).map(
(file_data) => file_data.blob!
Expand All @@ -28,7 +24,7 @@ export async function upload(
}

return await Promise.all(
await upload_fn(root_url, files, upload_id).then(
await this.upload_files(root_url, files, upload_id).then(
async (response: { files?: string[]; error?: string }) => {
if (response.error) {
throw new Error(response.error);
Expand Down
3 changes: 2 additions & 1 deletion client/js/src/utils/upload_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export async function upload_files(
const headers: {
Authorization?: string;
} = {};
if (this.options.hf_token) {
if (this?.options?.hf_token) {
headers.Authorization = `Bearer ${this.options.hf_token}`;
}

const chunkSize = 1000;
const uploadResponses = [];
for (let i = 0; i < files.length; i += chunkSize) {
Expand Down
4 changes: 1 addition & 3 deletions js/app/src/Blocks.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { tick } from "svelte";
import { _ } from "svelte-i18n";
import { Client } from "@gradio/client";
import { setContext } from "svelte";

import type { LoadingStatus, LoadingStatusCollection } from "./stores";

Expand Down Expand Up @@ -522,8 +521,6 @@
function isCustomEvent(event: Event): event is CustomEvent {
return "detail" in event;
}

setContext("upload_files", app.upload_files);
Copy link
Member Author

Choose a reason for hiding this comment

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

ANd we don't need this for the same reason.

</script>

<svelte:head>
Expand Down Expand Up @@ -562,6 +559,7 @@
{version}
{autoscroll}
max_file_size={app.config.max_file_size}
client={app}
/>
{/if}
</div>
Expand Down
7 changes: 1 addition & 6 deletions js/app/src/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</script>

<script lang="ts">
import { onMount, setContext, createEventDispatcher } from "svelte";
import { onMount, createEventDispatcher } from "svelte";
import type { SpaceStatus } from "@gradio/client";
import Embed from "./Embed.svelte";
import type { ThemeMode } from "./types";
Expand Down Expand Up @@ -100,11 +100,6 @@
loading_text = (event as CustomEvent).detail + "...";
});
}
export let fetch_implementation: typeof fetch = fetch;
setContext("fetch_implementation", fetch_implementation);
export let EventSource_factory: (url: URL) => EventSource = (url) =>
new EventSource(url);
setContext("EventSource_factory", EventSource_factory);
Comment on lines -103 to -107
Copy link
Member Author

Choose a reason for hiding this comment

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

Because we use client.x() everywhere now, we don't need this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

much cleaner


export let space: string | null;
export let host: string | null;
Expand Down
3 changes: 3 additions & 0 deletions js/app/src/MountComponents.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { onMount, createEventDispatcher } from "svelte";
import type { Client } from "@gradio/client";
import Render from "./Render.svelte";

export let rootNode: any;
Expand All @@ -9,6 +10,7 @@
export let version: any;
export let autoscroll: boolean;
export let max_file_size: number | null = null;
export let client: Client;

const dispatch = createEventDispatcher<{ mount?: never }>();
onMount(() => {
Expand All @@ -24,4 +26,5 @@
{version}
{autoscroll}
{max_file_size}
{client}
/>
13 changes: 5 additions & 8 deletions js/app/src/Render.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Gradio, formatter } from "./gradio_helper";
import { onMount, createEventDispatcher, setContext } from "svelte";
import type { ComponentMeta, ThemeMode } from "./types";
import type { Client } from "@gradio/client";
import RenderComponent from "./RenderComponent.svelte";

export let root: string;
Expand All @@ -13,6 +14,7 @@
export let version: string;
export let autoscroll: boolean;
export let max_file_size: number | null;
export let client: Client;

const dispatch = createEventDispatcher<{ mount: number; destroy: number }>();
let filtered_children: ComponentMeta[] = [];
Expand Down Expand Up @@ -45,12 +47,6 @@

setContext("BLOCK_KEY", parent);

function handle_prop_change(e: { detail: Record<string, any> }): void {
// for (const k in e.detail) {
// instance_map[id].props[k] = e.detail[k];
// }
}

$: {
Copy link
Member Author

Choose a reason for hiding this comment

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

This was an unused leftover.

if (node.type === "form") {
if (node.children?.every((c) => !c.props.visible)) {
Expand All @@ -70,7 +66,6 @@
elem_id={("elem_id" in node.props && node.props.elem_id) ||
`component-${node.id}`}
elem_classes={("elem_classes" in node.props && node.props.elem_classes) || []}
on:prop_change={handle_prop_change}
{target}
{...node.props}
{theme_mode}
Expand All @@ -83,7 +78,8 @@
root,
autoscroll,
max_file_size,
formatter
formatter,
client
Comment on lines +81 to +82
Copy link
Member Author

Choose a reason for hiding this comment

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

We pass the clients into the components here.

)}
>
{#if node.children && node.children.length}
Expand All @@ -98,6 +94,7 @@
on:destroy
on:mount
{max_file_size}
{client}
/>
{/each}
{/if}
Expand Down
6 changes: 1 addition & 5 deletions js/app/src/lite/Playground.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
export let mount_css: typeof default_mount_css = default_mount_css;
export let Client: typeof ClientType;
export let worker_proxy: WorkerProxy | undefined = undefined;
export let fetch_implementation: typeof fetch = fetch;
export let EventSource_factory: (url: URL) => EventSource = (url) =>
new EventSource(url);

export let space: string | null;
export let host: string | null;
export let src: string | null;
Expand Down Expand Up @@ -217,8 +215,6 @@
{mount_css}
{Client}
bind:worker_proxy
{fetch_implementation}
{EventSource_factory}
{space}
{host}
{src}
Expand Down
9 changes: 0 additions & 9 deletions js/app/src/lite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,6 @@ export function create(options: Options): GradioAppController {

mount_prebuilt_css(document.head);

const overridden_fetch: typeof fetch = (input, init?) => {
return wasm_proxied_fetch(worker_proxy, input, init);
};
const EventSource_factory = (url: URL): EventSource => {
return wasm_proxied_EventSource_factory(worker_proxy, url);
};

Comment on lines -129 to -135
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need this any more, we just use the client everywhere.

class LiteClient extends Client {
fetch_implementation(
input: RequestInfo | URL,
Expand Down Expand Up @@ -223,8 +216,6 @@ export function create(options: Options): GradioAppController {
worker_proxy,
Client: LiteClient,
mount_css: overridden_mount_css,
fetch_implementation: overridden_fetch,
EventSource_factory,
// For playground
layout: options.layout
};
Expand Down
2 changes: 2 additions & 0 deletions js/audio/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@
{waveform_settings}
{waveform_options}
{trim_region_settings}
upload={gradio.client.upload}
stream_handler={gradio.client.eventSource_factory}
Comment on lines +228 to +229
Copy link
Member Author

Choose a reason for hiding this comment

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

Client available on gradio namespace. Rest of the code is just more of this really.

>
<UploadText i18n={gradio.i18n} type="audio" />
</InteractiveAudio>
Expand Down
17 changes: 9 additions & 8 deletions js/audio/interactive/InteractiveAudio.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import { getContext, onDestroy, createEventDispatcher } from "svelte";
pngwn marked this conversation as resolved.
Show resolved Hide resolved
import { Upload, ModifyUpload } from "@gradio/upload";
import {
upload,
prepare_files,
type FileData,
type upload_files
type Client,
type upload
pngwn marked this conversation as resolved.
Show resolved Hide resolved
} from "@gradio/client";
import { BlockLabel } from "@gradio/atoms";
import { Music } from "@gradio/icons";
Expand Down Expand Up @@ -39,9 +39,8 @@
export let handle_reset_value: () => void = () => {};
export let editable = true;
export let max_file_size: number | null = null;

// Needed for wasm support
const upload_fn = getContext<typeof upload_files>("upload_files");
export let upload: Client["upload"];
export let stream_handler: Client["eventSource_factory"];

$: dispatch("drag", dragging);

Expand Down Expand Up @@ -98,9 +97,9 @@
let _audio_blob = new File(blobs, "audio.wav");
const val = await prepare_files([_audio_blob], event === "stream");
value = (
(
await upload(val, root, undefined, max_file_size ?? Infinity, upload_fn)
)?.filter(Boolean) as FileData[]
(await upload(val, root, undefined, max_file_size ?? Infinity))?.filter(
pngwn marked this conversation as resolved.
Show resolved Hide resolved
Boolean
) as FileData[]
)[0];

dispatch(event, value);
Expand Down Expand Up @@ -257,6 +256,8 @@
on:error={({ detail }) => dispatch("error", detail)}
{root}
{max_file_size}
{upload}
{stream_handler}
>
<slot />
</Upload>
Expand Down
2 changes: 2 additions & 0 deletions js/dataframe/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,7 @@
i18n={gradio.i18n}
{line_breaks}
{column_widths}
upload={gradio.client.upload}
stream_handler={gradio.client.eventSource_factory}
/>
</Block>
1 change: 1 addition & 0 deletions js/dataframe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@gradio/atoms": "workspace:^",
"@gradio/button": "workspace:^",
"@gradio/client": "workspace:^",
"@gradio/markdown": "workspace:^",
"@gradio/statustracker": "workspace:^",
"@gradio/upload": "workspace:^",
Expand Down
5 changes: 5 additions & 0 deletions js/dataframe/shared/Table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import EditableCell from "./EditableCell.svelte";
import type { SelectData } from "@gradio/utils";
import type { I18nFormatter } from "js/app/src/gradio_helper";
import { type Client } from "@gradio/client";
import VirtualTable from "./VirtualTable.svelte";
import type {
Headers,
Expand Down Expand Up @@ -38,6 +39,8 @@
export let height = 500;
export let line_breaks = true;
export let column_widths: string[] = [];
export let upload: Client["upload"];
export let stream_handler: Client["eventSource_factory"];

let selected: false | [number, number] = false;
export let display_value: string[][] | null = null;
Expand Down Expand Up @@ -722,6 +725,8 @@
</tbody>
</table>
<Upload
{upload}
{stream_handler}
flex={false}
center={false}
boundedheight={false}
Expand Down
2 changes: 2 additions & 0 deletions js/file/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
/>
{:else}
<FileUpload
upload={gradio.client.upload}
stream_handler={gradio.client.eventSource_factory}
{label}
{show_label}
{value}
Expand Down
Loading