sreepathi-ravikumar commited on
Commit
6ec513d
·
verified ·
1 Parent(s): 82721da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -36
app.py CHANGED
@@ -1,37 +1,33 @@
1
- from flask import Flask, request, jsonify, send_file from flask_cors import CORS from audio_generation import generate_audio import os import tempfile from werkzeug.utils import secure_filename import pytesseract from PIL import Image
2
-
3
- app = Flask(__name__) CORS(app)
4
-
5
- UPLOAD_FOLDER = tempfile.gettempdir() app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
6
-
7
- @app.route("/upload", methods=["POST"]) def upload_file(): if 'file' not in request.files: return jsonify({"error": "No file part"}), 400
8
-
9
- file = request.files['file']
10
- if file.filename == '':
11
- return jsonify({"error": "No selected file"}), 400
12
-
13
- filename = secure_filename(file.filename)
14
- filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
15
- file.save(filepath)
16
-
17
- ext = os.path.splitext(filename)[1].lower()
18
- if ext in ['.png', '.jpg', '.jpeg', '.bmp']:
19
- text = pytesseract.image_to_string(Image.open(filepath))
20
- elif ext in ['.txt']:
21
- with open(filepath, 'r', encoding='utf-8') as f:
22
- text = f.read()
23
- else:
24
- text = "[Unsupported file type for text extraction]"
25
-
26
- return jsonify({"text": text})
27
-
28
- @app.route("/speak", methods=["POST"]) def speak(): data = request.get_json() text = data.get("text", "")
29
-
30
- if not text:
31
- return jsonify({"error": "No text provided"}), 400
32
-
33
- audio_path = generate_audio(text)
34
- return send_file(audio_path, mimetype='audio/wav', as_attachment=False)
35
-
36
- if __name__ == "__main__":
37
  app.run(host="0.0.0.0", port=7860)
 
1
+ from flask import Flask, request, jsonify
2
+ from AudioGeneration import generate_audio
3
+ import os
4
+ import uuid
5
+
6
+ app = Flask(__name__)
7
+
8
+ @app.route("/generate-audio", methods=["POST"])
9
+ def generate_audio_endpoint():
10
+ try:
11
+ data = request.get_json()
12
+ text = data.get("text", "").strip()
13
+ if not text:
14
+ return jsonify({"error": "Text input is empty"}), 400
15
+
16
+ filename = f"{uuid.uuid4().hex}.wav"
17
+ audio_path = generate_audio(text, filename)
18
+ audio_url = f"/audio/{filename}"
19
+ return jsonify({"audio_url": audio_url})
20
+ except Exception as e:
21
+ return jsonify({"error": str(e)}), 500
22
+
23
+ @app.route("/audio/<filename>")
24
+ def serve_audio(filename):
25
+ return app.send_static_file(f"audio/{filename}")
26
+
27
+ @app.route("/")
28
+ def root():
29
+ return jsonify({"message": "TTS API is running!"})
30
+
31
+ if __name__ == "__main__":
32
+ os.makedirs("static/audio", exist_ok=True)
 
 
 
 
33
  app.run(host="0.0.0.0", port=7860)