Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import os
|
| 4 |
+
import base64
|
| 5 |
+
from colorizer import ImageColorizer
|
| 6 |
+
|
| 7 |
+
# Configure Flask to serve the React build
|
| 8 |
+
app = Flask(__name__, static_folder='frontend/dist', static_url_path='')
|
| 9 |
+
CORS(app)
|
| 10 |
+
|
| 11 |
+
# Initialize colorizer
|
| 12 |
+
colorizer = ImageColorizer()
|
| 13 |
+
|
| 14 |
+
@app.route('/')
|
| 15 |
+
def serve():
|
| 16 |
+
"""Serve the React frontend."""
|
| 17 |
+
return send_from_directory(app.static_folder, 'index.html')
|
| 18 |
+
|
| 19 |
+
@app.route('/api/status', methods=['GET'])
|
| 20 |
+
def check_status():
|
| 21 |
+
"""Check if model files are ready."""
|
| 22 |
+
try:
|
| 23 |
+
ready = colorizer.ensure_models()
|
| 24 |
+
return jsonify({"ready": ready})
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return jsonify({"ready": False, "error": str(e)}), 500
|
| 27 |
+
|
| 28 |
+
@app.route('/api/colorize', methods=['POST'])
|
| 29 |
+
def colorize_image():
|
| 30 |
+
"""Upload and colorize an image."""
|
| 31 |
+
if 'image' not in request.files:
|
| 32 |
+
return jsonify({"error": "No image uploaded"}), 400
|
| 33 |
+
|
| 34 |
+
file = request.files['image']
|
| 35 |
+
if file.filename == '':
|
| 36 |
+
return jsonify({"error": "No selected file"}), 400
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
print(f"Processing image: {file.filename}")
|
| 40 |
+
img_bytes = file.read()
|
| 41 |
+
colorized_bytes = colorizer.colorize(img_bytes)
|
| 42 |
+
|
| 43 |
+
# Return as base64 for easy frontend consumption
|
| 44 |
+
encoded = base64.b64encode(colorized_bytes).decode('utf-8')
|
| 45 |
+
return jsonify({
|
| 46 |
+
"image": f"data:image/jpeg;base64,{encoded}",
|
| 47 |
+
"message": "Successfully colorized"
|
| 48 |
+
})
|
| 49 |
+
except Exception as e:
|
| 50 |
+
import traceback
|
| 51 |
+
traceback.print_exc()
|
| 52 |
+
return jsonify({"error": str(e)}), 500
|
| 53 |
+
|
| 54 |
+
# Error handler to serve index.html for React routes
|
| 55 |
+
@app.errorhandler(404)
|
| 56 |
+
def not_found(e):
|
| 57 |
+
return send_from_directory(app.static_folder, 'index.html')
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
# Initial status check
|
| 61 |
+
print("Checking model files...")
|
| 62 |
+
colorizer.ensure_models()
|
| 63 |
+
|
| 64 |
+
# Use environment variable for port (required for cloud deployment)
|
| 65 |
+
port = int(os.environ.get("PORT", 7860))
|
| 66 |
+
print(f"Starting server on port {port}...")
|
| 67 |
+
app.run(host='0.0.0.0', port=port)
|