FurkanCinko's picture
Update app.py
512dcc0 verified
from flask import Flask, request, jsonify, render_template
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.preprocessing import image
import os
import gdown
app = Flask(__name__)
# Load trained model
MODEL_PATH = "brain_tumor_vgg16.keras"
MODEL_URL = "https://drive.google.com/uc?id=1ftUVldGPLHOWFLCvZg4iOm8DJMSWfNxM"
# Download model if not already downloaded
if not os.path.exists(MODEL_PATH):
print("Downloading model...")
gdown.download(MODEL_URL, MODEL_PATH, quiet=False, use_cookies=False)
# Load the model
model = keras.models.load_model(MODEL_PATH) # Fixed NameError
# Function to preprocess uploaded image
def preprocess_image(img_path):
img = image.load_img(img_path, target_size=(150, 150))
img_array = image.img_to_array(img) / 255.0 # Normalize
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
return img_array
@app.route("/")
def index():
return render_template("index.html")
@app.route("/predict", methods=["POST"])
def predict():
if "file" not in request.files:
return jsonify({"error": "No file uploaded"}), 400
file = request.files["file"]
file_path = "temp.jpg"
file.save(file_path)
# Preprocess and predict
img_array = preprocess_image(file_path)
prediction = model.predict(img_array)[0][0]
result = "Tumor Detected" if prediction > 0.5 else "No Tumor"
confidence = float(prediction)
return jsonify({"prediction": result, "confidence": confidence})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)