Add text mode (#321)

* Pretty good spot

* Working draft

* Fix other mode

* Add js to git

* Working

* Add code

* fix

* Fix

* Add code

* Fix submit race condition

* demo

* fix

* Fix

* Fix
This commit is contained in:
Freddy Boulton
2025-06-03 19:24:21 -04:00
committed by GitHub
parent 1179f8ef21
commit 1877720231
69 changed files with 110161 additions and 22889 deletions

View File

@@ -42,6 +42,7 @@ from fastrtc.utils import (
AdditionalOutputs,
Context,
RTCConfigurationCallable,
WebRTCData,
create_message,
webrtc_error_handler,
)
@@ -151,6 +152,20 @@ class WebRTCConnectionMixin:
for conn in self.connections[webrtc_id]:
conn.set_args(list(args))
def set_input_gradio(self, webrtc_data: WebRTCData | str, *args):
webrtc_id = webrtc_data
if isinstance(webrtc_data, WebRTCData):
webrtc_id = webrtc_data.webrtc_id
self.set_input(cast(str, webrtc_id), webrtc_data, *args)
def set_input_on_submit(self, webrtc_data: WebRTCData | str, *args):
webrtc_id = webrtc_data
if isinstance(webrtc_data, WebRTCData):
webrtc_id = webrtc_data.webrtc_id
self.set_input(cast(str, webrtc_id), webrtc_data, *args)
if hasattr(self.handlers[cast(str, webrtc_id)], "trigger_response"):
self.handlers[cast(str, webrtc_id)].trigger_response() # type: ignore
async def output_stream(
self, webrtc_id: str
) -> AsyncGenerator[AdditionalOutputs, None]:
@@ -182,6 +197,19 @@ class WebRTCConnectionMixin:
else:
return cast(dict[str, Any], self.rtc_configuration) or {}
async def _trigger_response(self, webrtc_id: str, args: list[Any] | None = None):
from fastrtc import ReplyOnPause
if webrtc_id in self.connections and isinstance(
self.handlers[webrtc_id], ReplyOnPause
):
if args:
cast(ReplyOnPause, self.handlers[webrtc_id]).set_args(args)
cast(ReplyOnPause, self.handlers[webrtc_id]).trigger_response()
return {"status": "success"}
else:
return {"status": "failed", "meta": {"error": "not_a_reply_on_pause"}}
async def handle_offer(self, body, set_outputs):
logger.debug("Starting to handle offer")
logger.debug("Offer body %s", body)