Files
gradio-webrtc/docs/userguide/api.md
Freddy Boulton 43e42c1b22 Tidy up connection logic (#90)
* Add code:

* code

* code

---------

Co-authored-by: Freddy Boulton <freddyboulton@hf-freddy.local>
2025-02-26 18:21:26 -05:00

17 KiB

Connecting via API

Before continuing, select the modality, mode of your Stream and whether you're using WebRTC or WebSockets.

WebRTC WebSocket
Audio Video Audio-Video
Send-Receive Receive Send

Sample Code

Message Format

Over both WebRTC and WebSocket, the server can send messages of the following format:

{
    "type": `send_input` | `fetch_output` | `stopword` | `error` | `warning` | `log`,
    "data": string | object
}
  • send_input: Send any input data for the handler to the server. See Additional Inputs for more details.
  • fetch_output: An instance of AdditionalOutputs is sent to the server.
  • stopword: The stopword has been detected. See ReplyOnStopWords for more details.
  • error: An error occurred. The data will be a string containing the error message.
  • warning: A warning occurred. The data will be a string containing the warning message.
  • log: A log message. The data will be a string containing the log message.

The ReplyOnPause handler can also send the following log messages.

{
    "type": "log",
    "data": "pause_detected" | "response_starting"
}

!!! tip When using WebRTC, the messages will be encoded as strings, so parse as JSON before using.

Additional Inputs

When the send_input message is received, update the inputs of your handler however you like by using the set_input method of the Stream object.

A common pattern is to use a POST request to send the updated data. The first argument to the set_input method is the webrtc_id of the handler.

from pydantic import BaseModel, Field

class InputData(BaseModel):
    webrtc_id: str
    conf_threshold: float = Field(ge=0, le=1)


@app.post("/input_hook")
async def _(data: InputData):
    stream.set_input(data.webrtc_id, data.conf_threshold)

The updated data will be passed to the handler on the next call.

Additional Outputs

The fetch_output message is sent to the client whenever an instance of AdditionalOutputs is available. You can access the latest output data by calling the fetch_latest_output method of the Stream object.

However, rather than fetching each output manually, a common pattern is to fetch the entire stream of output data by calling the output_stream method.

Here is an example:

from fastapi.responses import StreamingResponse

@app.get("/updates")
async def stream_updates(webrtc_id: str):
    async def output_stream():
        async for output in stream.output_stream(webrtc_id):
            # Output is the AdditionalOutputs instance
            # Be sure to serialize it however you would like
            yield f"data: {output.args[0]}\n\n"

    return StreamingResponse(
        output_stream(), 
        media_type="text/event-stream"
    )

Handling Errors

When connecting via WebRTC, the server will respond to the /webrtc/offer route with a JSON response. If there are too many connections, the server will respond with a 200 error.

{
    "status": "failed",
    "meta": {
        "error": "concurrency_limit_reached",
        "limit": 10
    }

Over WebSocket, the server will send the same message before closing the connection.

!!! tip The server will sends a 200 status code because otherwise the gradio client will not be able to process the json response and display the error.

<style> .config-selector { margin: 1em 0; display: flex; gap: 2em; } .select-group { display: flex; flex-direction: column; gap: 0.5em; } .select-group label { font-size: 0.8em; font-weight: 600; color: var(--md-default-fg-color--light); } .select-group select { padding: 0.5em; border: 1px solid var(--md-default-fg-color--lighter); border-radius: 4px; background-color: var(--md-code-bg-color); color: var(--md-code-fg-color); width: 150px; font-size: 0.9em; } /* Style code blocks to match site theme */ .rendered-content pre { background-color: var(--md-code-bg-color) !important; color: var(--md-code-fg-color) !important; padding: 1em; border-radius: 4px; } .rendered-content code { font-family: var(--md-code-font-family); background-color: var(--md-code-bg-color) !important; color: var(--md-code-fg-color) !important; } </style> <script> // doT.js // 2011-2014, Laura Doktorova, https://github.com/olado/doT // Licensed under the MIT license. var doT = { name: "doT", version: "1.1.1", templateSettings: { evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g, interpolate: /\{\{=([\s\S]+?)\}\}/g, encode: /\{\{!([\s\S]+?)\}\}/g, use: /\{\{#([\s\S]+?)\}\}/g, useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g, define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g, defineParams: /^\s*([\w$]+):([\s\S]+)/, conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g, iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g, varname: "it", strip: false, append: true, selfcontained: false, doNotSkipEncoded: false }, template: undefined, //fn, compile template compile: undefined, //fn, for express log: true }, _globals; doT.encodeHTMLSource = function (doNotSkipEncoded) { var encodeHTMLRules = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'", "/": "/" }, matchHTML = doNotSkipEncoded ? /[&<>"'\/]/g : /&(?!#?\w+;)|<|>|"|'|\//g; return function (code) { return code ? code.toString().replace(matchHTML, function (m) { return encodeHTMLRules[m] || m; }) : ""; }; }; _globals = (function () { return this || (0, eval)("this"); }()); /* istanbul ignore else */ if (typeof module !== "undefined" && module.exports) { module.exports = doT; } else if (typeof define === "function" && define.amd) { define(function () { return doT; }); } else { _globals.doT = doT; } var startend = { append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" }, split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML(" } }, skip = /$^/; function resolveDefs(c, block, def) { return ((typeof block === "string") ? block : block.toString()) .replace(c.define || skip, function (m, code, assign, value) { if (code.indexOf("def.") === 0) { code = code.substring(4); } if (!(code in def)) { if (assign === ":") { if (c.defineParams) value.replace(c.defineParams, function (m, param, v) { def[code] = { arg: param, text: v }; }); if (!(code in def)) def[code] = value; } else { new Function("def", "def['" + code + "']=" + value)(def); } } return ""; }) .replace(c.use || skip, function (m, code) { if (c.useParams) code = code.replace(c.useParams, function (m, s, d, param) { if (def[d] && def[d].arg && param) { var rw = (d + ":" + param).replace(/'|\\/g, "_"); def.__exp = def.__exp || {}; def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2"); return s + "def.__exp['" + rw + "']"; } }); var v = new Function("def", "return " + code)(def); return v ? resolveDefs(c, v, def) : v; }); } function unescape(code) { return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " "); } doT.template = function (tmpl, c, def) { c = c || doT.templateSettings; var cse = c.append ? startend.append : startend.split, needhtmlencode, sid = 0, indv, str = (c.use || c.define) ? resolveDefs(c, tmpl, def || {}) : tmpl; str = ("var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g, " ") .replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g, "") : str) .replace(/'|\\/g, "\\$&") .replace(c.interpolate || skip, function (m, code) { return cse.start + unescape(code) + cse.end; }) .replace(c.encode || skip, function (m, code) { needhtmlencode = true; return cse.startencode + unescape(code) + cse.end; }) .replace(c.conditional || skip, function (m, elsecase, code) { return elsecase ? (code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") : (code ? "';if(" + unescape(code) + "){out+='" : "';}out+='"); }) .replace(c.iterate || skip, function (m, iterate, vname, iname) { if (!iterate) return "';} } out+='"; sid += 1; indv = iname || "i" + sid; iterate = unescape(iterate); return "';var arr" + sid + "=" + iterate + ";if(arr" + sid + "){var " + vname + "," + indv + "=-1,l" + sid + "=arr" + sid + ".length-1;while(" + indv + "\`{{??}}an \`