Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,71 @@
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
-
# -----------------------------
|
| 8 |
-
# FastAPI
|
| 9 |
-
# -----------------------------
|
| 10 |
app = FastAPI(
|
| 11 |
title="O-Ring Defect Detection API",
|
| 12 |
description="CNN-based O-ring defect classifier",
|
| 13 |
version="1.0"
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# -----------------------------
|
| 17 |
-
# Load
|
| 18 |
-
# -----------------------------
|
| 19 |
MODEL_PATH = "o_ring_cnn_classifier (1).keras"
|
| 20 |
-
model = tf.keras.models.load_model(MODEL_PATH)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# -----------------------------
|
| 29 |
# Utils
|
| 30 |
-
# -----------------------------
|
| 31 |
-
def preprocess_image(image_bytes):
|
| 32 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 33 |
image = image.resize(IMG_SIZE)
|
| 34 |
image = np.array(image, dtype=np.float32) / 255.0
|
| 35 |
image = np.expand_dims(image, axis=0)
|
| 36 |
return image
|
| 37 |
|
| 38 |
-
|
| 39 |
-
# -----------------------------
|
| 40 |
# Routes
|
| 41 |
-
# -----------------------------
|
|
|
|
| 42 |
@app.get("/")
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
"classes": CLASS_NAMES
|
| 48 |
-
}
|
| 49 |
|
| 50 |
|
| 51 |
@app.post("/predict")
|
| 52 |
async def predict(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
| 53 |
image_bytes = await file.read()
|
| 54 |
image = preprocess_image(image_bytes)
|
| 55 |
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
|
| 8 |
+
# --------------------------------------------------
|
| 9 |
+
# FastAPI App
|
| 10 |
+
# --------------------------------------------------
|
| 11 |
app = FastAPI(
|
| 12 |
title="O-Ring Defect Detection API",
|
| 13 |
description="CNN-based O-ring defect classifier",
|
| 14 |
version="1.0"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# --------------------------------------------------
|
| 18 |
+
# Model Load (ON STARTUP)
|
| 19 |
+
# --------------------------------------------------
|
| 20 |
MODEL_PATH = "o_ring_cnn_classifier (1).keras"
|
|
|
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 24 |
+
print("✅ Model loaded successfully")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print("❌ Model loading failed:", e)
|
| 27 |
+
raise e
|
| 28 |
|
| 29 |
+
# Dataset semantics (VERY IMPORTANT)
|
| 30 |
+
CLASS_NAMES = ["paregi", "pelise"]
|
| 31 |
|
| 32 |
+
# Change ONLY if your training size was different
|
| 33 |
+
IMG_SIZE = (224, 224)
|
| 34 |
|
| 35 |
+
# --------------------------------------------------
|
| 36 |
# Utils
|
| 37 |
+
# --------------------------------------------------
|
| 38 |
+
def preprocess_image(image_bytes: bytes):
|
| 39 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 40 |
image = image.resize(IMG_SIZE)
|
| 41 |
image = np.array(image, dtype=np.float32) / 255.0
|
| 42 |
image = np.expand_dims(image, axis=0)
|
| 43 |
return image
|
| 44 |
|
| 45 |
+
# --------------------------------------------------
|
|
|
|
| 46 |
# Routes
|
| 47 |
+
# --------------------------------------------------
|
| 48 |
+
|
| 49 |
@app.get("/")
|
| 50 |
+
def root():
|
| 51 |
+
"""
|
| 52 |
+
Root endpoint (IMPORTANT for Hugging Face Spaces)
|
| 53 |
+
"""
|
| 54 |
+
return JSONResponse({
|
| 55 |
+
"message": "O-Ring Defect Detection API is running 🚀",
|
| 56 |
+
"endpoints": {
|
| 57 |
+
"predict": "/predict",
|
| 58 |
+
"docs": "/docs"
|
| 59 |
+
},
|
| 60 |
"classes": CLASS_NAMES
|
| 61 |
+
})
|
| 62 |
|
| 63 |
|
| 64 |
@app.post("/predict")
|
| 65 |
async def predict(file: UploadFile = File(...)):
|
| 66 |
+
"""
|
| 67 |
+
Image prediction endpoint
|
| 68 |
+
"""
|
| 69 |
image_bytes = await file.read()
|
| 70 |
image = preprocess_image(image_bytes)
|
| 71 |
|