ai newsFr., 23. Okt.
EilmeldungOpenAI vor 30-Milliarden-Dollar-Runde als Brücke zum Börsengang

How to Add AI Voice to Your Django App

Why Add AI Voice to Your Django App? Voice has become the new “screen” for many users. From hands‑free navigation to accessibility, integrating AI‑generated speech can make your Django project feel more natural and inclusive. Whether you’re building a customer‑support chatbot, an audiobook generato

dev.toFreitag 02:30 · 23.10.4 Min. Lesezeit
Bild: dev.to

Voice has become the new “screen” for many users. From hands‑free navigation to accessibility, integrating AI‑generated speech can make your Django project feel more natural and inclusive. Whether you’re building a customer‑support chatbot, an audiobook generator, or a language‑learning platform, adding text‑to‑speech (TTS) is a quick way to boost engagement.

When you need a reliable, high‑quality TTS service that supports voice cloning, ElevenLabs is a popular choice. Their API offers dozens of realistic voices, fine‑grained control over speaking rate, pitch, and more—all wrapped in a developer‑friendly REST interface.

If you’re looking for a service that’s easy to set up and scales with your Django app, try ElevenLabs: https://try.elevenlabs.io/kr07zfuqn1bp

ELEVENLABS_API_KEY=sk_your_key_here Enter fullscreen mode Exit fullscreen mode 2. Install the HTTP Client While you can use curl or any HTTP library, Django developers often prefer httpx for async support.

pip install httpx Enter fullscreen mode Exit fullscreen mode 3. Write a Simple TTS Helper Create a new module, e.g., voice.py, and add:

import os import httpx from django.conf import settings ELEVENLABS_API_URL = " https://api.elevenlabs.io/v1/text-to-speech " async def synthesize ( text: str, voice_id: str = " Bella ", output_path: str = None ): """ Convert text to speech using ElevenLabs. """ headers = { " accept ": " audio/mpeg ", " xi-api-key ": settings. ELEVENLABS_API_KEY, " Content-Type ": " application/json ", } payload = { " text ": text, " voice_settings ": { " stability ": 0.5, " similarity_boost ": 0.75, }, } async with httpx.

AsyncClient () as client: resp = await client. post ( f " { ELEVENLABS_API_URL } / { voice_id } ", json = payload, headers = headers ) resp. raise_for_status () audio_bytes = resp. content if output_path: with open ( output_path, " wb " ) as f: f. write ( audio_bytes ) return audio_bytes Enter fullscreen mode Exit fullscreen mode The voice_id parameter can be any voice you’ve enabled in your ElevenLabs dashboard. “Bella” is a default, but you can list all voices with another endpoint if you need to present options to users.

Here’s a quick example of an endpoint that returns an audio file directly:

from django.http import HttpResponse from.voice import synthesize async def speak ( request ): """ API endpoint: /speak/?text=Hello%20world Returns an MP3 file generated by ElevenLabs. """ text = request. GET. get ( " text ", " Hello, world! " ) audio = await synthesize ( text, voice_id = " Bella " ) return HttpResponse ( audio, content_type = " audio/mpeg " ) Enter fullscreen mode Exit fullscreen mode Add the URL in urls.py:

from django.urls import path from.views import speak urlpatterns = [ path ( " speak/ ", speak, name = " speak " ), ] Enter fullscreen mode Exit fullscreen mode Now, visiting /speak/?text=Good%20morning%20everyone will stream a polished voice directly in the browser.

If you want your users to speak in a personalized voice, ElevenLabs supports cloning with just a few samples. The process:

# Example: cloning a user’s voice async def clone_voice ( uploaded_audio ): headers = { " xi-api-key ": settings. ELEVENLABS_API_KEY } files = { " audio_file ": ( " sample.wav ", uploaded_audio, " audio/wav " )} async with httpx. AsyncClient () as client: resp = await client. post ( " https://api.elevenlabs.io/v1/voices ", headers = headers, files = files, ) resp. raise_for_status () return resp. json ()[ " voice_id " ] Enter fullscreen mode Exit fullscreen mode You can expose this as a Django view that accepts file uploads. Once the voice is cloned, store the voice_id in your user model for later use.

For real‑time applications (e.g., a chatbot that speaks instantly), you’ll want to stream the audio rather than buffering the whole file. ElevenLabs supports streaming via WebSockets or by returning raw audio/mpeg chunks. In Django, you can use StreamingHttpResponse:

from django.http import StreamingHttpResponse async def stream_speak ( request ): text = request. GET. get ( " text ", " Hello " ) async def generator (): # Simulate streaming by yielding chunks audio = await synthesize ( text, voice_id = " Bella " ) yield audio return StreamingHttpResponse ( generator (), content_type = " audio/mpeg " ) Enter fullscreen mode Exit fullscreen mode For production deployments, consider:

If you’re debugging or want to try it from the terminal:

curl -X POST "https://api.elevenlabs.io/v1/text-to-speech/Bella" \ -H "Content-Type: application/json" \ -H "xi-api-key: $ELEVENLABS_API_KEY " \ -d '{"text":"Hello from ElevenLabs!","voice_settings":{"stability":0.5,"similarity_boost":0.75}}' \ --output hello.mp3 Enter fullscreen mode Exit fullscreen mode Open hello.mp3 to hear the result.

Adding AI voice to a Django app is surprisingly straightforward:

By following the snippets above, you’ll have a functional TTS endpoint in minutes, ready to be integrated into your UI, API, or background workers.

Ready to give your Django app a voice? Dive into ElevenLabs today and transform text into natural, expressive speech: https://try.elevenlabs.io/kr07zfuqn1bp

Quelle: dev.to (dev.to). Original öffnen