Spaces:
Sleeping
Sleeping
Fix file paths
Browse files
app.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
import os
|
| 2 |
-
from flask import Flask, render_template, request, jsonify
|
| 3 |
from tensorflow.keras.models import load_model
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
# 1. Load Model
|
| 12 |
MODEL_PATH = 'model.h5'
|
| 13 |
try:
|
| 14 |
model = load_model(MODEL_PATH)
|
|
@@ -16,17 +17,11 @@ try:
|
|
| 16 |
except Exception as e:
|
| 17 |
print(f"Error loading model: {e}")
|
| 18 |
|
| 19 |
-
# Class names matching your Classification Report
|
| 20 |
CLASS_NAMES = ['NonDemented', 'VeryMildDemented', 'MildDemented', 'ModerateDemented']
|
| 21 |
|
| 22 |
def prepare_image(img_bytes):
|
| 23 |
-
# Convert bytes to PIL Image
|
| 24 |
img = Image.open(io.BytesIO(img_bytes))
|
| 25 |
-
|
| 26 |
-
# Resize to 224x224 (As we found earlier)
|
| 27 |
img = img.resize((224, 224))
|
| 28 |
-
|
| 29 |
-
# Convert to array and normalize
|
| 30 |
img_array = image.img_to_array(img)
|
| 31 |
img_array = np.expand_dims(img_array, axis=0)
|
| 32 |
img_array = img_array / 255.0
|
|
@@ -34,12 +29,25 @@ def prepare_image(img_bytes):
|
|
| 34 |
|
| 35 |
# --- ROUTES ---
|
| 36 |
|
| 37 |
-
# 1.
|
| 38 |
@app.route('/')
|
| 39 |
def home():
|
| 40 |
return render_template('index.html')
|
| 41 |
|
| 42 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
@app.route('/predict', methods=['POST'])
|
| 44 |
def predict():
|
| 45 |
if 'file' not in request.files:
|
|
@@ -48,10 +56,7 @@ def predict():
|
|
| 48 |
file = request.files['file']
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
# Process image
|
| 52 |
processed_img = prepare_image(file.read())
|
| 53 |
-
|
| 54 |
-
# Predict
|
| 55 |
prediction = model.predict(processed_img)
|
| 56 |
class_index = np.argmax(prediction)
|
| 57 |
confidence = float(np.max(prediction))
|
|
|
|
| 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.preprocessing import image
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
| 8 |
|
| 9 |
+
# Initialize Flask with standard folders
|
| 10 |
+
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 11 |
|
| 12 |
+
# 1. Load Model
|
| 13 |
MODEL_PATH = 'model.h5'
|
| 14 |
try:
|
| 15 |
model = load_model(MODEL_PATH)
|
|
|
|
| 17 |
except Exception as e:
|
| 18 |
print(f"Error loading model: {e}")
|
| 19 |
|
|
|
|
| 20 |
CLASS_NAMES = ['NonDemented', 'VeryMildDemented', 'MildDemented', 'ModerateDemented']
|
| 21 |
|
| 22 |
def prepare_image(img_bytes):
|
|
|
|
| 23 |
img = Image.open(io.BytesIO(img_bytes))
|
|
|
|
|
|
|
| 24 |
img = img.resize((224, 224))
|
|
|
|
|
|
|
| 25 |
img_array = image.img_to_array(img)
|
| 26 |
img_array = np.expand_dims(img_array, axis=0)
|
| 27 |
img_array = img_array / 255.0
|
|
|
|
| 29 |
|
| 30 |
# --- ROUTES ---
|
| 31 |
|
| 32 |
+
# 1. Main Page
|
| 33 |
@app.route('/')
|
| 34 |
def home():
|
| 35 |
return render_template('index.html')
|
| 36 |
|
| 37 |
+
# 2. FIX: Explicitly serve the assets folder
|
| 38 |
+
@app.route('/assets/<path:filename>')
|
| 39 |
+
def serve_assets(filename):
|
| 40 |
+
return send_from_directory('static/assets', filename)
|
| 41 |
+
|
| 42 |
+
# 3. FIX: Serve root files (like brain.svg)
|
| 43 |
+
@app.route('/<path:filename>')
|
| 44 |
+
def serve_root_files(filename):
|
| 45 |
+
# Only serve if the file exists in static (avoids crashing on bad links)
|
| 46 |
+
if os.path.exists(os.path.join('static', filename)):
|
| 47 |
+
return send_from_directory('static', filename)
|
| 48 |
+
return "File not found", 404
|
| 49 |
+
|
| 50 |
+
# 4. Handle Predictions
|
| 51 |
@app.route('/predict', methods=['POST'])
|
| 52 |
def predict():
|
| 53 |
if 'file' not in request.files:
|
|
|
|
| 56 |
file = request.files['file']
|
| 57 |
|
| 58 |
try:
|
|
|
|
| 59 |
processed_img = prepare_image(file.read())
|
|
|
|
|
|
|
| 60 |
prediction = model.predict(processed_img)
|
| 61 |
class_index = np.argmax(prediction)
|
| 62 |
confidence = float(np.max(prediction))
|