Spaces:
Sleeping
Sleeping
Ellie Sleightholm
commited on
Commit
·
8e62182
1
Parent(s):
d1591db
adding basic space structure
Browse files- app.py +176 -0
- images/blouse.jpg +0 -0
- images/cargo.jpg +0 -0
- images/dress.jpg +0 -0
- images/hat.jpg +0 -0
- images/polo-shirt.jpg +0 -0
- images/sunglasses.jpg +0 -0
- images/sweatpants.jpg +0 -0
- images/t-shirt.jpg +0 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import open_clip
|
3 |
+
import torch
|
4 |
+
import requests
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# Sidebar content
|
9 |
+
sidebar_markdown = """
|
10 |
+
|
11 |
+
We have several resources related to our new fashion models.
|
12 |
+
|
13 |
+
## Documentation
|
14 |
+
📚 [Blog Post](https://www.marqo.ai/blog/search-model-for-fashion)
|
15 |
+
|
16 |
+
📝 [Use Case Blog Post](https://www.marqo.ai/blog/ecommerce-image-classification-with-marqo-fashionclip)
|
17 |
+
|
18 |
+
## Code
|
19 |
+
💻 [GitHub Repo](https://github.com/marqo-ai/marqo-FashionCLIP)
|
20 |
+
|
21 |
+
🤝 [Google Colab](https://colab.research.google.com/drive/1nq978xFJjJcnyrJ2aE5l82GHAXOvTmfd?usp=sharing)
|
22 |
+
|
23 |
+
🤗 [Hugging Face Collection](https://huggingface.co/collections/Marqo/marqo-fashionclip-and-marqo-fashionsiglip-66b43f2d09a06ad2368d4af6)
|
24 |
+
|
25 |
+
## Citation
|
26 |
+
If you use Marqo-FashionSigLIP or Marqo-FashionCLIP, please cite us:
|
27 |
+
```
|
28 |
+
@software{Jung_Marqo-FashionCLIP_and_Marqo-FashionSigLIP_2024,
|
29 |
+
author = {Jung, Myong Chol and Clark, Jesse},
|
30 |
+
month = aug,
|
31 |
+
title = {{Marqo-FashionCLIP and Marqo-FashionSigLIP}},
|
32 |
+
url = {https://github.com/marqo-ai/marqo-FashionCLIP},
|
33 |
+
version = {1.0.0},
|
34 |
+
year = {2024}
|
35 |
+
```
|
36 |
+
"""
|
37 |
+
|
38 |
+
# List of fashion items
|
39 |
+
items = [
|
40 |
+
"leggings", "jogger", "palazzo", "cargo", "dresspants", "chinos",
|
41 |
+
"dress", "blouse", "t-shirt", "jeans", "skirt", "shorts",
|
42 |
+
"sweater", "cardigan", "tank top", "hoodie", "coat",
|
43 |
+
"jacket", "polo shirt", "crop top", "romper",
|
44 |
+
"overalls", "blazer", "sweatpants", "vest",
|
45 |
+
"dungarees", "poncho", "bodysuit", "maxi dress",
|
46 |
+
"hat", "sunglasses", "glasses", "shoes", "sandals", "heels", "trainers", "belt", "tie", "dress shirt", "boots",
|
47 |
+
"slippers",
|
48 |
+
"sneakers",
|
49 |
+
"insoles",
|
50 |
+
"socks",
|
51 |
+
"insulated jacket",
|
52 |
+
"fleece",
|
53 |
+
"rain jacket",
|
54 |
+
"running jacket",
|
55 |
+
"windbreaker",
|
56 |
+
"shirt",
|
57 |
+
"t-shirt",
|
58 |
+
"tank top",
|
59 |
+
"graphic top",
|
60 |
+
"sweater",
|
61 |
+
"sweatshirt",
|
62 |
+
"vest",
|
63 |
+
"pant",
|
64 |
+
"legging",
|
65 |
+
"short",
|
66 |
+
"dress",
|
67 |
+
"skirt",
|
68 |
+
"skort",
|
69 |
+
"brief",
|
70 |
+
"sports bra",
|
71 |
+
"base layer top",
|
72 |
+
"base layer bottom",
|
73 |
+
"swimsuit",
|
74 |
+
"rashguard",
|
75 |
+
"water shorts",
|
76 |
+
"cover up",
|
77 |
+
"goggle",
|
78 |
+
"hat",
|
79 |
+
"sun hat",
|
80 |
+
"glove",
|
81 |
+
"mitten",
|
82 |
+
"belt",
|
83 |
+
"leg gaiter",
|
84 |
+
]
|
85 |
+
|
86 |
+
# Initialize the model and tokenizer
|
87 |
+
model_name = 'hf-hub:Marqo/marqo-fashionSigLIP'
|
88 |
+
model, preprocess_train, preprocess_val = open_clip.create_model_and_transforms(model_name)
|
89 |
+
tokenizer = open_clip.get_tokenizer(model_name)
|
90 |
+
|
91 |
+
# Generate descriptions dynamically
|
92 |
+
def generate_description(item):
|
93 |
+
if "pants" in item or item in ["leggings", "jogger", "cargo", "chinos", "palazzo"]:
|
94 |
+
return f"a pair of {item} pants"
|
95 |
+
elif item in ["dress", "blouse", "t-shirt", "tank top", "sweater", "cardigan", "hoodie", "coat", "jacket", "polo shirt", "crop top", "romper", "blazer", "vest", "bodysuit", "maxi dress"]:
|
96 |
+
return f"a {item}"
|
97 |
+
elif item in ["hat", "sunglasses", "glasses"]:
|
98 |
+
return f"a {item} worn on the head or face"
|
99 |
+
elif item in ["shoes", "sandals", "heels", "trainers"]:
|
100 |
+
return f"a pair of {item} worn on the feet"
|
101 |
+
elif item in ["jeans", "skirt", "shorts", "sweatpants", "dungarees", "poncho", "overalls", "dress shirt"]:
|
102 |
+
return f"a {item} piece of clothing"
|
103 |
+
else:
|
104 |
+
return f"a fashion item called {item}"
|
105 |
+
|
106 |
+
items_desc = [generate_description(item) for item in items]
|
107 |
+
text = tokenizer(items_desc)
|
108 |
+
|
109 |
+
# Encode text features
|
110 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
111 |
+
text_features = model.encode_text(text)
|
112 |
+
text_features /= text_features.norm(dim=-1, keepdim=True)
|
113 |
+
|
114 |
+
# Prediction function
|
115 |
+
def predict(inp):
|
116 |
+
image = preprocess_val(inp).unsqueeze(0)
|
117 |
+
|
118 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
119 |
+
image_features = model.encode_image(image)
|
120 |
+
image_features /= image_features.norm(dim=-1, keepdim=True)
|
121 |
+
|
122 |
+
text_probs = (100 * image_features @ text_features.T).softmax(dim=-1)
|
123 |
+
|
124 |
+
# Sort the confidences and get the top 10
|
125 |
+
sorted_confidences = sorted(
|
126 |
+
{items[i]: float(text_probs[0, i]) for i in range(len(items))}.items(),
|
127 |
+
key=lambda x: x[1],
|
128 |
+
reverse=True
|
129 |
+
)
|
130 |
+
top_10_confidences = dict(sorted_confidences[:10])
|
131 |
+
|
132 |
+
return top_10_confidences
|
133 |
+
|
134 |
+
# Gradio interface
|
135 |
+
title = "Fashion Item Classifier with Marqo-FashionSigLIP"
|
136 |
+
description = "Upload an image of a fashion item and classify it using [Marqo-FashionSigLIP](https://huggingface.co/Marqo/marqo-fashionSigLIP)!"
|
137 |
+
|
138 |
+
# Example image paths with thumbnails
|
139 |
+
examples = [
|
140 |
+
["images/dress.jpg", "Dress"],
|
141 |
+
["images/sweatpants.jpg", "Sweatpants"],
|
142 |
+
["images/t-shirt.jpg", "T-Shirt"],
|
143 |
+
["images/hat.jpg", "Hat"],
|
144 |
+
["images/blouse.jpg", "Blouse"],
|
145 |
+
["images/cargo.jpg", "Cargos"],
|
146 |
+
["images/sunglasses.jpg", "Sunglasses"],
|
147 |
+
["images/polo-shirt.jpg", "Polo Shirt"],
|
148 |
+
]
|
149 |
+
|
150 |
+
with gr.Blocks(css="""
|
151 |
+
.remove-btn {
|
152 |
+
font-size: 24px !important; /* Increase the font size of the cross button */
|
153 |
+
line-height: 24px !important;
|
154 |
+
width: 30px !important; /* Increase the width */
|
155 |
+
height: 30px !important; /* Increase the height */
|
156 |
+
}
|
157 |
+
""") as demo:
|
158 |
+
with gr.Row():
|
159 |
+
with gr.Column(scale=1):
|
160 |
+
gr.Markdown(f"# {title}")
|
161 |
+
gr.Markdown(description)
|
162 |
+
gr.Markdown(sidebar_markdown)
|
163 |
+
gr.Markdown(" ", elem_id="vertical-line") # Add an empty Markdown with a custom ID
|
164 |
+
with gr.Column(scale=2):
|
165 |
+
input_image = gr.Image(type="pil", label="Upload Fashion Item Image", height=312)
|
166 |
+
predict_button = gr.Button("Classify")
|
167 |
+
gr.Markdown("Or click on one of the images below to classify it:")
|
168 |
+
gr.Examples(examples=examples, inputs=input_image)
|
169 |
+
# with gr.Column(scale=3):
|
170 |
+
output_label = gr.Label(num_top_classes=6)
|
171 |
+
predict_button.click(predict, inputs=input_image, outputs=output_label)
|
172 |
+
|
173 |
+
|
174 |
+
# Launch the interface
|
175 |
+
demo.launch(share=True)
|
176 |
+
|
images/blouse.jpg
ADDED
![]() |
images/cargo.jpg
ADDED
![]() |
images/dress.jpg
ADDED
![]() |
images/hat.jpg
ADDED
![]() |
images/polo-shirt.jpg
ADDED
![]() |
images/sunglasses.jpg
ADDED
![]() |
images/sweatpants.jpg
ADDED
![]() |
images/t-shirt.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
open_clip_torch
|
2 |
+
transformers
|