-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 4 commits
dd024fa
29e61e2
f58afc2
fe847b8
93b15ea
fecfcfa
b59891f
3f288ba
e4f0f31
a20e4c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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[], | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main change. |
||
} | ||
|
||
private async init(): Promise<void> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
@@ -562,6 +559,7 @@ | |
{version} | ||
{autoscroll} | ||
max_file_size={app.config.max_file_size} | ||
client={app} | ||
/> | ||
{/if} | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. much cleaner |
||
|
||
export let space: string | null; | ||
export let host: string | null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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[] = []; | ||
|
@@ -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]; | ||
// } | ||
} | ||
|
||
$: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
|
@@ -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} | ||
|
@@ -83,7 +78,8 @@ | |
root, | ||
autoscroll, | ||
max_file_size, | ||
formatter | ||
formatter, | ||
client | ||
Comment on lines
+81
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
|
@@ -98,6 +94,7 @@ | |
on:destroy | ||
on:mount | ||
{max_file_size} | ||
{client} | ||
/> | ||
{/each} | ||
{/if} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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 | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
There was a problem hiding this comment.
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.