跳到正文
DEV Community· VoiceDeveloper·· 3 小時前AI 評分27

使用 ElevenLabs 建立語音克隆示範應用程式

Build a Voice Cloning Demo App

AI 導讀

這篇教學示範如何使用 ElevenLabs 建立語音克隆示範應用程式,讓用戶上載音訊訓練語音模型,再輸入文字生成克隆語音。內容涵蓋 Flask 後端、簡單網頁介面及雲端部署,並列出每月 10 k 字元免費額度,以及按量收費由每 100 k 字元 $0.01 起。

正文

Why Voice Cloning Is the Next Big Thing in Voice AI

Voice AI has gone from “talk to your phone” to “talk to your phone’s voice.”

Text‑to‑speech (TTS) engines have improved dramatically, but the real game‑changer is the ability to clone a specific voice—your own, a brand mascot, or a fictional character. This opens doors for personalized assistants, dynamic audiobooks, and even real‑time dubbing.

If you’re a developer who wants to jump into voice cloning, you’ll need a reliable TTS API, a simple front‑end, and a way to host the model. ElevenLabs offers a powerful, production‑ready voice cloning platform that lets you train a model with just a few minutes of audio. In this article we’ll walk through building a quick demo app that takes a text prompt, clones a voice, and plays back the result—all in under an hour.


Prerequisites

Item Why
Python 3.9+ We'll use a lightweight Flask server for the backend.
Node.js (optional) If you prefer a JavaScript front‑end, you can swap the Flask part for Express.
An ElevenLabs account Sign up at https://try.elevenlabs.io/kr07zfuqn1bp to get your API key.
Audio clip (30–60 s) Needed to train the voice model.
Git Clone the repo or copy the snippets.

Tip: Keep your ElevenLabs API key in an environment variable (ELEVENLABS_API_KEY) so you don’t hard‑code it.


1. Get Your ElevenLabs API Key

  1. Visit https://try.elevenlabs.io/kr07zfuqn1bp and sign up.
  2. Navigate to the API section of the dashboard.
  3. Copy your key and store it in a .env file:
# .env
ELEVENLABS_API_KEY="sk_your_key_here"

Why this link? The link is the official affiliate referral, so every signup helps the platform and you get a small reward.


2. Train a Voice Clone

ElevenLabs makes it simple: upload a short audio file and the system extracts the voice signature.

import os
import requests
import json

API_KEY = os.getenv("ELEVENLABS_API_KEY")
HEADERS = {"xi-api-key": API_KEY, "Content-Type": "application/json"}

# 1️⃣ Create a new voice
voice_name = "Demo Clone"
audio_path = "sample.wav"   # 30–60s of clear speech

with open(audio_path, "rb") as f:
    files = {"audio_file": f}
    resp = requests.post(
        "https://api.elevenlabs.io/v1/voices",
        headers={"xi-api-key": API_KEY},
        files=files,
        data={"name": voice_name}
    )
print(resp.json())

The response will contain a voice_id. Save it; it’s the key to generating cloned audio later.

Pro tip: Use a quiet room and a good microphone. The cleaner the source, the better the clone.


3. Build the Backend

We'll use Flask to expose a simple /synthesize endpoint that accepts a text string and returns an audio blob.

# app.py
import os
import json
import requests
from flask import Flask, request, send_file
from dotenv import load_dotenv

load_dotenv()
API_KEY = os.getenv("ELEVENLABS_API_KEY")
VOICE_ID = os.getenv("ELEVENLABS_VOICE_ID")  # Set this after training

app = Flask(__name__)

@app.route("/synthesize", methods=["POST"])
def synthesize():
    data = request.json
    text = data.get("text")
    if not text:
        return {"error": "Missing text"}, 400

    payload = {
        "text": text,
        "voice_id": VOICE_ID,
        "model_id": "eleven_monolingual_v1"
    }
    headers = {"xi-api-key": API_KEY, "Content-Type": "application/json"}

    resp = requests.post(
        "https://api.elevenlabs.io/v1/text-to-speech",
        headers=headers,
        data=json.dumps(payload)
    )
    if resp.status_code != 200:
        return {"error": "Synthesis failed"}, 500

    # Save the audio temporarily
    audio_path = "output.mp3"
    with open(audio_path, "wb") as f:
        f.write(resp.content)

    return send_file(audio_path, mimetype="audio/mpeg")

