mirror of
https://github.com/HumanAIGC-Engineering/gradio-webrtc.git
synced 2026-02-05 18:09:23 +08:00
Fix async echo example (#118)
* Fix async example * Version bump --------- Co-authored-by: Freddy Boulton <freddyboulton@hf-freddy.local>
This commit is contained in:
@@ -35,6 +35,7 @@ from numpy import typing as npt
|
|||||||
from fastrtc.utils import (
|
from fastrtc.utils import (
|
||||||
AdditionalOutputs,
|
AdditionalOutputs,
|
||||||
DataChannel,
|
DataChannel,
|
||||||
|
WebRTCError,
|
||||||
create_message,
|
create_message,
|
||||||
current_channel,
|
current_channel,
|
||||||
player_worker_decode,
|
player_worker_decode,
|
||||||
@@ -459,6 +460,11 @@ class AudioCallback(AudioStreamTrack):
|
|||||||
if isinstance(self.event_handler, AsyncHandler):
|
if isinstance(self.event_handler, AsyncHandler):
|
||||||
callable = self.event_handler.emit
|
callable = self.event_handler.emit
|
||||||
start_up = self.event_handler.start_up()
|
start_up = self.event_handler.start_up()
|
||||||
|
if not inspect.isawaitable(start_up):
|
||||||
|
raise WebRTCError(
|
||||||
|
"In AsyncStreamHandler, start_up must be a coroutine (async def)"
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
callable = functools.partial(
|
callable = functools.partial(
|
||||||
loop.run_in_executor, None, self.event_handler_emit
|
loop.run_in_executor, None, self.event_handler_emit
|
||||||
|
|||||||
@@ -151,30 +151,30 @@ Here is aa simple example of using `AsyncStreamHandler`:
|
|||||||
|
|
||||||
=== "Code"
|
=== "Code"
|
||||||
``` py
|
``` py
|
||||||
|
from fastrtc import AsyncStreamHandler, wait_for_item, Stream
|
||||||
from fastrtc import AsyncStreamHandler, wait_for_item
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
class AsyncEchoHandler(AsyncStreamHandler):
|
class AsyncEchoHandler(AsyncStreamHandler):
|
||||||
"""Handler for the Gemini API"""
|
"""Simple Async Echo Handler"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__(input_sample_rate=24000)
|
||||||
self.queue = asyncio.Queue()
|
self.queue = asyncio.Queue()
|
||||||
|
|
||||||
async def receive(self, frame: tuple[int, np.ndarray]) -> None:
|
async def receive(self, frame: tuple[int, np.ndarray]) -> None:
|
||||||
self.queue.put(frame)
|
await self.queue.put(frame)
|
||||||
|
|
||||||
async def emit(self) -> None: # (2)
|
async def emit(self) -> None:
|
||||||
return await wait_for_item(self.queue)
|
return await wait_for_item(self.queue)
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
return AsyncEchoHandler()
|
return AsyncEchoHandler()
|
||||||
|
|
||||||
async def shutdown(self): # (3)
|
async def shutdown(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def start_up(self) -> None: # (4)
|
async def start_up(self) -> None:
|
||||||
pass
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "fastrtc"
|
name = "fastrtc"
|
||||||
version = "0.0.9"
|
version = "0.0.10"
|
||||||
description = "The realtime communication library for Python"
|
description = "The realtime communication library for Python"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = "apache-2.0"
|
license = "apache-2.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user