refaat9900 commited on
Commit
a604d2e
·
verified ·
1 Parent(s): 1f83219

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -1,3 +1,8 @@
 
 
 
 
 
1
  # Patch Pipeline
2
  import fasttransform
3
  import fastcore.transform as _fct
@@ -30,22 +35,38 @@ def load_learner_fixed(fname):
30
 
31
  # Load model
32
  model = load_learner_fixed("clothing_classifier.pkl")
33
- labels = model.dls.vocab
34
 
35
- # Prediction function — MultiCategoryBlock needs sigmoid threshold
36
  def predict(img):
37
  img = PILImage.create(img)
38
  pred, pred_idx, probs = model.predict(img)
39
- # Convert tensor to plain float dict
40
- result = {}
41
- for i, label in enumerate(labels):
42
- result[str(label)] = float(probs[i])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  return result
44
 
45
  demo = gr.Interface(
46
  fn=predict,
47
  inputs=gr.Image(type="pil"),
48
- outputs=gr.Label(num_top_classes=5),
49
  title="👗 Clothes Classifier",
50
  description="Upload a clothing image and the model will classify it!"
51
  )
 
1
+ # Install cloudpickle first
2
+ import subprocess
3
+ subprocess.run(["pip", "install", "cloudpickle", "-q"], check=True)
4
+ import cloudpickle
5
+
6
  # Patch Pipeline
7
  import fasttransform
8
  import fastcore.transform as _fct
 
35
 
36
  # Load model
37
  model = load_learner_fixed("clothing_classifier.pkl")
38
+ all_labels = model.dls.vocab
39
 
40
+ # Prediction function
41
  def predict(img):
42
  img = PILImage.create(img)
43
  pred, pred_idx, probs = model.predict(img)
44
+
45
+ age_labels = ['Kids', 'Adults']
46
+ clothing_labels = [l for l in all_labels if l not in age_labels]
47
+
48
+ # Get best clothing label from all probs (not just threshold-filtered pred)
49
+ clothing_probs = {l: float(probs[list(all_labels).index(l)]) for l in clothing_labels}
50
+ best_clothing = max(clothing_probs, key=clothing_probs.get)
51
+
52
+ # Get age
53
+ kids_prob = float(probs[list(all_labels).index('Kids')])
54
+ adults_prob = float(probs[list(all_labels).index('Adults')])
55
+ age = 'Kids' if kids_prob > adults_prob else 'Adults'
56
+
57
+ # Return top clothing labels only (no Adults/Kids in output)
58
+ top_clothing = dict(sorted(clothing_probs.items(), key=lambda x: x[1], reverse=True)[:5])
59
+
60
+ # Add age info as first key
61
+ result = {f"✅ {best_clothing} for {age}": 1.0}
62
+ result.update(top_clothing)
63
+
64
  return result
65
 
66
  demo = gr.Interface(
67
  fn=predict,
68
  inputs=gr.Image(type="pil"),
69
+ outputs=gr.Label(num_top_classes=6),
70
  title="👗 Clothes Classifier",
71
  description="Upload a clothing image and the model will classify it!"
72
  )