Spaces:
Sleeping
Sleeping
File size: 1,843 Bytes
4975e29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | from flask import Flask, request, jsonify
import tensorflow as tf
from flask_cors import CORS
from utils import predict_image
import os
import requests
app = Flask(__name__)
CORS(app)
# ------------------------------
# MODEL CONFIG
# ------------------------------
MODEL_PATH = "model.h5"
MODEL_URL = "https://huggingface.co/bakhili/stroke-classification-resnet-model/resolve/main/stroke_classification_model.h5"
# ------------------------------
# DOWNLOAD MODEL IF NOT EXISTS
# ------------------------------
if not os.path.exists(MODEL_PATH):
print("Downloading model from Hugging Face...")
r = requests.get(MODEL_URL, stream=True)
with open(MODEL_PATH, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
print("Model downloaded successfully!")
# ------------------------------
# LOAD MODEL
# ------------------------------
print("Loading model...")
model = tf.keras.models.load_model(MODEL_PATH)
print("Model loaded successfully!")
# ------------------------------
# ROUTES
# ------------------------------
@app.route("/")
def home():
return "Stroke Detection Backend Running"
@app.route("/predict", methods=["POST"])
def predict():
try:
if "file" not in request.files:
return jsonify({"error": "No file uploaded"}), 400
file = request.files["file"]
if file.filename == "":
return jsonify({"error": "Empty filename"}), 400
result = predict_image(model, file)
return jsonify(result)
except Exception as e:
print("Error during prediction:", str(e))
return jsonify({"error": "Prediction failed"}), 500
# ------------------------------
# RUN SERVER
# ------------------------------
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
|