Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,40 @@
|
|
| 1 |
-
import
|
| 2 |
from TTS.api import TTS
|
| 3 |
import os
|
|
|
|
| 4 |
|
|
|
|
| 5 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 6 |
|
|
|
|
| 7 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to("cpu")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
output_path = "./output.wav"
|
| 11 |
-
tts.tts_to_file(text=text, speaker_wav=audio, language="en", file_path=output_path)
|
| 12 |
-
return output_path
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file, jsonify
|
| 2 |
from TTS.api import TTS
|
| 3 |
import os
|
| 4 |
+
import tempfile
|
| 5 |
|
| 6 |
+
# Accept TOS
|
| 7 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 8 |
|
| 9 |
+
# Initialize TTS model once
|
| 10 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to("cpu")
|
| 11 |
|
| 12 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
@app.route("/clone", methods=["POST"])
|
| 15 |
+
def clone_route():
|
| 16 |
+
try:
|
| 17 |
+
# Get text and audio file from request
|
| 18 |
+
text = request.form.get("text")
|
| 19 |
+
audio_file = request.files.get("audio")
|
| 20 |
|
| 21 |
+
if not text or not audio_file:
|
| 22 |
+
return jsonify({"error": "Missing text or audio file"}), 400
|
| 23 |
|
| 24 |
+
# Save uploaded audio temporarily
|
| 25 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio:
|
| 26 |
+
audio_path = tmp_audio.name
|
| 27 |
+
audio_file.save(audio_path)
|
| 28 |
+
|
| 29 |
+
# Generate cloned voice
|
| 30 |
+
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
|
| 31 |
+
tts.tts_to_file(text=text, speaker_wav=audio_path, language="en", file_path=output_path)
|
| 32 |
+
|
| 33 |
+
# Return generated audio
|
| 34 |
+
return send_file(output_path, mimetype="audio/wav", as_attachment=True, download_name="output.wav")
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return jsonify({"error": str(e)}), 500
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
app.run(host="0.0.0.0", port=5000, debug=True)
|