voice-clone / app.py
Hjgugugjhuhjggg's picture
Update app.py
a221f1e verified
raw
history blame contribute delete
981 Bytes
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)