Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
shapes = ["leggings", "jogger",
|
9 |
+
"palazzo", "cargo",
|
10 |
+
"dresspants", "chinos"]
|
11 |
+
|
12 |
+
model, preprocess_train, preprocess_val = open_clip.create_model_and_transforms('hf-hub:Marqo/marqo-fashionCLIP')
|
13 |
+
tokenizer = open_clip.get_tokenizer('hf-hub:Marqo/marqo-fashionCLIP')
|
14 |
+
|
15 |
+
|
16 |
+
shapes_desc = list(map(lambda x: "a " + x + " pants shape", shapes))
|
17 |
+
text = tokenizer(shapes_desc)
|
18 |
+
|
19 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
20 |
+
text_features = model.encode_text(text)
|
21 |
+
text_features /= text_features.norm(dim=-1, keepdim=True)
|
22 |
+
|
23 |
+
def predict(inp):
|
24 |
+
image = preprocess_val(inp).unsqueeze(0)
|
25 |
+
|
26 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
27 |
+
image_features = model.encode_image(image)
|
28 |
+
image_features /= image_features.norm(dim=-1, keepdim=True)
|
29 |
+
|
30 |
+
text_probs = (100 * image_features @ text_features.T).softmax(dim=-1)
|
31 |
+
confidences = {shapes[i]: float(text_probs[0, i]) for i in range(6)}
|
32 |
+
return confidences
|
33 |
+
|
34 |
+
|
35 |
+
gr.Interface(fn=predict,
|
36 |
+
inputs=gr.Image(type="pil"),
|
37 |
+
outputs=gr.Label(num_top_classes=6),
|
38 |
+
examples=["imgs/cargo.jpg", "imgs/palazzo.jpg",
|
39 |
+
"imgs/leggings.jpg", "imgs/jogger.jpg",
|
40 |
+
"imgs/chinos.jpg", "imgs/dresspants.jpg"]).launch(share=True)
|