Spaces:
Running
on
Zero
Running
on
Zero
Delay Model Loading Until Inside a GPU Context
Browse files
app.py
CHANGED
@@ -5,29 +5,20 @@ from transformers import CLIPProcessor, CLIPModel
|
|
5 |
|
6 |
# Load the CLIP model and processor on the CPU initially
|
7 |
model_name = "openai/clip-vit-base-patch32"
|
8 |
-
model = CLIPModel.from_pretrained(model_name)
|
9 |
-
processor = CLIPProcessor.from_pretrained(model_name)
|
10 |
|
11 |
@spaces.GPU
|
12 |
def clip_similarity(image, text):
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
"""
|
17 |
-
# Create a torch device for cuda
|
18 |
-
device = torch.device("cuda")
|
19 |
|
20 |
-
|
21 |
model.to(device)
|
22 |
|
23 |
-
# Preprocess the inputs and move tensors to GPU
|
24 |
inputs = processor(text=[text], images=image, return_tensors="pt", padding=True)
|
25 |
-
inputs = {
|
26 |
|
27 |
-
# Run inference
|
28 |
outputs = model(**inputs)
|
29 |
-
|
30 |
-
# Extract similarity score (logits_per_image): higher value indicates better matching
|
31 |
similarity_score = outputs.logits_per_image.detach().cpu().numpy()[0]
|
32 |
return float(similarity_score)
|
33 |
|
|
|
5 |
|
6 |
# Load the CLIP model and processor on the CPU initially
|
7 |
model_name = "openai/clip-vit-base-patch32"
|
|
|
|
|
8 |
|
9 |
@spaces.GPU
|
10 |
def clip_similarity(image, text):
|
11 |
+
# Load the model and processor inside GPU context
|
12 |
+
model = CLIPModel.from_pretrained(model_name)
|
13 |
+
processor = CLIPProcessor.from_pretrained(model_name)
|
|
|
|
|
|
|
14 |
|
15 |
+
device = torch.device("cuda")
|
16 |
model.to(device)
|
17 |
|
|
|
18 |
inputs = processor(text=[text], images=image, return_tensors="pt", padding=True)
|
19 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
20 |
|
|
|
21 |
outputs = model(**inputs)
|
|
|
|
|
22 |
similarity_score = outputs.logits_per_image.detach().cpu().numpy()[0]
|
23 |
return float(similarity_score)
|
24 |
|