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

ensure @gradio/client always returns the correct data #8716

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/rare-frogs-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/client": patch
"gradio": patch
---

fix:ensure `@gradio/client` always returns the correct data
7 changes: 4 additions & 3 deletions client/js/src/helpers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ export function handle_payload(

let updated_payload: unknown[] = [];
let payload_index = 0;
for (let i = 0; i < dependency.inputs.length; i++) {
const input_id = dependency.inputs[i];
const deps = type === "input" ? dependency.inputs : dependency.outputs;
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 just need to check which dependencies we care about here and set it accordingly, when handling inputs we use dependency.inputs when handling outputs we use dependency.outputs.

for (let i = 0; i < deps.length; i++) {
const input_id = deps[i];
const component = components.find((c) => c.id === input_id);

if (component?.type === "state") {
// input + with_null_state needs us to fill state with null values
if (with_null_state) {
if (resolved_payload.length === dependency.inputs.length) {
if (resolved_payload.length === deps.length) {
const value = resolved_payload[payload_index];
updated_payload.push(value);
payload_index++;
Expand Down
7 changes: 4 additions & 3 deletions client/js/src/test/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe("handle_payload", () => {
it("should return an output payload without the state component value when with_null_state is false", () => {
const resolved_payload = ["hello", null];
const dependency = {
inputs: [2, 3]
outputs: [2, 3]
};
const components = [
{ id: 2, type: "textbox" },
Expand All @@ -383,7 +383,7 @@ describe("handle_payload", () => {
it("should return an ouput payload without the two state component values when with_null_state is false", () => {
const resolved_payload = ["hello", null, "world", null];
const dependency = {
inputs: [2, 3, 4, 5]
outputs: [2, 3, 4, 5]
};
const components = [
{ id: 2, type: "textbox" },
Expand All @@ -406,7 +406,7 @@ describe("handle_payload", () => {
it("should return an ouput payload with the two state component values when with_null_state is true", () => {
const resolved_payload = ["hello", null, "world", null];
const dependency = {
inputs: [2, 3, 4, 5]
outputs: [2, 3, 4, 5]
};
const components = [
{ id: 2, type: "textbox" },
Expand Down Expand Up @@ -441,6 +441,7 @@ describe("handle_payload", () => {
// @ts-ignore
dependency,
components,
"input",
with_null_state
);
expect(result).toEqual(["hello", "world"]);
Expand Down
5 changes: 4 additions & 1 deletion client/js/src/utils/handle_blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export async function handle_blob(
path,
file_url,
type,
name: blob instanceof File ? blob?.name : undefined
name:
typeof File !== "undefined" && blob instanceof File
? blob?.name
: undefined
Comment on lines +44 to +47
Copy link
Member Author

Choose a reason for hiding this comment

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

I was getting an error about File not being defined. This check fixes it.

};
})
);
Expand Down
30 changes: 0 additions & 30 deletions js/app/test/image_remote_url.spec.ts

This file was deleted.