diff --git a/.changeset/huge-rivers-cough.md b/.changeset/huge-rivers-cough.md new file mode 100644 index 0000000000..17b452c1a1 --- /dev/null +++ b/.changeset/huge-rivers-cough.md @@ -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 diff --git a/client/js/src/client.ts b/client/js/src/client.ts index 1bff814611..83dac4eb64 100644 --- a/client/js/src/client.ts +++ b/client/js/src/client.ts @@ -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"; @@ -61,10 +62,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 } @@ -74,6 +76,12 @@ export class Client { files: (Blob | File)[], upload_id?: string ) => Promise; + upload: ( + file_data: FileData[], + root_url: string, + upload_id?: string, + max_file_size?: number + ) => Promise<(FileData | null)[] | null>; handle_blob: ( endpoint: string, data: unknown[], @@ -109,6 +117,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); } private async init(): Promise { diff --git a/client/js/src/upload.ts b/client/js/src/upload.ts index b40c6ff68a..0dfe714f7d 100644 --- a/client/js/src/upload.ts +++ b/client/js/src/upload.ts @@ -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 = 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! @@ -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); diff --git a/client/js/src/utils/upload_files.ts b/client/js/src/utils/upload_files.ts index dae40ff4d0..1f0a47c759 100644 --- a/client/js/src/utils/upload_files.ts +++ b/client/js/src/utils/upload_files.ts @@ -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 = []; let response: Response; diff --git a/js/app/src/Blocks.svelte b/js/app/src/Blocks.svelte index d22b0e673f..f287005770 100644 --- a/js/app/src/Blocks.svelte +++ b/js/app/src/Blocks.svelte @@ -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"; @@ -522,8 +521,6 @@ function isCustomEvent(event: Event): event is CustomEvent { return "detail" in event; } - - setContext("upload_files", app.upload_files); @@ -562,6 +559,7 @@ {version} {autoscroll} max_file_size={app.config.max_file_size} + client={app} /> {/if} diff --git a/js/app/src/Index.svelte b/js/app/src/Index.svelte index 7ab1833829..e366460493 100644 --- a/js/app/src/Index.svelte +++ b/js/app/src/Index.svelte @@ -63,7 +63,7 @@