if __name__ == "__main__":
    app.run(debug=True)

Run the server:

export FLASK_APP=app.py
flask run

Now the endpoint is ready to synthesize text into your cloned voice.


4. Quick Front‑End Demo

Below is a minimal HTML/JS page that talks to the Flask server. You can open this in a browser and try it out.

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Voice Clone Demo</title>
</head>
<body>
  <h1>Voice Clone Demo</h1>
  <textarea id="text" rows="4" cols="50" placeholder="Type something…"></textarea><br>
  <button id="speak">Speak</button>
  <audio id="audio" controls></audio>

  <script>
    const button = document.getElementById('speak');
    const audio = document.getElementById('audio');

    button.onclick = async () => {
      const text = document.getElementById('text').value;
      const res = await fetch('/synthesize', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({text})
      });

      if (!res.ok) {
        alert('Error: ' + res.statusText);
        return;
      }

      const blob = await res.blob();
      audio.src = URL.createObjectURL(blob);
      audio.play();
    };
  </script>
</body>
</html>

Serve the HTML file via Flask’s static_folder or simply open it locally and point the fetch URL to http://localhost:5000/synthesize.


5. Testing the Demo

  1. Open the front‑end in your browser.
  2. Paste a short sentence like “Hello, world! This is my cloned voice.”
  3. Click “Speak.”
  4. The audio should play back immediately—notice the natural prosody and timbre that matches your original recording.

If you’re not satisfied with the quality, try re‑training with more data or a different sample. ElevenLabs allows you to delete and re‑upload a voice, so iterate quickly.


6. Deploying to the Cloud

For a production demo, you can host the Flask app on platforms like Render, Fly.io, or Heroku. Here’s a quick Render setup:

  1. Create a Render account and start a new Web Service.
  2. Push your repo to GitHub.
  3. In Render, set the environment variable ELEVENLABS_API_KEY and ELEVENLABS_VOICE_ID.
  4. Deploy, and your endpoint will be accessible via https://your-app.onrender.com/synthesize.

You can also containerize the app:

# Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run", "--host=0.0.0.0"]

Build and push to any container registry, then run it in a cloud VM.


7. Scaling and Pricing

ElevenLabs offers a generous free tier: 10 k characters per month. When you hit that limit, the pay‑as‑you‑go model starts at $0.01 per 100 k characters. For a demo app, you’re likely to stay well under that. Just remember to monitor usage in the dashboard.


8. Common Pitfalls

Issue Fix
Low audio quality Use a high‑sample‑rate (44.1 kHz) WAV file.
API key error Verify the key is correct and has Text to Speech permissions.
CORS errors Add flask-cors or set appropriate headers on the server.
Latency The API call can take ~1–2 s; consider pre‑fetching or streaming for larger texts.

9. Wrap‑Up

You now have a fully functional voice cloning demo that:

  • Trains a voice with ElevenLabs.
  • Exposes a clean API for synthesis.
  • Serves a simple web UI for quick testing.
  • Can be deployed to any cloud provider.

This skeleton can be extended to support multi‑language support, voice‑to‑voice translation, or even a Discord bot that speaks in the cloned voice.


Call to Action

Ready to build your own voice‑cloned experiences? Sign up at the official ElevenLabs link below, grab your API key, and start experimenting today. The platform’s intuitive API and powerful cloning engine make it the perfect choice for developers who want to push the boundaries of voice AI.

👉 Try ElevenLabs now: https://try.elevenlabs.io/kr07zfuqn1bp

Happy coding, and may your voices be ever‑lively!

來源:DEV Community · dev.to