Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,37 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
| 4 |
-
from flask_cors import CORS
|
| 5 |
-
from audio_generation import generate_audio
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
|
| 9 |
-
CORS(app)
|
| 10 |
|
| 11 |
-
@app.route("/")
|
| 12 |
-
def index():
|
| 13 |
-
return "Bark TTS API is running."
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
text = data.get("text", "")
|
| 19 |
-
if not text:
|
| 20 |
-
return jsonify({"error": "No text provided"}), 400
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 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)
|