File size: 1,933 Bytes
132168d
27f77b8
132168d
 
 
 
 
 
 
27f77b8
132168d
 
 
 
 
 
 
 
 
c6adf9a
27f77b8
 
 
 
 
132168d
 
c6adf9a
27f77b8
 
 
 
c6adf9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27f77b8
 
 
 
 
7a10c1c
27f77b8
 
 
132168d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# ======= PATCH FIRST =======
import fastcore.transform as _fct
try:
    import fasttransform
    if not hasattr(_fct, 'Pipeline'):
        from fasttransform import Pipeline
        _fct.Pipeline = Pipeline
except:
    pass
import torch
_original_torch_load = torch.load
def _patched_load(*args, **kwargs):
    kwargs['weights_only'] = False
    return _original_torch_load(*args, **kwargs)
torch.load = _patched_load
# ======= NOW import fastai =======
from fastai.vision.all import *
import gradio as gr

path = '/content/clothing-dataset-full'
def get_x(r): 
    return path + '/images_original/' + r['image']
def get_y(r): 
    return r['label_cat'].split(' ')

# ======= Load model =======
model = load_learner("clothing_classifier.pkl", cpu=True)
all_labels = model.dls.vocab

def predict(img):
    img = PILImage.create(img)
    pred, pred_idx, probs = model.predict(img)

    age_labels      = ['Kids', 'Adults']
    clothing_labels = [l for l in all_labels if l not in age_labels]

    # أفضل clothing
    clothing_probs = {l: float(probs[list(all_labels).index(l)]) for l in clothing_labels}
    best_clothing  = max(clothing_probs, key=clothing_probs.get)

    # العمر
    kids_prob   = float(probs[list(all_labels).index('Kids')])
    adults_prob = float(probs[list(all_labels).index('Adults')])
    age = 'Kids' if kids_prob > adults_prob else 'Adults'

    # النتيجة: العنوان + top 4 ملابس بس (بدون Adults/Kids)
    top_clothing = dict(sorted(clothing_probs.items(), key=lambda x: x[1], reverse=True)[:4])
    result = {f" {best_clothing} for {age}": 1.0}
    result.update(top_clothing)

    return result

demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=5),
    title="👗 Clothes Classifier",
    description="Upload a clothing image and the model will classify it!"
)
demo.launch(server_name="0.0.0.0", server_port=7860)