Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,38 +3,54 @@ from flask_cors import CORS
|
|
| 3 |
from audio_generator import generate_audio
|
| 4 |
import os
|
| 5 |
import logging
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
CORS(app)
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.route('/health', methods=['GET'])
|
| 12 |
def health():
|
| 13 |
-
return jsonify({"status": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
@app.route('/generate-audio', methods=['POST'])
|
| 16 |
-
def generate_audio_route():
|
| 17 |
data = request.get_json()
|
| 18 |
text = data.get("text", "").strip()
|
| 19 |
-
|
| 20 |
if not text:
|
| 21 |
return jsonify({"error": "Text is required"}), 400
|
|
|
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return send_file(
|
| 27 |
-
|
| 28 |
mimetype="audio/mpeg",
|
| 29 |
as_attachment=True,
|
| 30 |
-
download_name="
|
| 31 |
)
|
| 32 |
-
except RuntimeError as e:
|
| 33 |
-
app.logger.error(f"Audio generation failed: {str(e)}")
|
| 34 |
-
return jsonify({"error": str(e)}), 500
|
| 35 |
except Exception as e:
|
| 36 |
-
|
| 37 |
-
return jsonify({"error":
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
-
app.run(host=
|
|
|
|
| 3 |
from audio_generator import generate_audio
|
| 4 |
import os
|
| 5 |
import logging
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
CORS(app)
|
| 10 |
+
|
| 11 |
+
# Configure logging
|
| 12 |
+
logging.basicConfig(
|
| 13 |
+
level=logging.INFO,
|
| 14 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 15 |
+
)
|
| 16 |
|
| 17 |
@app.route('/health', methods=['GET'])
|
| 18 |
def health():
|
| 19 |
+
return jsonify({"status": "healthy", "service": "Free TTS API"})
|
| 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 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 37 |
+
filename = f"output_{timestamp}.mp3"
|
| 38 |
+
|
| 39 |
+
# Generate with natural voice (adjust parameters as needed)
|
| 40 |
+
audio_path = generate_audio(
|
| 41 |
+
text,
|
| 42 |
+
filename=filename
|
| 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 |
+
logging.error(f"TTS Error: {str(e)}")
|
| 53 |
+
return jsonify({"error": str(e)}), 500
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
app.run(host="0.0.0.0", port=7860)
|