File size: 981 Bytes
a221f1e
 
11fdcf5
 
a221f1e
 
 
11fdcf5
 
 
 
a13c70d
a221f1e
 
 
 
 
 
 
 
a13c70d
a221f1e
 
 
 
 
 
5d8ec27
a221f1e
 
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
import os
from flask import Flask, request, jsonify, send_file
import torch
from TTS.api import TTS
import tempfile

app = Flask(__name__)
os.environ["COQUI_TOS_AGREED"] = "1"

device = "cuda"
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)

@app.route("/clone", methods=["POST"])
def clone_voice():
    data = request.json
    text = data.get("text")
    audio_file_path = data.get("audio_file")

    if not text or not audio_file_path:
        return jsonify({"error": "Text and audio_file are required"}), 400

    try:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio_file:
            tts.tts_to_file(text=text, speaker_wav=audio_file_path, language="en", file_path=tmp_audio_file.name)
            return send_file(tmp_audio_file.name, as_attachment=True, mimetype="audio/wav")
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)