File size: 1,171 Bytes
aa72760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import gradio as gr
from transformers import CLIPProcessor, CLIPModel
from PIL import Image
import torch, requests
from io import BytesIO
import numpy as np

# Load CLIP model
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

labels = [
    "dress or gown",
    "t-shirt or top or blouse",
    "jeans or pants or trousers",
    "shoes or sneakers or boots",
    "jacket or coat or blazer",
    "handbag or purse or bag",
    "accessories or jewelry",
    "sportswear or activewear",
    "formal wear or suit",
    "non-fashion item"
]

def classify(img):
    inputs = processor(text=labels, images=img, return_tensors="pt", padding=True)
    outputs = model(**inputs)
    probs = outputs.logits_per_image.softmax(dim=1)[0].detach().numpy()
    result = {labels[i]: float(probs[i]) for i in range(len(labels))}
    return result

iface = gr.Interface(
    fn=classify,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=3),
    title="AI Clothing Classifier",
    description="Upload an image to identify clothing or fashion items."
)

iface.launch(share=True)