Views
No views yet
CLIPModel)1from transformers import CLIPProcessor, CLIPModel
2from PIL import Image
3import torch
4
5# Load model and processor
6model_name = "YourOrg/clip-zero-shot-ecommerce-v1"
7model = CLIPModel.from_pretrained(model_name)
8processor = CLIPProcessor.from_pretrained(model_name)
9
10# 1. Define Candidate Labels
11candidate_labels = ["a photo of a ceramic mug", "a photo of a woolen scarf", "a photo of a leather belt"]
12text_inputs = processor(text=candidate_labels, return_tensors="pt", padding=True)
13
14# 2. Load the Image (Conceptual - Replace with actual image loading)
15# image = Image.open("path/to/product_image.jpg")
16dummy_image = Image.new('RGB', (224, 224), color = 'red')
17image_inputs = processor(images=dummy_image, return_tensors="pt")
18
19# 3. Calculate Similarity (Inference)
20with torch.no_grad():
21 outputs = model(**text_inputs, **image_inputs)
22
23logits_per_image = outputs.logits_per_image # (1, num_labels)
24probs = logits_per_image.softmax(dim=1)
25
26# Find the best match
27best_match_index = probs.argmax().item()
28predicted_label = candidate_labels[best_match_index]
29confidence = probs[0][best_match_index].item()
30
31print(f"Predicted Class: {predicted_label} (Confidence: {confidence:.2f})")