Skip to main content

The compact version of FastRTC, the realtime communication library for Python

Project description

FastRTC-Compact

A lean, Gradio-free fork of FastRTC.

GitHub

Turn any Python function into a real-time audio and video stream over WebRTC or WebSockets — without the Gradio, librosa and their dependencies baggage of upstream.

This is a fork. It tracks FastRTC v0.0.34 and strips everything not needed for a production, bring-your-own-frontend deployment. The public Stream API and the fastrtc import path are unchanged, so code that mounts a stream on FastAPI works as-is.


What's different from upstream

Removed / out of scope

  • Gradio — the entire auto-UI (.ui.launch()), the Gradio WebRTC component, and the bundled Svelte / compiled frontend assets.
  • librosa + numba + llvmlite — replaced by soxr for audio resampling. Same resampler quality (soxr_hq), roughly 400 MB lighter, and no one-time JIT compilation stall on the first call.
  • fastphone() — the free temporary phone number (HF token + Gradio tunneling) has been removed.
  • HuggingFace Spaces toolingupload_space.py and related helpers.

Kept

  • WebRTC and WebSocket endpoints via .mount(app).
  • Voice Activity Detection and turn-taking (ReplyOnPause, optional vad extra).
  • Optional TTS / STT / stop-word extras.

The result is a much smaller dependency tree and image footprint, suitable for packaging as a plain FastAPI / WebRTC library.


Installation

This fork is distributed from git (it is not published to PyPI under this name):

pip install "git+https://github.com/abdurrafay0610/FastRTC-Compact.git"

To use built-in pause detection (see ReplyOnPause) and text to speech (see Text To Speech), add the vad and tts extras:

pip install "fastrtc-compact[vad,tts] @ git+https://github.com/abdurrafay0610/FastRTC-Compact.git"

Other optional extras: stt, stopword.

Naming: the distribution is fastrtc-compact, but the import path is unchanged — from fastrtc import Stream, ReplyOnPause. It is a drop-in replacement for code already written against FastRTC.


Key Features

  • 🗣️ Automatic voice detection & turn-taking — only worry about the logic for responding to the user; ReplyOnPause handles detecting when they've finished speaking.
  • 🔌 WebRTC support.mount(app) adds a /webrtc/offer endpoint to a FastAPI app for your own frontend.
  • ⚡️ WebSocket support — the same .mount(app) adds a /websocket/offer endpoint.
  • 🤖 Fully customizable backend — a Stream mounts onto any FastAPI app, so you can extend it to fit your production system.

Quickstart

Echo Audio

from fastrtc import Stream, ReplyOnPause
import numpy as np

def echo(audio: tuple[int, np.ndarray]):
    # The function is passed the audio until the user pauses.
    # Implement any iterator that yields audio.
    yield audio

stream = Stream(
    handler=ReplyOnPause(echo),
    modality="audio",
    mode="send-receive",
)

LLM Voice Chat

from fastrtc import (
    ReplyOnPause, Stream,
    audio_to_bytes, aggregate_bytes_to_16bit,
)
import numpy as np
from groq import Groq
import anthropic
from elevenlabs import ElevenLabs

groq_client = Groq()
claude_client = anthropic.Anthropic()
tts_client = ElevenLabs()


def response(audio: tuple[int, np.ndarray]):
    prompt = groq_client.audio.transcriptions.create(
        file=("audio-file.mp3", audio_to_bytes(audio)),
        model="whisper-large-v3-turbo",
        response_format="verbose_json",
    ).text
    reply = claude_client.messages.create(
        model="claude-3-5-haiku-20241022",
        max_tokens=512,
        messages=[{"role": "user", "content": prompt}],
    )
    response_text = " ".join(
        block.text
        for block in reply.content
        if getattr(block, "type", None) == "text"
    )
    iterator = tts_client.text_to_speech.convert_as_stream(
        text=response_text,
        voice_id="JBFqnCBsd6RMkjVDRZzb",
        model_id="eleven_multilingual_v2",
        output_format="pcm_24000",
    )
    for chunk in aggregate_bytes_to_16bit(iterator):
        audio_array = np.frombuffer(chunk, dtype=np.int16).reshape(1, -1)
        yield (24000, audio_array)

stream = Stream(
    modality="audio",
    mode="send-receive",
    handler=ReplyOnPause(response),
)

Webcam Stream

from fastrtc import Stream
import numpy as np


def flip_vertically(image):
    return np.flip(image, axis=0)


stream = Stream(
    handler=flip_vertically,
    modality="video",
    mode="send-receive",
)

Running the Stream

Mount the stream on a FastAPI app and serve your own frontend:

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastrtc import Stream, ReplyOnPause

app = FastAPI()
stream = Stream(handler=ReplyOnPause(...), modality="audio", mode="send-receive")
stream.mount(app)

# Optional: serve your frontend
@app.get("/")
async def index():
    return HTMLResponse(content=open("index.html").read())

# uvicorn app:app --host 0.0.0.0 --port 8000

mount() registers the following routes (prefixed with the optional path argument):

Endpoint Protocol Purpose
/webrtc/offer HTTP POST WebRTC SDP / ICE exchange
/websocket/offer WebSocket WebSocket streaming
/telephone/incoming HTTP POST Twilio inbound call webhook *
/telephone/handler WebSocket Twilio media-stream handler *

* Telephone (Twilio): dial-in routes are still mounted, so you can point your own Twilio number at /telephone/incoming. The zero-config fastphone() temporary number is gone. (Pending decision: keep these routes for phone dial-in, or strip them for a browser-only deployment.)


Examples

For end-to-end demos (Gemini / OpenAI / Claude voice chat, Whisper transcription, object detection, and more), see the upstream FastRTC cookbook. Those demos are built against the original library and its Gradio UI; adapt the handler logic to a .mount(app) deployment when porting them to this fork.


Credits

Forked from FastRTC by the Gradio team. MIT licensed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastrtc_compact-0.0.1.tar.gz (808.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastrtc_compact-0.0.1-py3-none-any.whl (815.2 kB view details)

Uploaded Python 3

File details

Details for the file fastrtc_compact-0.0.1.tar.gz.

File metadata

  • Download URL: fastrtc_compact-0.0.1.tar.gz
  • Upload date:
  • Size: 808.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for fastrtc_compact-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7f885e961bde72645e7061ba735c5076937237da8ab48166c1d621fac6c65bf3
MD5 0d842f01e92c6fa3a468e226dbe89cb9
BLAKE2b-256 f19c6ea088c4f2e1c3fd7b893601e6d1fa398191828dcb752c8a02b4e4c4b958

See more details on using hashes here.

File details

Details for the file fastrtc_compact-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: fastrtc_compact-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 815.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for fastrtc_compact-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d8544cec2e6163e401b3b330043e77c5eadedfe61f18a363c4a9ddc709b6241
MD5 4525f8fe962c55a1639c912332e108fe
BLAKE2b-256 edbdf6cdbe239a7bb416bf5d00d9a54ca4a2595f683f007bc58634dee03b82f7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page