Akash8150 commited on
Commit ·
cc9042f
1
Parent(s): 3ea03df
Fix: use tf-keras 2.16 for Keras 2.x compatibility with saved .h5 model
Browse files- app.py +15 -25
- requirements-hf.txt +4 -3
app.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
-
"""
|
| 2 |
Flask Web Application for Image Denoising
|
| 3 |
"""
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
import numpy as np
|
| 7 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from flask import Flask, render_template, request, jsonify
|
| 9 |
-
|
|
|
|
| 10 |
from PIL import Image
|
| 11 |
import io
|
| 12 |
import base64
|
|
@@ -40,7 +45,6 @@ def load_model_info():
|
|
| 40 |
model_info = json.load(f)
|
| 41 |
print(f"Model info loaded from {MODEL_INFO_PATH}")
|
| 42 |
else:
|
| 43 |
-
# Default info if file doesn't exist
|
| 44 |
model_info = {
|
| 45 |
"model_name": "CNN Autoencoder",
|
| 46 |
"architecture": "Convolutional Autoencoder",
|
|
@@ -48,35 +52,26 @@ def load_model_info():
|
|
| 48 |
"test_f1_score": "N/A",
|
| 49 |
"test_loss": "N/A"
|
| 50 |
}
|
| 51 |
-
print(f"Warning: Model info file {MODEL_INFO_PATH} not found! Using defaults.")
|
| 52 |
|
| 53 |
def preprocess_image(image):
|
| 54 |
"""Preprocess uploaded image for model"""
|
| 55 |
-
# Convert to grayscale
|
| 56 |
img = image.convert('L')
|
| 57 |
-
# Resize to 28x28
|
| 58 |
img = img.resize((28, 28))
|
| 59 |
-
# Convert to numpy array and normalize
|
| 60 |
img_array = np.array(img) / 255.0
|
| 61 |
-
# Reshape for model input
|
| 62 |
img_array = img_array.reshape(1, 28, 28, 1)
|
| 63 |
return img_array
|
| 64 |
|
| 65 |
def array_to_base64(img_array):
|
| 66 |
"""Convert numpy array to base64 string for display"""
|
| 67 |
-
# Remove batch and channel dimensions
|
| 68 |
img_array = img_array.squeeze()
|
| 69 |
-
# Convert to 0-255 range
|
| 70 |
img_array = (img_array * 255).astype(np.uint8)
|
| 71 |
-
# Create PIL image
|
| 72 |
img = Image.fromarray(img_array, mode='L')
|
| 73 |
-
# Convert to base64
|
| 74 |
buffer = io.BytesIO()
|
| 75 |
img.save(buffer, format='PNG')
|
| 76 |
img_str = base64.b64encode(buffer.getvalue()).decode()
|
| 77 |
return f"data:image/png;base64,{img_str}"
|
| 78 |
|
| 79 |
-
# Load model and info at module level so it works with Docker
|
| 80 |
load_trained_model()
|
| 81 |
load_model_info()
|
| 82 |
|
|
@@ -98,35 +93,30 @@ def denoise():
|
|
| 98 |
"""Handle image denoising request"""
|
| 99 |
if model is None:
|
| 100 |
return jsonify({'error': 'Model not loaded'}), 500
|
| 101 |
-
|
| 102 |
if 'image' not in request.files:
|
| 103 |
return jsonify({'error': 'No image uploaded'}), 400
|
| 104 |
-
|
| 105 |
file = request.files['image']
|
| 106 |
if file.filename == '':
|
| 107 |
return jsonify({'error': 'No image selected'}), 400
|
| 108 |
-
|
| 109 |
try:
|
| 110 |
-
# Read and preprocess image
|
| 111 |
image = Image.open(file.stream)
|
| 112 |
processed_img = preprocess_image(image)
|
| 113 |
-
|
| 114 |
-
# Denoise image
|
| 115 |
denoised_img = model.predict(processed_img, verbose=0)
|
| 116 |
-
|
| 117 |
-
# Convert to base64 for display
|
| 118 |
original_b64 = array_to_base64(processed_img)
|
| 119 |
denoised_b64 = array_to_base64(denoised_img)
|
| 120 |
-
|
| 121 |
return jsonify({
|
| 122 |
'original': original_b64,
|
| 123 |
'denoised': denoised_b64
|
| 124 |
})
|
| 125 |
-
|
| 126 |
except Exception as e:
|
| 127 |
return jsonify({'error': str(e)}), 500
|
| 128 |
|
| 129 |
if __name__ == '__main__':
|
| 130 |
-
# Hugging Face Spaces requires port 7860; fallback to 5000 for local dev
|
| 131 |
port = int(os.environ.get('PORT', 7860))
|
| 132 |
-
app.run(debug=False, host='0.0.0.0', port=port)
|
|
|
|
| 1 |
+
"""
|
| 2 |
Flask Web Application for Image Denoising
|
| 3 |
"""
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
import numpy as np
|
| 7 |
import json
|
| 8 |
+
|
| 9 |
+
# Use tf-keras (Keras 2 compatibility layer) to load old .h5 models
|
| 10 |
+
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 11 |
+
|
| 12 |
from flask import Flask, render_template, request, jsonify
|
| 13 |
+
import tf_keras as keras
|
| 14 |
+
from tf_keras.models import load_model
|
| 15 |
from PIL import Image
|
| 16 |
import io
|
| 17 |
import base64
|
|
|
|
| 45 |
model_info = json.load(f)
|
| 46 |
print(f"Model info loaded from {MODEL_INFO_PATH}")
|
| 47 |
else:
|
|
|
|
| 48 |
model_info = {
|
| 49 |
"model_name": "CNN Autoencoder",
|
| 50 |
"architecture": "Convolutional Autoencoder",
|
|
|
|
| 52 |
"test_f1_score": "N/A",
|
| 53 |
"test_loss": "N/A"
|
| 54 |
}
|
|
|
|
| 55 |
|
| 56 |
def preprocess_image(image):
|
| 57 |
"""Preprocess uploaded image for model"""
|
|
|
|
| 58 |
img = image.convert('L')
|
|
|
|
| 59 |
img = img.resize((28, 28))
|
|
|
|
| 60 |
img_array = np.array(img) / 255.0
|
|
|
|
| 61 |
img_array = img_array.reshape(1, 28, 28, 1)
|
| 62 |
return img_array
|
| 63 |
|
| 64 |
def array_to_base64(img_array):
|
| 65 |
"""Convert numpy array to base64 string for display"""
|
|
|
|
| 66 |
img_array = img_array.squeeze()
|
|
|
|
| 67 |
img_array = (img_array * 255).astype(np.uint8)
|
|
|
|
| 68 |
img = Image.fromarray(img_array, mode='L')
|
|
|
|
| 69 |
buffer = io.BytesIO()
|
| 70 |
img.save(buffer, format='PNG')
|
| 71 |
img_str = base64.b64encode(buffer.getvalue()).decode()
|
| 72 |
return f"data:image/png;base64,{img_str}"
|
| 73 |
|
| 74 |
+
# Load model and info at module level so it works with Docker
|
| 75 |
load_trained_model()
|
| 76 |
load_model_info()
|
| 77 |
|
|
|
|
| 93 |
"""Handle image denoising request"""
|
| 94 |
if model is None:
|
| 95 |
return jsonify({'error': 'Model not loaded'}), 500
|
| 96 |
+
|
| 97 |
if 'image' not in request.files:
|
| 98 |
return jsonify({'error': 'No image uploaded'}), 400
|
| 99 |
+
|
| 100 |
file = request.files['image']
|
| 101 |
if file.filename == '':
|
| 102 |
return jsonify({'error': 'No image selected'}), 400
|
| 103 |
+
|
| 104 |
try:
|
|
|
|
| 105 |
image = Image.open(file.stream)
|
| 106 |
processed_img = preprocess_image(image)
|
|
|
|
|
|
|
| 107 |
denoised_img = model.predict(processed_img, verbose=0)
|
| 108 |
+
|
|
|
|
| 109 |
original_b64 = array_to_base64(processed_img)
|
| 110 |
denoised_b64 = array_to_base64(denoised_img)
|
| 111 |
+
|
| 112 |
return jsonify({
|
| 113 |
'original': original_b64,
|
| 114 |
'denoised': denoised_b64
|
| 115 |
})
|
| 116 |
+
|
| 117 |
except Exception as e:
|
| 118 |
return jsonify({'error': str(e)}), 500
|
| 119 |
|
| 120 |
if __name__ == '__main__':
|
|
|
|
| 121 |
port = int(os.environ.get('PORT', 7860))
|
| 122 |
+
app.run(debug=False, host='0.0.0.0', port=port)
|
requirements-hf.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# Inference-only requirements for Hugging Face deployment
|
| 2 |
-
#
|
| 3 |
-
tensorflow-cpu==2.
|
| 4 |
-
|
|
|
|
| 5 |
flask==3.0.3
|
| 6 |
pillow==10.3.0
|
|
|
|
| 1 |
# Inference-only requirements for Hugging Face deployment
|
| 2 |
+
# tf-keras provides Keras 2.x compatibility for loading old .h5 models with TF 2.16+
|
| 3 |
+
tensorflow-cpu==2.16.1
|
| 4 |
+
tf-keras==2.16.0
|
| 5 |
+
numpy==1.26.4
|
| 6 |
flask==3.0.3
|
| 7 |
pillow==10.3.0
|