sreepathi-ravikumar commited on
Commit
a103946
·
verified ·
1 Parent(s): e9a4e37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -43
app.py CHANGED
@@ -1,54 +1,26 @@
 
 
1
  from flask import Flask, request, jsonify, send_file
2
  from flask_cors import CORS
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, 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)
 
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)