Update app.py
Browse files
app.py
CHANGED
|
@@ -6,51 +6,49 @@ import logging
|
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
-
CORS(app)
|
| 10 |
|
| 11 |
# Configure logging
|
| 12 |
-
logging.basicConfig(
|
| 13 |
-
|
| 14 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 15 |
-
)
|
| 16 |
|
| 17 |
@app.route('/health', methods=['GET'])
|
| 18 |
def health():
|
| 19 |
-
return jsonify({"status": "healthy", "
|
| 20 |
|
| 21 |
@app.route('/tts', methods=['POST'])
|
| 22 |
def text_to_speech():
|
| 23 |
-
"""Free TTS Endpoint"""
|
| 24 |
-
if not request.is_json:
|
| 25 |
-
return jsonify({"error": "JSON body required"}), 400
|
| 26 |
-
|
| 27 |
-
data = request.get_json()
|
| 28 |
-
text = data.get("text", "").strip()
|
| 29 |
-
|
| 30 |
-
if not text:
|
| 31 |
-
return jsonify({"error": "Text is required"}), 400
|
| 32 |
-
if len(text) > 3000:
|
| 33 |
-
return jsonify({"error": "Text too long (max 3000 chars)"}), 400
|
| 34 |
-
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return send_file(
|
| 46 |
audio_path,
|
| 47 |
mimetype="audio/mpeg",
|
| 48 |
as_attachment=True,
|
| 49 |
download_name="speech.mp3"
|
| 50 |
)
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
|
| 53 |
return jsonify({"error": str(e)}), 500
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
|
|
|
| 56 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
+
CORS(app, resources={r"/tts": {"origins": "*"}})
|
| 10 |
|
| 11 |
# Configure logging
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.route('/health', methods=['GET'])
|
| 16 |
def health():
|
| 17 |
+
return jsonify({"status": "healthy", "time": datetime.utcnow().isoformat()})
|
| 18 |
|
| 19 |
@app.route('/tts', methods=['POST'])
|
| 20 |
def text_to_speech():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
+
if not request.is_json:
|
| 23 |
+
return jsonify({"error": "JSON body required"}), 400
|
| 24 |
+
|
| 25 |
+
data = request.get_json()
|
| 26 |
+
text = data.get("text", "").strip()
|
| 27 |
+
|
| 28 |
+
if not text:
|
| 29 |
+
return jsonify({"error": "Text is required"}), 400
|
| 30 |
+
if len(text) > 3000:
|
| 31 |
+
return jsonify({"error": "Text exceeds 3000 character limit"}), 400
|
| 32 |
+
|
| 33 |
+
audio_path = generate_audio(text)
|
| 34 |
|
| 35 |
+
# Clean up old files (keep last 10)
|
| 36 |
+
files = sorted([f for f in os.listdir(os.path.dirname(audio_path))],
|
| 37 |
+
key=lambda f: os.path.getmtime(os.path.join(os.path.dirname(audio_path), f)))
|
| 38 |
+
for old_file in files[:-10]:
|
| 39 |
+
os.remove(os.path.join(os.path.dirname(audio_path), old_file))
|
| 40 |
+
|
| 41 |
return send_file(
|
| 42 |
audio_path,
|
| 43 |
mimetype="audio/mpeg",
|
| 44 |
as_attachment=True,
|
| 45 |
download_name="speech.mp3"
|
| 46 |
)
|
| 47 |
+
|
| 48 |
except Exception as e:
|
| 49 |
+
logger.error(f"Error: {str(e)}")
|
| 50 |
return jsonify({"error": str(e)}), 500
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
os.makedirs("tts_outputs", exist_ok=True)
|
| 54 |
app.run(host="0.0.0.0", port=7860)
|