akhaliq HF Staff Claude commited on
Commit
54d25fe
·
1 Parent(s): 0322825

Use client.submit() with for-await to stream data events

Browse files

Gradio 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>

Files changed (1) hide show
  1. index.html +5 -9
index.html CHANGED
@@ -350,13 +350,14 @@ async function submit() {
350
  let fullText = "";
351
  let renderQueued = false;
352
 
353
- const stream = client.stream("/chat", {
354
  message: gradioMsg,
355
  history: history.slice(0, -1),
356
  });
357
 
358
- stream.on("data", (chunk) => {
359
- const data = chunk?.data?.[0];
 
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) {