Spaces:
Sleeping
Sleeping
Update app/services/predictor.py
Browse files- app/services/predictor.py +35 -22
app/services/predictor.py
CHANGED
|
@@ -1,33 +1,44 @@
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
-
from typing import Tuple
|
| 3 |
import tensorflow as tf
|
| 4 |
-
from
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from app.core.preprocessing import preprocess_image
|
| 7 |
from app.core.validation import is_valid_image
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Get model
|
| 17 |
model = get_classification_model()
|
| 18 |
-
|
| 19 |
if model is None:
|
| 20 |
raise ValueError("Classification model not loaded")
|
| 21 |
|
| 22 |
-
# Save image first
|
| 23 |
-
import hashlib
|
| 24 |
-
image_filename = f"{hashlib.md5(filename.encode()).hexdigest()[:12]}.jpg"
|
| 25 |
-
image_path = save_image(file_bytes, image_filename)
|
| 26 |
-
|
| 27 |
# Preprocess
|
| 28 |
-
img_array = preprocess_image(
|
| 29 |
|
| 30 |
-
# 🔥
|
| 31 |
result = model(tf.constant(img_array))
|
| 32 |
|
| 33 |
if isinstance(result, dict):
|
|
@@ -35,11 +46,13 @@ def predict_image(file_bytes: bytes, filename: str) -> Tuple[str, float, str]:
|
|
| 35 |
else:
|
| 36 |
predictions = result.numpy()
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
print(f"🔍 CLASSIFICATION: {prediction} ({confidence:.3f})")
|
| 44 |
|
| 45 |
-
return prediction, confidence,
|
|
|
|
| 1 |
+
# app/services/predictor.py
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import tensorflow as tf
|
| 4 |
+
from typing import Tuple, Optional
|
| 5 |
+
|
| 6 |
+
from app.configs import (
|
| 7 |
+
get_classification_model,
|
| 8 |
+
sigmoid_to_class,
|
| 9 |
+
CLASSIFIER_URI,
|
| 10 |
+
)
|
| 11 |
from app.core.preprocessing import preprocess_image
|
| 12 |
from app.core.validation import is_valid_image
|
| 13 |
|
| 14 |
+
|
| 15 |
+
def classify_image(
|
| 16 |
+
image_bytes: bytes,
|
| 17 |
+
model_version: Optional[str] = None,
|
| 18 |
+
) -> Tuple[str, float, str]:
|
| 19 |
+
"""
|
| 20 |
+
Classify image using the classification model.
|
| 21 |
|
| 22 |
+
Args:
|
| 23 |
+
image_bytes: Raw image bytes
|
| 24 |
+
model_version: Optional model version (for A/B testing)
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
Tuple of (prediction, confidence, actual_version)
|
| 28 |
+
"""
|
| 29 |
+
# Validate image
|
| 30 |
+
if not is_valid_image(image_bytes):
|
| 31 |
+
raise ValueError("Invalid image format")
|
| 32 |
|
| 33 |
# Get model
|
| 34 |
model = get_classification_model()
|
|
|
|
| 35 |
if model is None:
|
| 36 |
raise ValueError("Classification model not loaded")
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Preprocess
|
| 39 |
+
img_array = preprocess_image(image_bytes, target_size=(224, 224))
|
| 40 |
|
| 41 |
+
# 🔥 Call SavedModel signature
|
| 42 |
result = model(tf.constant(img_array))
|
| 43 |
|
| 44 |
if isinstance(result, dict):
|
|
|
|
| 46 |
else:
|
| 47 |
predictions = result.numpy()
|
| 48 |
|
| 49 |
+
# Sigmoid output -> binary classification
|
| 50 |
+
confidence = float(predictions[0][0])
|
| 51 |
+
prediction = sigmoid_to_class(confidence)
|
| 52 |
+
|
| 53 |
+
# Use provided version or default
|
| 54 |
+
actual_version = model_version or CLASSIFIER_URI
|
| 55 |
|
| 56 |
+
print(f"🔍 CLASSIFICATION: {prediction} (confidence={confidence:.3f})")
|
| 57 |
|
| 58 |
+
return prediction, confidence, actual_version
|