Spaces:
Sleeping
Sleeping
fix preprocessing steps
Browse files
app.py
CHANGED
|
@@ -1,40 +1,35 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from flask import Flask, render_template, request, jsonify, send_from_directory
|
|
|
|
|
|
|
| 3 |
from tensorflow.keras.models import load_model
|
| 4 |
-
from tensorflow.keras.
|
| 5 |
-
import numpy as np
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import io
|
| 8 |
-
import sys # Added for logging
|
| 9 |
|
| 10 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 11 |
|
| 12 |
# 1. Load Model (Safe Mode)
|
| 13 |
MODEL_PATH = 'model.h5'
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
-
# compile=False prevents crashing if the model was trained on a different TF version
|
| 16 |
model = load_model(MODEL_PATH, compile=False)
|
| 17 |
print("SUCCESS: Model loaded!", file=sys.stderr)
|
| 18 |
except Exception as e:
|
| 19 |
print(f"CRITICAL ERROR: Failed to load model. {e}", file=sys.stderr)
|
| 20 |
|
| 21 |
-
CLASS_NAMES = ['
|
| 22 |
|
| 23 |
def prepare_image(img_bytes):
|
| 24 |
try:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
if img.mode != 'RGB':
|
| 29 |
-
img = img.convert('RGB')
|
| 30 |
-
|
| 31 |
-
# FIX 2: Resize
|
| 32 |
-
img = img.resize((224, 224))
|
| 33 |
-
|
| 34 |
-
img_array = image.img_to_array(img)
|
| 35 |
-
img_array = np.expand_dims(img_array, axis=0)
|
| 36 |
-
#img_array = img_array / 255.0
|
| 37 |
-
return img_array
|
| 38 |
except Exception as e:
|
| 39 |
print(f"Error processing image: {e}", file=sys.stderr)
|
| 40 |
raise e
|
|
@@ -51,48 +46,46 @@ def serve_assets(filename):
|
|
| 51 |
|
| 52 |
@app.route('/<path:filename>')
|
| 53 |
def serve_root_files(filename):
|
| 54 |
-
|
| 55 |
-
return send_from_directory('static', filename)
|
| 56 |
-
return "File not found", 404
|
| 57 |
|
| 58 |
-
# Predict Route
|
| 59 |
@app.route('/api/classify', methods=['POST'])
|
| 60 |
def predict():
|
|
|
|
|
|
|
|
|
|
| 61 |
if not request.files:
|
| 62 |
return jsonify({'error': 'No file uploaded'}), 400
|
| 63 |
|
| 64 |
-
# Grab the first file
|
| 65 |
file = next(iter(request.files.values()))
|
| 66 |
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
print(f"Received file: {file.filename}", file=sys.stderr)
|
| 70 |
|
| 71 |
-
processed_img = prepare_image(
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
class_index = np.argmax(prediction)
|
| 77 |
confidence = float(np.max(prediction))
|
| 78 |
|
| 79 |
result_class = CLASS_NAMES[class_index]
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
result = {
|
| 83 |
-
# Frontend expects 'result', not 'class'
|
| 84 |
'result': result_class,
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
'confidence': float(confidence)
|
| 88 |
}
|
| 89 |
|
| 90 |
return jsonify(result)
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
-
# This will show up in the Logs tab!
|
| 94 |
print(f"PREDICTION CRASHED: {str(e)}", file=sys.stderr)
|
| 95 |
return jsonify({'error': str(e)}), 500
|
| 96 |
|
| 97 |
if __name__ == '__main__':
|
| 98 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
+
import numpy as np
|
| 4 |
from flask import Flask, render_template, request, jsonify, send_from_directory
|
| 5 |
+
|
| 6 |
+
import tensorflow as tf
|
| 7 |
from tensorflow.keras.models import load_model
|
| 8 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 11 |
|
| 12 |
# 1. Load Model (Safe Mode)
|
| 13 |
MODEL_PATH = 'model.h5'
|
| 14 |
+
model = None
|
| 15 |
+
|
| 16 |
try:
|
|
|
|
| 17 |
model = load_model(MODEL_PATH, compile=False)
|
| 18 |
print("SUCCESS: Model loaded!", file=sys.stderr)
|
| 19 |
except Exception as e:
|
| 20 |
print(f"CRITICAL ERROR: Failed to load model. {e}", file=sys.stderr)
|
| 21 |
|
| 22 |
+
CLASS_NAMES = ['Non Demented', 'Very Mild Demented', 'Mild Demented', 'Moderate Demented']
|
| 23 |
|
| 24 |
def prepare_image(img_bytes):
|
| 25 |
try:
|
| 26 |
+
image = tf.io.decode_image(img_bytes, channels=3, expand_animations=False)
|
| 27 |
+
image = tf.image.resize(image, [224, 224])
|
| 28 |
+
image = tf.cast(image, tf.float32)
|
| 29 |
+
image = tf.expand_dims(image, axis=0)
|
| 30 |
+
image = preprocess_input(image)
|
| 31 |
|
| 32 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
print(f"Error processing image: {e}", file=sys.stderr)
|
| 35 |
raise e
|
|
|
|
| 46 |
|
| 47 |
@app.route('/<path:filename>')
|
| 48 |
def serve_root_files(filename):
|
| 49 |
+
return send_from_directory('static', filename)
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Predict Route
|
| 52 |
@app.route('/api/classify', methods=['POST'])
|
| 53 |
def predict():
|
| 54 |
+
if model is None:
|
| 55 |
+
return jsonify({'error': 'Model not loaded correctly'}), 500
|
| 56 |
+
|
| 57 |
if not request.files:
|
| 58 |
return jsonify({'error': 'No file uploaded'}), 400
|
| 59 |
|
|
|
|
| 60 |
file = next(iter(request.files.values()))
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
file_bytes = file.read()
|
|
|
|
| 64 |
|
| 65 |
+
processed_img = prepare_image(file_bytes)
|
| 66 |
|
| 67 |
+
prediction_tensor = model(processed_img, training=False)
|
| 68 |
+
|
| 69 |
+
prediction = prediction_tensor.numpy()
|
| 70 |
|
| 71 |
class_index = np.argmax(prediction)
|
| 72 |
confidence = float(np.max(prediction))
|
| 73 |
|
| 74 |
result_class = CLASS_NAMES[class_index]
|
| 75 |
+
|
| 76 |
+
print(f"Prediction: {result_class} ({confidence:.2%})", file=sys.stderr)
|
| 77 |
|
| 78 |
result = {
|
|
|
|
| 79 |
'result': result_class,
|
| 80 |
+
'confidence': confidence,
|
| 81 |
+
'index': int(class_index)
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
return jsonify(result)
|
| 85 |
|
| 86 |
except Exception as e:
|
|
|
|
| 87 |
print(f"PREDICTION CRASHED: {str(e)}", file=sys.stderr)
|
| 88 |
return jsonify({'error': str(e)}), 500
|
| 89 |
|
| 90 |
if __name__ == '__main__':
|
| 91 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|