chris999 commited on
Commit
a132837
·
1 Parent(s): 9a9cb47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -1,15 +1,16 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
 
4
- pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
5
 
6
- def predict(image):
7
- predictions = pipeline(image)
8
- return {p["label"]: p["score"] for p in predictions}
9
 
10
- gr.Interface(
11
- predict,
12
- inputs=gr.inputs.Image(label="Upload hot dog candidate", type="filepath"),
13
- outputs=gr.outputs.Label(num_top_classes=2),
14
- title="Hot Dog? Or Not?",
15
- ).launch()
 
 
 
1
+ from PIL import Image
2
+ import requests
3
 
4
+ from transformers import CLIPProcessor, CLIPModel
5
 
6
+ model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
7
+ processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
 
8
 
9
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
10
+ image = Image.open(requests.get(url, stream=True).raw)
11
+
12
+ inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
13
+
14
+ outputs = model(**inputs)
15
+ logits_per_image = outputs.logits_per_image # this is the image-text similarity score
16
+ probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities