Spaces:
Sleeping
Sleeping
Commit ·
4975e29
0
Parent(s):
initial backend
Browse files- Dockerfile +0 -0
- app.py +74 -0
- requirements.txt +7 -0
- runtime.txt +1 -0
- utils.py +35 -0
Dockerfile
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from flask_cors import CORS
|
| 4 |
+
from utils import predict_image
|
| 5 |
+
import os
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
CORS(app)
|
| 10 |
+
|
| 11 |
+
# ------------------------------
|
| 12 |
+
# MODEL CONFIG
|
| 13 |
+
# ------------------------------
|
| 14 |
+
|
| 15 |
+
MODEL_PATH = "model.h5"
|
| 16 |
+
MODEL_URL = "https://huggingface.co/bakhili/stroke-classification-resnet-model/resolve/main/stroke_classification_model.h5"
|
| 17 |
+
|
| 18 |
+
# ------------------------------
|
| 19 |
+
# DOWNLOAD MODEL IF NOT EXISTS
|
| 20 |
+
# ------------------------------
|
| 21 |
+
|
| 22 |
+
if not os.path.exists(MODEL_PATH):
|
| 23 |
+
print("Downloading model from Hugging Face...")
|
| 24 |
+
r = requests.get(MODEL_URL, stream=True)
|
| 25 |
+
|
| 26 |
+
with open(MODEL_PATH, "wb") as f:
|
| 27 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 28 |
+
if chunk:
|
| 29 |
+
f.write(chunk)
|
| 30 |
+
|
| 31 |
+
print("Model downloaded successfully!")
|
| 32 |
+
|
| 33 |
+
# ------------------------------
|
| 34 |
+
# LOAD MODEL
|
| 35 |
+
# ------------------------------
|
| 36 |
+
|
| 37 |
+
print("Loading model...")
|
| 38 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 39 |
+
print("Model loaded successfully!")
|
| 40 |
+
|
| 41 |
+
# ------------------------------
|
| 42 |
+
# ROUTES
|
| 43 |
+
# ------------------------------
|
| 44 |
+
|
| 45 |
+
@app.route("/")
|
| 46 |
+
def home():
|
| 47 |
+
return "Stroke Detection Backend Running"
|
| 48 |
+
|
| 49 |
+
@app.route("/predict", methods=["POST"])
|
| 50 |
+
def predict():
|
| 51 |
+
try:
|
| 52 |
+
if "file" not in request.files:
|
| 53 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 54 |
+
|
| 55 |
+
file = request.files["file"]
|
| 56 |
+
|
| 57 |
+
if file.filename == "":
|
| 58 |
+
return jsonify({"error": "Empty filename"}), 400
|
| 59 |
+
|
| 60 |
+
result = predict_image(model, file)
|
| 61 |
+
|
| 62 |
+
return jsonify(result)
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print("Error during prediction:", str(e))
|
| 66 |
+
return jsonify({"error": "Prediction failed"}), 500
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# ------------------------------
|
| 70 |
+
# RUN SERVER
|
| 71 |
+
# ------------------------------
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask-cors
|
| 3 |
+
tensorflow
|
| 4 |
+
numpy
|
| 5 |
+
pillow
|
| 6 |
+
gunicorn
|
| 7 |
+
requests
|
runtime.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python-3.10
|
utils.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Utility functions can go here
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
CLASS_NAMES = ['hemorrhagic_stroke', 'ischemic_stroke', 'no_stroke']
|
| 7 |
+
|
| 8 |
+
def preprocess_image(image_file, target_size=(224, 224)):
|
| 9 |
+
"""
|
| 10 |
+
Preprocess uploaded image for prediction
|
| 11 |
+
"""
|
| 12 |
+
img = Image.open(image_file).convert("RGB")
|
| 13 |
+
img = img.resize(target_size)
|
| 14 |
+
|
| 15 |
+
img_array = tf.keras.utils.img_to_array(img)
|
| 16 |
+
img_array = tf.expand_dims(img_array, 0)
|
| 17 |
+
|
| 18 |
+
return img_array
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def predict_image(model, image_file):
|
| 22 |
+
"""
|
| 23 |
+
Predict stroke type
|
| 24 |
+
"""
|
| 25 |
+
processed = preprocess_image(image_file)
|
| 26 |
+
|
| 27 |
+
predictions = model.predict(processed)
|
| 28 |
+
index = np.argmax(predictions[0])
|
| 29 |
+
|
| 30 |
+
confidence = float(np.max(predictions[0]) * 100)
|
| 31 |
+
|
| 32 |
+
return {
|
| 33 |
+
"prediction": CLASS_NAMES[index],
|
| 34 |
+
"confidence": round(confidence, 2)
|
| 35 |
+
}
|