Spaces:
Sleeping
Sleeping
MaenGit commited on
Commit ·
44ae691
1
Parent(s): 8364a96
edit server
Browse files
server.py
CHANGED
|
@@ -2,6 +2,7 @@ from flask import Flask, request, send_file, jsonify, after_this_request
|
|
| 2 |
from TTS.api import TTS
|
| 3 |
import tempfile
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
@@ -9,42 +10,42 @@ app = Flask(__name__)
|
|
| 9 |
MODEL_NAME = "tts_models/multilingual/multi-dataset/xtts_v2"
|
| 10 |
tts = TTS(model_name=MODEL_NAME, gpu=False, progress_bar=False)
|
| 11 |
|
| 12 |
-
voices = tts.speakers
|
| 13 |
-
female_voice = voices[0]
|
| 14 |
-
male_voice = voices[1] if len(voices) > 1 else voices[0]
|
| 15 |
|
| 16 |
-
|
| 17 |
-
@app.route("/tts", methods=["GET"])
|
| 18 |
def tts_api():
|
| 19 |
-
text = request.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
if not text:
|
| 24 |
return jsonify({"error": "Text is required"}), 400
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
tmp_path = tmp.name
|
| 30 |
-
tmp.close()
|
| 31 |
|
| 32 |
tts.tts_to_file(
|
| 33 |
text=text,
|
| 34 |
-
|
| 35 |
language=language,
|
| 36 |
-
file_path=
|
| 37 |
)
|
| 38 |
|
|
|
|
|
|
|
| 39 |
@after_this_request
|
| 40 |
def cleanup(response):
|
| 41 |
try:
|
| 42 |
-
os.remove(
|
| 43 |
except Exception as e:
|
| 44 |
print("Cleanup error:", e)
|
| 45 |
return response
|
| 46 |
|
| 47 |
-
return send_file(
|
| 48 |
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from TTS.api import TTS
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
+
import uuid
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
|
|
| 10 |
MODEL_NAME = "tts_models/multilingual/multi-dataset/xtts_v2"
|
| 11 |
tts = TTS(model_name=MODEL_NAME, gpu=False, progress_bar=False)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
@app.route("/tts", methods=["POST"])
|
|
|
|
| 15 |
def tts_api():
|
| 16 |
+
text = request.form.get("text", "")
|
| 17 |
+
language = request.form.get("lang", "en")
|
| 18 |
+
voice_file = request.files.get("voice")
|
| 19 |
|
| 20 |
if not text:
|
| 21 |
return jsonify({"error": "Text is required"}), 400
|
| 22 |
|
| 23 |
+
if not voice_file:
|
| 24 |
+
return jsonify({"error": "Voice file is required"}), 400
|
| 25 |
+
|
| 26 |
+
voice_path = f"/tmp/{uuid.uuid4()}.wav"
|
| 27 |
+
out_path = f"/tmp/{uuid.uuid4()}.wav"
|
| 28 |
|
| 29 |
+
voice_file.save(voice_path)
|
|
|
|
|
|
|
| 30 |
|
| 31 |
tts.tts_to_file(
|
| 32 |
text=text,
|
| 33 |
+
speaker_wav=voice_path,
|
| 34 |
language=language,
|
| 35 |
+
file_path=out_path
|
| 36 |
)
|
| 37 |
|
| 38 |
+
os.remove(voice_path)
|
| 39 |
+
|
| 40 |
@after_this_request
|
| 41 |
def cleanup(response):
|
| 42 |
try:
|
| 43 |
+
os.remove(out_path)
|
| 44 |
except Exception as e:
|
| 45 |
print("Cleanup error:", e)
|
| 46 |
return response
|
| 47 |
|
| 48 |
+
return send_file(out_path, mimetype="audio/wav")
|
| 49 |
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|