Spaces:
Runtime error
Runtime error
Commit
·
98ba976
1
Parent(s):
0a773af
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from turtle import title
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# More models in the model hub.
|
9 |
+
checkpoint = "openai/clip-vit-large-patch14-336"
|
10 |
+
classifier = pipeline("zero-shot-image-classification",
|
11 |
+
model=checkpoint)
|
12 |
+
|
13 |
+
|
14 |
+
def shot(image, labels_text):
|
15 |
+
PIL_image = Image.fromarray(np.uint8(image)).convert('RGB')
|
16 |
+
labels = labels_text.split(",")
|
17 |
+
res = classifier(images=PIL_image,
|
18 |
+
candidate_labels=labels,
|
19 |
+
hypothesis_template= "This is a photo of a {}")
|
20 |
+
return {dic["label"]: dic["score"] for dic in res}
|
21 |
+
|
22 |
+
interface = gr.Interface(shot,
|
23 |
+
inputs=["image", "text"],
|
24 |
+
outputs="label",
|
25 |
+
description="Add a picture and a list of labels separated by commas",
|
26 |
+
examples=[['tundra.jpg', 'Ford F-150, RAM 1500, GMC Sierra, Silverado'],
|
27 |
+
['interior.jpeg', 'Interior,Exterior'],
|
28 |
+
['carplay.webp', 'Carplay, Android play']
|
29 |
+
]
|
30 |
+
title="Zero-shot Image Classification")
|
31 |
+
|
32 |
+
interface.launch()
|