Joshnicholas commited on
Commit
89e5e58
·
verified ·
1 Parent(s): 920b51d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Stolen from https://huggingface.co/spaces/Datatrooper/zero-shot-image-classification/tree/main
2
+
3
+ from turtle import title
4
+ import gradio as gr
5
+ from transformers import pipeline
6
+ import numpy as np
7
+ from PIL import Image
8
+
9
+
10
+ pipe = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
11
+ images="dog.jpg"
12
+
13
+ def shot(image, labels_text):
14
+ PIL_image = Image.fromarray(np.uint8(image)).convert('RGB')
15
+ labels = labels_text.split(",")
16
+ res = pipe(images=PIL_image,
17
+ candidate_labels=labels,
18
+ hypothesis_template= "This is a photo of a {}")
19
+ return {dic["label"]: dic["score"] for dic in res}
20
+
21
+ iface = gr.Interface(shot,
22
+ ["image", "text"],
23
+ "label",
24
+ examples=[["dog.jpg", "dog,cat,bird"],
25
+ ["germany.jpg", "germany,belgium,colombia"],
26
+ ["colombia.jpg", "germany,belgium,colombia"]],
27
+ description="Add a picture and a list of labels separated by commas",
28
+ title="Zero-shot Image Classification")
29
+
30
+ iface.launch()