Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,20 +6,18 @@ import io
|
|
6 |
import torch
|
7 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
8 |
from realesrgan import RealESRGANer
|
9 |
-
import threading
|
10 |
import time
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
CORS(app)
|
14 |
|
15 |
# Global variables
|
16 |
upsampler = None
|
17 |
-
initialization_status = "initializing" # "initializing", "ready", or "failed"
|
18 |
-
initialization_error = None
|
19 |
|
20 |
-
# Initialize Real-ESRGAN upsampler
|
21 |
-
def
|
22 |
-
global upsampler
|
23 |
|
24 |
try:
|
25 |
print("Starting model initialization...")
|
@@ -53,28 +51,26 @@ def initialize_enhancer_thread():
|
|
53 |
upsampler.enhance(test_img, outscale=4, alpha_upsampler='realesrgan')
|
54 |
|
55 |
print("Model initialization completed successfully")
|
56 |
-
|
57 |
|
58 |
except Exception as e:
|
59 |
error_msg = f"Model initialization failed: {str(e)}"
|
60 |
print(error_msg)
|
61 |
-
|
62 |
-
initialization_error = error_msg
|
63 |
|
64 |
-
#
|
65 |
-
|
|
|
|
|
|
|
66 |
|
67 |
@app.route('/enhance', methods=['POST'])
|
68 |
def enhance_image():
|
69 |
-
global upsampler
|
70 |
|
71 |
-
# Check if
|
72 |
-
if
|
73 |
-
return jsonify({'error': 'Enhancement model is
|
74 |
-
|
75 |
-
# Check if initialization failed
|
76 |
-
if initialization_status == "failed":
|
77 |
-
return jsonify({'error': f'Model initialization failed: {initialization_error}'}), 500
|
78 |
|
79 |
# Check if file was uploaded
|
80 |
if 'file' not in request.files:
|
@@ -114,28 +110,25 @@ def enhance_image():
|
|
114 |
|
115 |
@app.route('/health', methods=['GET'])
|
116 |
def health_check():
|
117 |
-
global
|
118 |
|
119 |
status_info = {
|
120 |
-
'status':
|
121 |
'timestamp': time.time()
|
122 |
}
|
123 |
|
124 |
-
if initialization_status == "failed" and initialization_error:
|
125 |
-
status_info['error'] = initialization_error
|
126 |
-
|
127 |
-
# Return 200 OK even if not ready, but include status in response
|
128 |
return jsonify(status_info)
|
129 |
|
130 |
@app.route('/')
|
131 |
def home():
|
|
|
132 |
return jsonify({
|
133 |
'message': 'Image Enhancement API',
|
134 |
'endpoints': {
|
135 |
'POST /enhance': 'Process images (4x upscale)',
|
136 |
'GET /health': 'Service status check'
|
137 |
},
|
138 |
-
'status':
|
139 |
})
|
140 |
|
141 |
if __name__ == '__main__':
|
|
|
6 |
import torch
|
7 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
8 |
from realesrgan import RealESRGANer
|
|
|
9 |
import time
|
10 |
+
import os
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
CORS(app)
|
14 |
|
15 |
# Global variables
|
16 |
upsampler = None
|
|
|
|
|
17 |
|
18 |
+
# Initialize Real-ESRGAN upsampler at startup
|
19 |
+
def initialize_enhancer():
|
20 |
+
global upsampler
|
21 |
|
22 |
try:
|
23 |
print("Starting model initialization...")
|
|
|
51 |
upsampler.enhance(test_img, outscale=4, alpha_upsampler='realesrgan')
|
52 |
|
53 |
print("Model initialization completed successfully")
|
54 |
+
return True
|
55 |
|
56 |
except Exception as e:
|
57 |
error_msg = f"Model initialization failed: {str(e)}"
|
58 |
print(error_msg)
|
59 |
+
return False
|
|
|
60 |
|
61 |
+
# Initialize model immediately at startup - this blocks until model is ready
|
62 |
+
if not initialize_enhancer():
|
63 |
+
print("ERROR: Model failed to initialize. Server will exit.")
|
64 |
+
# In production, you might prefer to exit when model fails
|
65 |
+
# os.exit(1)
|
66 |
|
67 |
@app.route('/enhance', methods=['POST'])
|
68 |
def enhance_image():
|
69 |
+
global upsampler
|
70 |
|
71 |
+
# Check if upsampler is ready
|
72 |
+
if upsampler is None:
|
73 |
+
return jsonify({'error': 'Enhancement model is not initialized.'}), 500
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# Check if file was uploaded
|
76 |
if 'file' not in request.files:
|
|
|
110 |
|
111 |
@app.route('/health', methods=['GET'])
|
112 |
def health_check():
|
113 |
+
global upsampler
|
114 |
|
115 |
status_info = {
|
116 |
+
'status': 'ready' if upsampler is not None else 'failed',
|
117 |
'timestamp': time.time()
|
118 |
}
|
119 |
|
|
|
|
|
|
|
|
|
120 |
return jsonify(status_info)
|
121 |
|
122 |
@app.route('/')
|
123 |
def home():
|
124 |
+
global upsampler
|
125 |
return jsonify({
|
126 |
'message': 'Image Enhancement API',
|
127 |
'endpoints': {
|
128 |
'POST /enhance': 'Process images (4x upscale)',
|
129 |
'GET /health': 'Service status check'
|
130 |
},
|
131 |
+
'status': 'ready' if upsampler is not None else 'not ready'
|
132 |
})
|
133 |
|
134 |
if __name__ == '__main__':
|