File size: 2,147 Bytes
de447d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import uuid
import shutil
from flask import Flask, request, jsonify, render_template
from gradio_client import Client

app = Flask(__name__)
app.secret_key = os.urandom(24)

# Where we’ll drop generated mp3s for the browser to fetch
os.makedirs("static", exist_ok=True)

# Point to your HF Space (leave as-is if this is already working for you)
TTS_SPACE_URL = "https://altafo-free-tts-unlimted-words.hf.space/"
client = Client(TTS_SPACE_URL)

@app.route("/")
def index():
    # expects templates/index.html
    return render_template("index.html")

@app.route("/api/ping")
def ping():
    return "pong", 200

@app.route("/tts", methods=["POST"])
def tts():
    """
    JSON body:
      { "text": "...", "voice": "en-US-AriaNeural - en-US (Female)", "rate": 0, "pitch": 0 }
    Returns:
      { "url": "/static/<uuid>.mp3" }
    """
    data = request.get_json(force=True) or {}
    text_input = (data.get("text") or "").strip()
    if not text_input:
        return jsonify({"error": "No text provided"}), 400

    voice = (data.get("voice") or "en-US-AriaNeural - en-US (Female)").strip()
    try:
        rate = int(data.get("rate", 0))
    except Exception:
        rate = 0
    try:
        pitch = int(data.get("pitch", 0))
    except Exception:
        pitch = 0

    # Clamp to safe ranges
    rate = max(-50, min(50, rate))
    pitch = max(-20, min(20, pitch))

    try:
        # Call the Space
        result = client.predict(
            text_input,
            voice,
            rate,
            pitch,
            api_name="/tts_interface"
        )
        temp_path = result[0]
        if not temp_path or not os.path.exists(temp_path):
            return jsonify({"error": "TTS failed"}), 500

        out_name = f"{uuid.uuid4().hex}.mp3"
        out_path = os.path.join("static", out_name)
        shutil.copy(temp_path, out_path)

        return jsonify({"url": f"/static/{out_name}"})
    except Exception as e:
        return jsonify({"error": f"TTS request failed: {e}"}), 500


if __name__ == "__main__":
    print("🔊 TTS Flask server running on http://0.0.0.0:7860")
    app.run(host="0.0.0.0", port=7860)