Spaces:
Sleeping
Sleeping
| # app/services/predictor.py | |
| import numpy as np | |
| import tensorflow as tf | |
| from typing import Tuple | |
| from app.configs import ( | |
| get_classification_model, | |
| sigmoid_to_class, | |
| ) | |
| from app.core.preprocessing import preprocess_image | |
| from app.core.validation import is_valid_image | |
| def classify_image(image_bytes: bytes) -> Tuple[str, float]: | |
| """ | |
| Classify image using the classification model. | |
| Args: | |
| image_bytes: Raw image bytes | |
| Returns: | |
| Tuple of (prediction, confidence) | |
| """ | |
| # Validate image | |
| if not is_valid_image(image_bytes): | |
| raise ValueError("Invalid image format") | |
| # Get model | |
| model = get_classification_model() | |
| if model is None: | |
| raise ValueError("Classification model not loaded") | |
| # Preprocess | |
| img_array = preprocess_image(image_bytes, target_size=(224, 224)) | |
| # 🔥 Call SavedModel signature | |
| result = model(tf.constant(img_array)) | |
| if isinstance(result, dict): | |
| predictions = list(result.values())[0].numpy() | |
| else: | |
| predictions = result.numpy() | |
| # Sigmoid output -> binary classification | |
| confidence = float(predictions[0][0]) | |
| prediction = sigmoid_to_class(confidence) | |
| print(f"🔍 CLASSIFICATION: {prediction} (confidence={confidence:.3f})") | |
| return prediction, confidence |