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/.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)