fastrtc
repository·main·Indexed 26 days ago
https://github.com/gradio-app/fastrtcA real-time communication library for Python that allows developers to turn any Python function into a real-time audio or video stream over WebRTC or WebSockets. It integrates with the Gradio SDK to support various multimodal AI demos, including voice chat, object detection, and live transcription.
What's inside fastrtc
- The Integrated Textbox is a feature designed to allow users to interact with any Large Language Model (LLM) via either voice (talking) or text (typing). It leverages WebRTC and WebSockets for real-time communication and is built using the Gradio SDK.
Overview of Voice Text Editor demo
mainThe Voice Text Editor is a demonstration application that allows users to edit text documents using voice commands. It is built using thegradioSDK (version 5.16.0) and utilizes WebRTC and WebSockets for real-time interaction.Run the Llama Code Editor demo
mainThe Llama Code Editor is a demo application that allows users to create interactive HTML web pages using voice commands. It is built using thegradioSDK (version5.16.0) and requires several API keys to function. The main application logic is located inapp.py.Core Concepts of the Stream object
mainThe
Streamobject is the central component of FastRTC, used to stream audio, video, or both. It can be configured with different modes, modalities, and handlers to define how data flows between the client and server.Stream Modes
send-receive: Bidirectional streaming (default).send: Client-to-server only.receive: Server-to-client only.
Modalities
video: Video streaming.audio: Audio streaming.audio-video: Combined audio and video streaming.
Handlers
The
handleris the main argument forStream. The required type depends on the modality and mode:Modality send-receivesendreceivevideoFunction (frame $\rightarrow$ frame) Function (frame $\rightarrow$ frame) Function (frame $\rightarrow$ frame) audioStreamHandlerorAsyncStreamHandlersubclassStreamHandlerorAsyncStreamHandlersubclassGenerator yielding audio frames audio-videoAudioVideoStreamHandlerorAsyncAudioVideoStreamHandlersubclassNot Supported Yet Not Supported Yet from fastrtc import Stream import gradio as gr import numpy as np def detection(image, slider): return np.flip(image, axis=0) stream = Stream( handler=detection, # (1) modality="video", # (2) mode="send-receive", # (3) additional_inputs=[ gr.Slider(minimum=0, maximum=1, step=0.01, value=0.3) # (4) ], additional_outputs=None, # (5) additional_outputs_handler=None # (6) )Run the Object Detection demo with YOLOv10
mainThis demo uses YOLOv10 to perform real-time object detection via WebRTC. It is built using the Gradio SDK (version 5.16.0).Moonshine Live Transcription Overview
mainMoonshine Live Transcription provides real-time captions using Moonshine ONNX models. It utilizes WebRTC and WebSockets for low-latency streaming and is built using the Gradio SDK (version 5.17.0).Explore FastRTC Application Examples
mainThe FastRTC cookbook provides a collection of real-world applications demonstrating various capabilities of the library. You can explore implementations for:
- Audio & Voice Chat: Real-time conversations with LLMs (Llama, Claude, GPT-4o), speech-to-speech models (Moshi, Ultravox), and integration with providers like ElevenLabs, Groq, and OpenAI.
- Video & Computer Vision: Real-time object detection using YOLOv10 and RT-DETR on webcam streams or uploaded videos.
- Real-time APIs: Direct integration with Google Gemini, OpenAI, and Azure Realtime APIs for low-latency voice/video interactions.
- Transcription: Real-time speech-to-text using Whisper (both cloud-based via Groq and local via Transformers).
- Specialized Tools: Voice-activated code editors, stop-word detection (e.g., 'Hello Llama'), and agentic voice assistants.
Run Phonic AI Chat
mainPhonic AI Chat is a speech-to-speech application that allows users to talk to Phonic AI's model. It is built using thegradioSDK (version 5.16.0) and requires Python 3.11. The application entry point isapp.py.Configure environment variables for Voice Text Editor
mainTo run the Voice Text Editor demo, you must provide the following secret environment variables:
HF_TOKEN: Hugging Face token.SAMBANOVA_API_KEY: API key for Sambanova services.
Configure Cloudflare Calls API with Cloudflare API Token
mainIf you have exhausted your free Hugging Face quota, you can use your own Cloudflare account.
- Create a TURN App in the Cloudflare Calls dashboard.
- Obtain your
TURN_KEY_IDandTURN_KEY_API_TOKEN. - Set these as environment variables:
TURN_KEY_IDandTURN_KEY_API_TOKEN. - Pass
get_cloudflare_turn_credentials_asyncto thertc_configurationparameter of theStreamclass.
from fastrtc import Stream, get_cloudflare_turn_credentials_async # Make sure the TURN_KEY_ID and TURN_KEY_API_TOKEN environment variables are set stream = Stream( handler=..., rtc_configuration=get_cloudflare_turn_credentials_async, modality="audio", mode="send-receive", )Configure Phonic AI Chat environment variables
mainTo run the Phonic AI Chat application, you must provide the following secret environment variables:
TWILIO_ACCOUNT_SID: Your Twilio account SID.TWILIO_AUTH_TOKEN: Your Twilio authentication token.PHONIC_API_KEY: Your Phonic API key.
Run Whisper Realtime Transcription demo
mainThis demo provides realtime audio transcription using Whisper. It requires a Gradio SDK version of
5.16.0or higher and utilizes WebRTC and WebSockets for low-latency streaming. To run this application, you must provide the following environment variables/secrets:HF_TOKEN: Hugging Face token.GROQ_API_KEY: API key for Groq services.
The application entry point is
app.py.