Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,26 @@
|
|
|
|
|
|
1 |
from flask import Flask, request, jsonify, send_file
|
2 |
from flask_cors import CORS
|
3 |
-
from
|
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(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 |
-
|
34 |
-
|
35 |
-
|
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 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
)
|
47 |
|
48 |
-
|
49 |
-
|
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)
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
from flask import Flask, request, jsonify, send_file
|
4 |
from flask_cors import CORS
|
5 |
+
from audio_generation import generate_audio
|
6 |
import os
|
|
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
+
CORS(app)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
@app.route("/")
|
12 |
+
def index():
|
13 |
+
return "Bark TTS API is running."
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
@app.route("/speak", methods=["POST"])
|
16 |
+
def speak():
|
17 |
+
data = request.json
|
18 |
+
text = data.get("text", "")
|
19 |
+
if not text:
|
20 |
+
return jsonify({"error": "No text provided"}), 400
|
21 |
|
22 |
+
audio_path = generate_audio(text)
|
23 |
+
return send_file(audio_path, mimetype="audio/wav")
|
|
|
24 |
|
25 |
if __name__ == "__main__":
|
|
|
26 |
app.run(host="0.0.0.0", port=7860)
|