Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,32 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import CLIPProcessor, CLIPModel
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
|
6 |
+
# Load the model and processor
|
7 |
+
model_id = "DGurgurov/clip-vit-base-patch32-oxford-pets"
|
8 |
+
model = CLIPModel.from_pretrained(model_id)
|
9 |
+
processor = CLIPProcessor.from_pretrained(model_id)
|
10 |
+
|
11 |
+
# Define the inference function
|
12 |
+
def predict(image):
|
13 |
+
inputs = processor(images=image, return_tensors="pt")
|
14 |
+
outputs = model.get_image_features(**inputs)
|
15 |
+
logits_per_image = outputs.logits_per_image
|
16 |
+
probs = torch.nn.functional.softmax(logits_per_image, dim=1)
|
17 |
+
return {f"Class {i}": prob.item() for i, prob in enumerate(probs[0])}
|
18 |
+
|
19 |
+
# Define Gradio interface
|
20 |
+
image = gr.inputs.Image(type="pil")
|
21 |
+
label = gr.outputs.Label(num_top_classes=5)
|
22 |
+
|
23 |
+
interface = gr.Interface(
|
24 |
+
fn=predict,
|
25 |
+
inputs=image,
|
26 |
+
outputs=label,
|
27 |
+
title="CLIP Model - Oxford Pets",
|
28 |
+
description="Upload an image and get the top 5 class predictions."
|
29 |
+
)
|
30 |
+
|
31 |
+
# Launch the Gradio app
|
32 |
+
interface.launch()
|