Skip to content

Fix api event drops #6556

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

Merged
merged 44 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e91912d
changes
aliabid94 Nov 22, 2023
0092230
changes
aliabid94 Nov 23, 2023
85ae8f9
add changeset
gradio-pr-bot Nov 23, 2023
1885bb9
changes
aliabid94 Nov 24, 2023
b7d35c8
Merge branch 'fix_api_event_dops' of https://github.com/gradio-app/gr…
aliabid94 Nov 24, 2023
57c6e5b
changes
aliabid94 Nov 29, 2023
ca73780
changes
aliabid94 Nov 29, 2023
ba37ca8
changs
aliabid94 Nov 29, 2023
41eb16a
chagnes
aliabid94 Nov 29, 2023
f1d8baf
changes
Dec 5, 2023
31cf67f
changes
Dec 5, 2023
ca1d7fa
changes
Dec 5, 2023
6a66067
changes
Dec 5, 2023
4ceb71a
changes
Dec 5, 2023
edc95e4
changes
Dec 5, 2023
a7db781
changes
Dec 5, 2023
c6d2bc9
changes
Dec 5, 2023
e9db998
Merge remote-tracking branch 'origin' into fix_api_event_dops
Dec 5, 2023
8456e68
changes
Dec 5, 2023
2fa939c
changes
Dec 5, 2023
fdaa96b
changes~git push
Dec 5, 2023
f27f336
changes
Dec 6, 2023
27ad025
changes
Dec 6, 2023
8ea90a4
chagmes
Dec 6, 2023
acd1f1d
changes
Dec 7, 2023
692f2d9
changes
Dec 7, 2023
64d30bd
Merge remote-tracking branch 'origin' into fix_api_event_dops
Dec 7, 2023
1f53bf3
changes
Dec 7, 2023
9fa6d3d
changes
Dec 7, 2023
1ff6e7d
Merge branch 'main' into fix_api_event_dops
abidlabs Dec 7, 2023
448f9c4
changes
Dec 11, 2023
7dbe37c
Merge remote-tracking branch 'origin' into fix_api_event_dops
Dec 11, 2023
7f6ebe4
changes
Dec 11, 2023
5c6349b
changes
Dec 11, 2023
7cde497
Merge remote-tracking branch 'origin' into fix_api_event_dops
Dec 11, 2023
b722e46
changes
Dec 12, 2023
6e5a093
changes
Dec 12, 2023
5c598a0
changes
Dec 12, 2023
b7ef2c5
change
Dec 12, 2023
778f15b
changes
Dec 12, 2023
bba88da
changes
Dec 12, 2023
caf6786
Merge remote-tracking branch 'origin' into fix_api_event_dops
Dec 12, 2023
a23d771
changes
Dec 12, 2023
aaa63fb
changes
Dec 12, 2023
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
7 changes: 7 additions & 0 deletions .changeset/ripe-spiders-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@gradio/client": patch
"gradio": patch
"gradio_client": patch
---

fix:Fix api event drops
152 changes: 147 additions & 5 deletions client/js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ export function api_factory(

const session_hash = Math.random().toString(36).substring(2);
const last_status: Record<string, Status["stage"]> = {};
let stream_open = false;
let event_stream: EventSource | null = null;
const event_callbacks: Record<string, () => Promise<void>> = {};
let config: Config;
let api_map: Record<string, number> = {};

Expand Down Expand Up @@ -437,7 +440,7 @@ export function api_factory(

let websocket: WebSocket;
let eventSource: EventSource;
let protocol = config.protocol ?? "sse";
let protocol = config.protocol ?? "ws";

const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
let payload: Payload;
Expand Down Expand Up @@ -540,7 +543,7 @@ export function api_factory(
config.path,
true
)}
/queue/join${url_params ? "?" + url_params : ""}`);
/queue/data${url_params ? "?" + url_params : ""}`);

if (jwt) {
url.searchParams.set("__sign", jwt);
Expand Down Expand Up @@ -646,7 +649,7 @@ export function api_factory(
websocket.send(JSON.stringify({ hash: session_hash }))
);
}
} else {
} else if (protocol == "sse") {
fire_event({
type: "status",
stage: "pending",
Expand All @@ -664,7 +667,7 @@ export function api_factory(
host,
config.path,
true
)}/queue/join?${url_params ? url_params + "&" : ""}${params}`
)}/queue/data?${url_params ? url_params + "&" : ""}${params}`
);

eventSource = new EventSource(url);
Expand Down Expand Up @@ -695,7 +698,7 @@ export function api_factory(
host,
config.path,
true
)}/queue/data`,
)}/queue/join`,
{
...payload,
session_hash,
Expand Down Expand Up @@ -766,6 +769,121 @@ export function api_factory(
}
}
};
} else if (protocol == "sse_v1") {
fire_event({
type: "status",
stage: "pending",
queue: true,
endpoint: _endpoint,
fn_index,
time: new Date()
});

post_data(
`${http_protocol}//${resolve_root(
host,
config.path,
true
)}/queue/join?${url_params}`,
{
...payload,
session_hash
},
hf_token
).then(([response, status]) => {
if (status !== 200) {
fire_event({
type: "status",
stage: "error",
message: BROKEN_CONNECTION_MSG,
queue: true,
endpoint: _endpoint,
fn_index,
time: new Date()
});
} else {
event_id = response.event_id as string;
if (!stream_open) {
open_stream();
}

let callback = async function (_data: object): void {
const { type, status, data } = handle_message(
_data,
last_status[fn_index]
);

if (type === "update" && status && !complete) {
// call 'status' listeners
fire_event({
type: "status",
endpoint: _endpoint,
fn_index,
time: new Date(),
...status
});
} else if (type === "complete") {
complete = status;
} else if (type === "log") {
fire_event({
type: "log",
log: data.log,
level: data.level,
endpoint: _endpoint,
fn_index
});
} else if (type === "generating") {
fire_event({
type: "status",
time: new Date(),
...status,
stage: status?.stage!,
queue: true,
endpoint: _endpoint,
fn_index
});
}
if (data) {
fire_event({
type: "data",
time: new Date(),
data: transform_files
? transform_output(
data.data,
api_info,
config.root,
config.root_url
)
: data.data,
endpoint: _endpoint,
fn_index
});

if (complete) {
fire_event({
type: "status",
time: new Date(),
...complete,
stage: status?.stage!,
queue: true,
endpoint: _endpoint,
fn_index
});
}
}

if (status.stage === "complete" || status.stage === "error") {
if (event_callbacks[event_id]) {
delete event_callbacks[event_id];
if (Object.keys(event_callbacks).length === 0) {
close_stream();
}
}
}
};
event_callbacks[event_id] = callback;
}
});
}
});

Expand Down Expand Up @@ -864,6 +982,30 @@ export function api_factory(
};
}

function open_stream(): void {
stream_open = true;
let params = new URLSearchParams({
session_hash: session_hash
}).toString();
let url = new URL(
`${http_protocol}//${resolve_root(
host,
config.path,
true
)}/queue/data?${params}`
);
event_stream = new EventSource(url);
event_stream.onmessage = async function (event) {
let _data = JSON.parse(event.data);
await event_callbacks[_data.event_id](_data);
};
}

function close_stream(): void {
stream_open = false;
event_stream?.close();
}

async function component_server(
component_id: number,
fn_name: string,
Expand Down
2 changes: 1 addition & 1 deletion client/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Config {
show_api: boolean;
stylesheets: string[];
path: string;
protocol?: "sse" | "ws";
protocol?: "sse_v1" | "sse" | "ws";
}

export interface Payload {
Expand Down
Loading