DEV Community

VoiceDeveloper
VoiceDeveloper

Posted on

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 generator, or a language‑learning platform, adding text‑to‑speech (TTS) is a quick way to boost engagement.

What’s the Landscape of AI Voice?

  • Text‑to‑Speech (TTS) – Convert plain text into spoken audio. Modern models deliver near‑human prosody and intonation.
  • Speech‑to‑Text (STT) – Convert spoken words back into text. Useful for voice‑controlled interfaces.
  • Voice Cloning – Replicate a specific speaker’s voice. Ideal for brand consistency or personal assistants.

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

Getting Started with ElevenLabs

1. Create an Account and Grab Your API Key

  1. Sign up at the link above.
  2. In the dashboard, navigate to API Keys and copy the key.
  3. Store it securely, e.g., in a .env file:
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.

4. Hook It Into a Django View

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.

Adding Voice Cloning

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

  1. Upload Voice Samples – Use the Create Voice endpoint with 2–5 minutes of audio.
  2. Generate a Voice ID – The API returns a voice_id you’ll use in subsequent requests.
  3. Synthesize – Pass that voice_id to the same synthesize function.
# 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.

Handling Streaming & Performance

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:

  • Caching generated audio for repeated requests.
  • Background tasks (Celery) to pre‑generate audio for heavy texts.
  • Rate limiting your API key usage to stay within ElevenLabs quotas.

Quick curl Example

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.

Wrap‑Up

Adding AI voice to a Django app is surprisingly straightforward:

  1. Pick a service (ElevenLabs is a solid, feature‑rich choice).
  2. Store your API key securely.
  3. Wrap the REST calls in an async helper.
  4. Expose an endpoint (or background task) to generate audio.
  5. (Optional) Clone user voices for a truly personalized experience.

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

Happy coding!

Top comments (0)