File size: 2,413 Bytes
92d5846
 
 
 
b0662ae
 
92d5846
33c2569
 
92d5846
33c2569
 
 
68876da
 
 
33c2569
 
 
 
 
 
92d5846
33c2569
 
 
92d5846
33c2569
 
 
 
92d5846
33c2569
 
92d5846
b0662ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33c2569
 
 
b0662ae
33c2569
b0662ae
 
 
 
 
 
 
 
 
 
 
33c2569
 
 
 
 
b0662ae
 
33c2569
b0662ae
92d5846
 
33c2569
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import time
import requests
import pathlib
from io import BytesIO
from flask import Flask, request, render_template, send_file
from pydub import AudioSegment

# Flask App Setup
app = Flask(__name__)

# ElevenLabs API Configuration
ELEVENLABS_API_URL = "https://api.elevenlabs.io/v1/text-to-speech"
HEADERS = {"User-Agent": "TTSApp"}
CACHE_DIR = pathlib.Path("/tmp/audio_cache")
CACHE_DIR.mkdir(parents=True, exist_ok=True)

# Available Voices
ALL_VOICES = {
    "Brian": "nPczCjzI2devNBz1zQrb",
    "Alice": "Xb7hH8MSUJpSbSDYk0k2",
    "Will": "bIHbv24MWmeRgasZH58o",
}

# Split text into sentences (Basic)
def split_sentences(text):
    return text.split(". ")

# Generate TTS
def generate_audio(text, voice):
    if voice not in ALL_VOICES:
        return {"error": f"Invalid voice '{voice}'"}

    filename = CACHE_DIR / f"{int(time.time())}.mp3"
    sentences = split_sentences(text)

    audio_chunks = []
    for sentence in sentences:
        response = requests.post(
            f"{ELEVENLABS_API_URL}/{ALL_VOICES[voice]}",
            headers=HEADERS,
            json={"text": sentence, "model_id": "eleven_multilingual_v2"},
            timeout=20
        )
        if response.ok:
            audio_chunks.append(BytesIO(response.content))

    # Combine all audio parts
    combined_audio = AudioSegment.empty()
    for chunk in audio_chunks:
        chunk_audio = AudioSegment.from_file(chunk, format="mp3")
        combined_audio += chunk_audio

    combined_audio.export(filename, format="mp3")
    return filename.as_posix()

# Flask Routes
@app.route("/", methods=["GET", "POST"])
def home():
    if request.method == "POST":
        text = request.form["text"]
        voice = request.form.get("voice", "Brian")
        audio_file = generate_audio(text, voice)
        return send_file(audio_file, as_attachment=True)

    return """
    <h1>Text-to-Speech Generator</h1>
    <form method="post">
        <label>Text:</label><br>
        <textarea name="text" rows="4" cols="50" required></textarea><br>
        <label>Voice:</label>
        <select name="voice">
            <option value="Brian">Brian</option>
            <option value="Alice">Alice</option>
            <option value="Will">Will</option>
        </select><br><br>
        <button type="submit">Generate & Download</button>
    </form>
    """

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=5000)