Update app.py
Browse files
app.py
CHANGED
|
@@ -8,19 +8,20 @@ model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
|
| 8 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 9 |
|
| 10 |
# Define predict function
|
| 11 |
-
def predict(image,
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
else:
|
| 15 |
-
labels = candidate_labels
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
|
| 18 |
outputs = model(**inputs)
|
| 19 |
-
|
| 20 |
logits_per_image = outputs.logits_per_image
|
| 21 |
probs = logits_per_image.softmax(dim=1)[0].tolist()
|
|
|
|
| 22 |
|
| 23 |
-
return [{"label": lbl, "score": round(score, 4)} for lbl, score in zip(labels, probs)]
|
| 24 |
|
| 25 |
# Define the Gradio interface
|
| 26 |
demo = gr.Interface(
|
|
|
|
| 8 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 9 |
|
| 10 |
# Define predict function
|
| 11 |
+
def predict(image, labels):
|
| 12 |
+
print("Received image:", image)
|
| 13 |
+
print("Received labels:", labels)
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
if not labels or not image:
|
| 16 |
+
return [{"label": "Invalid input", "score": 0.0}]
|
| 17 |
+
|
| 18 |
+
labels = labels.split(",")
|
| 19 |
inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
|
| 20 |
outputs = model(**inputs)
|
|
|
|
| 21 |
logits_per_image = outputs.logits_per_image
|
| 22 |
probs = logits_per_image.softmax(dim=1)[0].tolist()
|
| 23 |
+
return [{"label": lbl.strip(), "score": round(score, 4)} for lbl, score in zip(labels, probs)]
|
| 24 |
|
|
|
|
| 25 |
|
| 26 |
# Define the Gradio interface
|
| 27 |
demo = gr.Interface(
|