Use client.submit() with for-await to stream data events
Browse filesGradio JS client exposes streaming via submit() returning an async
iterator, not a stream() event emitter. Iterate over msg.type=='data'
events to receive partial chunks.
Co-Authored-By: Claude <noreply@anthropic.com>
- index.html +5 -9
index.html
CHANGED
|
@@ -350,13 +350,14 @@ async function submit() {
|
|
| 350 |
let fullText = "";
|
| 351 |
let renderQueued = false;
|
| 352 |
|
| 353 |
-
const
|
| 354 |
message: gradioMsg,
|
| 355 |
history: history.slice(0, -1),
|
| 356 |
});
|
| 357 |
|
| 358 |
-
|
| 359 |
-
|
|
|
|
| 360 |
const txt = typeof data === "object" ? (data?.content || "") : (data || "");
|
| 361 |
if (txt) {
|
| 362 |
fullText = txt;
|
|
@@ -368,12 +369,7 @@ async function submit() {
|
|
| 368 |
});
|
| 369 |
}
|
| 370 |
}
|
| 371 |
-
}
|
| 372 |
-
|
| 373 |
-
await new Promise((resolve, reject) => {
|
| 374 |
-
stream.on("end", resolve);
|
| 375 |
-
stream.on("error", reject);
|
| 376 |
-
});
|
| 377 |
|
| 378 |
history.push({ role: "assistant", content: fullText });
|
| 379 |
} catch (err) {
|
|
|
|
| 350 |
let fullText = "";
|
| 351 |
let renderQueued = false;
|
| 352 |
|
| 353 |
+
const submission = client.submit("/chat", {
|
| 354 |
message: gradioMsg,
|
| 355 |
history: history.slice(0, -1),
|
| 356 |
});
|
| 357 |
|
| 358 |
+
for await (const msg of submission) {
|
| 359 |
+
if (msg.type !== "data") continue;
|
| 360 |
+
const data = msg?.data?.[0];
|
| 361 |
const txt = typeof data === "object" ? (data?.content || "") : (data || "");
|
| 362 |
if (txt) {
|
| 363 |
fullText = txt;
|
|
|
|
| 369 |
});
|
| 370 |
}
|
| 371 |
}
|
| 372 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
history.push({ role: "assistant", content: fullText });
|
| 375 |
} catch (err) {
|