Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,42 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
|
|
|
| 5 |
|
| 6 |
-
@app.route(
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if __name__ == "__main__":
|
| 17 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from tts_engine import BilingualTTS
|
| 4 |
+
import asyncio
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
+
CORS(app, resources={r"/tts": {"origins": "*"}})
|
| 10 |
|
| 11 |
+
@app.route('/tts', methods=['POST'])
|
| 12 |
+
def text_to_speech():
|
| 13 |
+
try:
|
| 14 |
+
data = request.get_json()
|
| 15 |
+
text = data.get("text", "").strip()
|
| 16 |
+
|
| 17 |
+
if not text:
|
| 18 |
+
return jsonify({"error": "Text is required"}), 400
|
| 19 |
+
if len(text) > 5000:
|
| 20 |
+
return jsonify({"error": "Text too long (max 5000 chars)"}), 400
|
| 21 |
+
|
| 22 |
+
# Generate audio
|
| 23 |
+
tts_engine = BilingualTTS()
|
| 24 |
+
audio_path = asyncio.run(tts_engine.generate_audio(text))
|
| 25 |
+
|
| 26 |
+
# Cleanup old files
|
| 27 |
+
for f in os.listdir(tts_engine.temp_dir):
|
| 28 |
+
if f != "final_output.mp3":
|
| 29 |
+
os.remove(os.path.join(tts_engine.temp_dir, f))
|
| 30 |
+
|
| 31 |
+
return send_file(
|
| 32 |
+
audio_path,
|
| 33 |
+
mimetype="audio/mpeg",
|
| 34 |
+
as_attachment=True,
|
| 35 |
+
download_name="speech.mp3"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return jsonify({"error": str(e)}), 500
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
app.run(host="0.0.0.0", port=7860)
|