Omarelrayes commited on
Commit
8b1f705
·
verified ·
1 Parent(s): a930b94

Update app/services/predictor.py

Browse files
Files changed (1) hide show
  1. 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 app.configs import get_classification_model, model_classes
5
- from app.storage import save_image
 
 
 
 
 
6
  from app.core.preprocessing import preprocess_image
7
  from app.core.validation import is_valid_image
8
 
9
- def predict_image(file_bytes: bytes, filename: str) -> Tuple[str, float, str]:
10
- """Predict image using classification model"""
 
 
 
 
 
11
 
12
- # Check if image is valid
13
- if not is_valid_image(file_bytes):
14
- raise ValueError("Invalid image")
 
 
 
 
 
 
 
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(file_bytes, target_size=(224, 224))
29
 
30
- # 🔥 Use SavedModel signature
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
- confidence = float(np.max(predictions[0]))
39
- predicted_class = int(np.argmax(predictions[0]))
40
-
41
- prediction = model_classes.get(predicted_class, "unknown")
 
 
42
 
43
- print(f"🔍 CLASSIFICATION: {prediction} ({confidence:.3f})")
44
 
45
- return prediction, confidence, image_path
 
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