Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -12,7 +12,7 @@ from fastapi import FastAPI, File, Form, UploadFile
|
|
| 12 |
from fastapi.middleware.cors import CORSMiddleware
|
| 13 |
from pydantic import BaseModel
|
| 14 |
from PIL import Image
|
| 15 |
-
from rembg import remove
|
| 16 |
import torch
|
| 17 |
from transformers import CLIPProcessor, CLIPModel
|
| 18 |
from supabase import create_client
|
|
@@ -24,7 +24,10 @@ supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
|
| 24 |
|
| 25 |
### βββ CLIP zero-shot setup ββββββββββββββββββββββββββ
|
| 26 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
-
clip_model = CLIPModel.from_pretrained(
|
|
|
|
|
|
|
|
|
|
| 28 |
clip_proc = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 29 |
|
| 30 |
CATEGORIES = [
|
|
@@ -54,7 +57,6 @@ class Processed(BaseModel):
|
|
| 54 |
category: str
|
| 55 |
|
| 56 |
def remove_bg_and_composite(img: Image.Image) -> Image.Image:
|
| 57 |
-
"""Remove the background and composite on white."""
|
| 58 |
img = img.convert("RGBA")
|
| 59 |
fg = remove(img)
|
| 60 |
white = Image.new("RGBA", fg.size, (255,255,255,255))
|
|
@@ -70,12 +72,13 @@ def classify(img: Image.Image) -> str:
|
|
| 70 |
)
|
| 71 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 72 |
with torch.no_grad():
|
| 73 |
-
img_feats = clip_model.get_image_features(
|
| 74 |
txt_feats = clip_model.get_text_features(
|
| 75 |
-
|
|
|
|
| 76 |
)
|
| 77 |
-
img_feats = img_feats
|
| 78 |
-
txt_feats = txt_feats
|
| 79 |
sims = (img_feats @ txt_feats.T).squeeze(0)
|
| 80 |
return CATEGORIES[sims.argmax().item()]
|
| 81 |
|
|
|
|
| 12 |
from fastapi.middleware.cors import CORSMiddleware
|
| 13 |
from pydantic import BaseModel
|
| 14 |
from PIL import Image
|
| 15 |
+
from rembg import remove
|
| 16 |
import torch
|
| 17 |
from transformers import CLIPProcessor, CLIPModel
|
| 18 |
from supabase import create_client
|
|
|
|
| 24 |
|
| 25 |
### βββ CLIP zero-shot setup ββββββββββββββββββββββββββ
|
| 26 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
+
clip_model = CLIPModel.from_pretrained(
|
| 28 |
+
"openai/clip-vit-base-patch32",
|
| 29 |
+
from_tf=True # β load the TF weights as PyTorch
|
| 30 |
+
).to(device)
|
| 31 |
clip_proc = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 32 |
|
| 33 |
CATEGORIES = [
|
|
|
|
| 57 |
category: str
|
| 58 |
|
| 59 |
def remove_bg_and_composite(img: Image.Image) -> Image.Image:
|
|
|
|
| 60 |
img = img.convert("RGBA")
|
| 61 |
fg = remove(img)
|
| 62 |
white = Image.new("RGBA", fg.size, (255,255,255,255))
|
|
|
|
| 72 |
)
|
| 73 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 74 |
with torch.no_grad():
|
| 75 |
+
img_feats = clip_model.get_image_features(pixel_values=inputs["pixel_values"])
|
| 76 |
txt_feats = clip_model.get_text_features(
|
| 77 |
+
input_ids=inputs["input_ids"],
|
| 78 |
+
attention_mask=inputs["attention_mask"],
|
| 79 |
)
|
| 80 |
+
img_feats = img_feats / img_feats.norm(dim=-1, keepdim=True)
|
| 81 |
+
txt_feats = txt_feats / txt_feats.norm(dim=-1, keepdim=True)
|
| 82 |
sims = (img_feats @ txt_feats.T).squeeze(0)
|
| 83 |
return CATEGORIES[sims.argmax().item()]
|
| 84 |
|