Hugging Face 发布 FastRTC:简化 Python 实时语音交互开发
Hugging Face 推出开源库 FastRTC,旨在降低 Python 开发者构建实时语音交互应用的门槛。该工具封装了 WebRTC 复杂性,支持快速集成音频流与大模型推理,填补了当前实时多模态应用开发中的基础设施空白。
报道正文
本文目录3 个章节
FastRTC:面向 Python 的实时通信库
发布于 2025 年 2 月 25 日 在 GitHub 上更新 点赞 176
Freddy Boulton freddyaboulton Abubakar Abid abidlabs
在过去几个月里,许多新的实时语音模型相继发布,围绕开源和闭源模型也涌现出不少初创公司。列举几个里程碑事件:
- OpenAI 和 Google 发布了 ChatGPT 和 Gemini 的实时多模态 API。OpenAI 甚至推出了 1-800-ChatGPT 电话号码!
- Kyutai 发布了 Moshi,这是一个完全开源的音频到音频 LLM。阿里巴巴发布了 Qwen2-Audio,Fixie.ai 发布了 Ultravox——两款原生支持理解音频的开源 LLM。
- ElevenLabs 在其 C 轮融资中 筹集了 1.8 亿美元。
尽管在模型和融资方面呈现爆发式增长,但构建能够流式传输音频和视频的实时 AI 应用仍然颇具挑战,尤其是在 Python 环境中。
- ML 工程师可能缺乏构建实时应用所需的技术经验,例如 WebRTC。
- 即使是 Cursor 和 Copilot 这样的代码助手工具,也难以编写支持实时音视频应用的 Python 代码。对此我深有体会!
正因如此,我们很高兴宣布推出 FastRTC,这是一款面向 Python 的实时通信库。该库旨在让开发者能够极其轻松地使用纯 Python 构建实时音视频 AI 应用!
在这篇博客文章中,我们将通过构建实时音频应用来介绍 FastRTC 的基础知识。阅读完毕后,你将了解 FastRTC 的核心特性:
- 🗣️ 内置自动语音检测与话轮转换(Turn Taking)功能,因此你只需专注于响应用户的逻辑。
- 💻 自动 UI - 内置支持 WebRTC 的 Gradio UI,用于测试(或直接部署到生产环境!)。
- 📞 电话呼叫 - 使用
fastphone()获取一个免费电话号码以接入你的音频流(需要 HF Token。PRO 账户享有更高限额)。 - ⚡️ 支持 WebRTC 和 WebSocket。
- 💪 可定制 - 你可以将流挂载到任何 FastAPI 应用中,从而提供自定义 UI 或在 Gradio 之外进行部署。
- 🧰 提供大量实用工具,包括文本转语音(TTS)、语音转文本(STT)以及停用词检测,助你快速上手。
让我们深入了解一下。
入门指南
我们将从构建实时音频领域的“Hello World”开始:回显用户所说的内容。在 FastRTC 中,这非常简单:
from fastrtc import Stream, ReplyOnPause
import numpy as np
def echo(audio: tuple[int, np.ndarray]) -> tuple[int, np.ndarray]:
yield audio
stream = Stream(ReplyOnPause(echo), modality="audio", mode="send-receive")
stream.ui.launch()让我们拆解一下这段代码:
ReplyOnPause会为你处理语音检测和话轮转换。你只需关注响应用户的逻辑即可。任何返回音频元组(表示为(sample_rate, audio_data))的生成器都能正常工作。Stream类会为你构建一个 Gradio UI,以便快速测试你的流。完成原型设计后,你可以通过一行代码将 Stream 部署为可用于生产环境的 FastAPI 应用——stream.mount(app),其中app是一个 FastAPI 应用。
以下是实际运行效果:
进阶:LLM 语音聊天
下一步是使用 LLM 来响应用户。FastRTC 内置了语音转文本(STT)和文本转语音(TTS)功能,因此与 LLM 协作变得非常容易。让我们相应地修改我们的 echo 函数:
import os
from fastrtc import (ReplyOnPause, Stream, get_stt_model, get_tts_model)
from openai import OpenAI
sambanova_client = OpenAI(
api_key=os.getenv("SAMBANOVA_API_KEY"), base_url="https://api.sambanova.ai/v1"
)
stt_model = get_stt_model()
tts_model = get_tts_model()
def echo(audio): prompt = stt_model.stt(audio) response = sambanova_client.chat.completions.create( model="Meta-Llama-3.2-3B-Instruct", messages=[{"role": "user", "content": prompt}], max_tokens=200, ) prompt = response.choices[0].message.content for audio_chunk in tts_model.stream_tts_sync(prompt): yield audio_chunk
stream = Stream(ReplyOnPause(echo), modality="audio", mode="send-receive") stream.ui.launch()
我们使用 SambaNova API,因为它速度很快。`get_stt_model()` 会从 Hub 获取 [Moonshine Base](https://huggingface.co/UsefulSensors/moonshine-base),而 `get_tts_model()` 会获取 [Kokoro](https://huggingface.co/hexgrad/Kokoro-82M),这两个模型都针对设备端 CPU 推理进行了进一步优化。不过,你可以使用任何 LLM、文本转语音(TTS)或语音转文本(STT)API,甚至可以使用端到端的语音到语音模型。带上你喜欢的工具——`FastRTC` 只负责处理实时通信层。
## 额外功能:通过电话呼叫
如果你不调用 `stream.ui.launch()`,而是调用 `stream.fastphone()`,你将获得一个免费的电话号码来接入你的流。注意,这需要 Hugging Face token。PRO 账户享有更高的限额。
你会在终端中看到类似这样的信息:
INFO Your FastPhone is now live! Call +1 877-713-4471 and use code 530574 to connect to your stream. INFO You have 30 minutes remaining in your quota (Resetting on 2025-03-23)
然后你可以拨打该号码,它将把你连接到你的流!
## 下一步
- 阅读 [文档](https://fastrtc.org/) 以了解更多关于 `FastRTC` 基础知识的细节。
- 开始构建的最佳方式是查看 [cookbook](https://fastrtc.org/cookbook)。了解如何与流行的 LLM 提供商集成(包括 OpenAI 和 Gemini 的实时 API),如何将你的流与 FastAPI 应用集成并进行自定义部署,如何从 handler 返回额外数据,如何进行视频处理等等!
- ⭐️ 给 [repo](https://github.com/freddyaboulton/fastrtc) 点个 Star,并提交 bug 和功能请求!
- 关注 HuggingFace 上的 [FastRTC Org](https://huggingface.co/fastrtc) 以获取更新,并查看已部署的示例!
感谢体验 `FastRTC`!
## 本文中提到的模型 3
更多来自我们博客的文章
[
real-timeaudiovideo
## Hugging Face 与 Cloudflare 合作,通过 FastRTC 实现无缝的实时语音和视频
30 April 9, 2025](https://huggingface.co/blog/fastrtc-cloudflare)[
audiovisionllm
## Gemma 3n 现已完全融入开源生态系统!
- +4
123 June 26, 2025](https://huggingface.co/blog/gemma3n)
### 社区
[fmurimi](https://huggingface.co/fmurimi) [Feb 25, 2025](https://huggingface.co/blog/fastrtc#67bdfaec63dfad11c2e8d3c8)
哇。
[Omkarp2403](https://huggingface.co/Omkarp2403) [Feb 26, 2025](https://huggingface.co/blog/fastrtc#67beeeb30fd2e346e4bb5bc7) • [edited Feb 26, 2025](https://huggingface.co/blog/fastrtc#67beeeb30fd2e346e4bb5bc7)
fastphone() 可以接受印度电话号码吗?
· [freddyaboulton](https://huggingface.co/freddyaboulton) Article author [Feb 26, 2025](https://huggingface.co/blog/fastrtc#67bf32624512775fcb6579fc)
我们正在努力获取一个 WhatsApp 号码。
[hamishfromatech](https://huggingface.co/hamishfromatech) [Feb 26, 2025](https://huggingface.co/blog/fastrtc#67beeec77de24a342a0424e0)
太棒了!
[ngxson](https://huggingface.co/ngxson) [Feb 26, 2025](https://huggingface.co/blog/fastrtc#67bf3a4463d7e4a8f3ca87ab)
📻 🎙️ 嘿,我基于这篇博客文章生成了一期 **AI 播客**,快来听听吧!
*该播客通过 [ngxson/kokoro-podcast-generator](https://huggingface.co/spaces/ngxson/kokoro-podcast-generator) 生成,使用了 [DeepSeek-R1](https://huggingface.co/deepseek-ai/DeepSeek-R1) 和 [Kokoro-TTS](https://huggingface.co/hexgrad/Kokoro-82M)。*
[MRU4913](https://huggingface.co/MRU4913) [Feb 27, 2025](https://huggingface.co/blog/fastrtc#67bfdc8663fe3d8c3d7d3b9a) • [edited Feb 28, 2025](https://huggingface.co/blog/fastrtc#67bfdc8663fe3d8c3d7d3b9a)
感谢大家。干得漂亮!!!
我有一个关于在使用 tts_model 和 stt_model 时并发处理的问题。每种类型的模型如何处理同时发生的多个请求?(例如:批处理技术?仅 CPU 多线程……)[@freddyaboulton](https://huggingface.co/freddyaboulton)
· [freddyaboulton](https://huggingface.co/freddyaboulton) Article author [Mar 2, 2025](https://huggingface.co/blog/fastrtc#67c4b368579f8be669795de9)
你好 [@MRU4913](https://huggingface.co/MRU4913)!每个流都是事件循环中的一个独立事件。但你可以通过 Stream 类中的参数轻松限制并发运行的流数量。
> *[注:本文篇幅超长,以上为核心前篇精译,后续内容保留原文呈现]*
[Nirav-Madhani](https://huggingface.co/Nirav-Madhani) [Feb 28, 2025](https://huggingface.co/blog/fastrtc#67c139412b161bbc4abfd747) • [edited Feb 28, 2025](https://huggingface.co/blog/fastrtc#67c139412b161bbc4abfd747)
`Taking a while to connect. Are you on a VPN?` Anyone else stuck with this error (I am not using VPN)? This only happens on Gemini examples
· [freddyaboulton](https://huggingface.co/freddyaboulton) Article author [Mar 2, 2025](https://huggingface.co/blog/fastrtc#67c4b38b929c97b1e2760673)
Hi [@Nirav-Madhani](https://huggingface.co/Nirav-Madhani) - I am not sure. Let me see. Feel free to clone and run locally in the meantime.
[MechanicCoder](https://huggingface.co/MechanicCoder) [Mar 3, 2025](https://huggingface.co/blog/fastrtc#67c5396e35c65ea5c1b6b38f)
Would be very cool if you can also add a example with Azure OpenAI-API
[freddyaboulton](https://huggingface.co/freddyaboulton) Article author [Mar 3, 2025](https://huggingface.co/blog/fastrtc#67c54f5517ff2ccbf95b9e1f)
Hi [@MechanicCoder](https://huggingface.co/MechanicCoder) - please feel free to add an example here if you’d like. It should be straightforward- take the example in this blog post and replace the LLM with the api call for the LLM on Azure you like.
· [MechanicCoder](https://huggingface.co/MechanicCoder) [Mar 13, 2025](https://huggingface.co/blog/fastrtc#67d276477669418056c1db7c)
Hey, have a working example...should I send you a repo link?
[monkeyin92](https://huggingface.co/monkeyin92) [Mar 10, 2025](https://huggingface.co/blog/fastrtc#67ceaed3446c69e0ef18d84d)
Can I connect something like FreeSWITCH and have its RTC directly parsed by fastRTC?
· [freddyaboulton](https://huggingface.co/freddyaboulton) Article author [Mar 10, 2025](https://huggingface.co/blog/fastrtc#67cf13cdd3ba7e1e80aa7249) • [edited Mar 10, 2025](https://huggingface.co/blog/fastrtc#67cf13cdd3ba7e1e80aa7249)
I have not tried this myself but I think so. The FastRTC server is completely open so you can integrate with any telephony/webrtc client.
Please open a PR to add a guide on how to do this: [https://github.com/freddyaboulton/fastrtc/blob/main/docs/userguide/audio.md](https://github.com/freddyaboulton/fastrtc/blob/main/docs/userguide/audio.md)
Also feel free to join the HF discord and ask questions in the fastrtc-channels: [https://discord.gg/TSWU7HyaYu](https://discord.gg/TSWU7HyaYu)
[JuanRoyo](https://huggingface.co/JuanRoyo) [Apr 10, 2025](https://huggingface.co/blog/fastrtc#67f777c16b486fe078198cdb)
Hi, I'm new to WebRTC applications, and one of my main questions is: how does the process of capturing audio work? I mean, in demos, you always take the audio directly from the microphone, but I'd like to know if it's possible to get the input audio from a specific port (for example, a listening port where RTP packets are arriving). I guess I need to better understand how WebRTC communications work...Thank you!
· [freddyaboulton](https://huggingface.co/freddyaboulton) Article author [Apr 10, 2025](https://huggingface.co/blog/fastrtc#67f7c9a97500c1c63d07236a)
Can you tell me a bit more about the use case [@JuanRoyo](https://huggingface.co/JuanRoyo) ? WebRTC requires a "handshake" to happen between the two clients. This handshake is taken care of by the `webrtc/offer` route of the FastRTC server. So you can just send a post request there. See this `js` code snippet:
[https://fastrtc.org/userguide/api/](https://fastrtc.org/userguide/api/)
Upload images, audio, and videos by dragging in the text input, pasting, or clicking here. Tap or paste here to upload images
· [Sign up](https://huggingface.co/join?next=%2Fblog%2Ffastrtc) or [log in](https://huggingface.co/login?next=%2Fblog%2Ffastrtc) to comment