Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from turtle import title
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
|
8 |
+
pipe = pipeline("zero-shot-image-classification", model="OFA-Sys/chinese-clip-vit-base-patch16")
|
9 |
+
images="festival.jpg"
|
10 |
+
|
11 |
+
def shot(image, labels_text):
|
12 |
+
PIL_image = Image.fromarray(np.uint8(image)).convert('RGB')
|
13 |
+
labels = labels_text.split(",")
|
14 |
+
res = pipe(images=PIL_image,
|
15 |
+
candidate_labels=labels,
|
16 |
+
hypothesis_template= "This is a photo of a {}")
|
17 |
+
return {dic["label"]: dic["score"] for dic in res}
|
18 |
+
|
19 |
+
iface = gr.Interface(shot,
|
20 |
+
["image", "text"],
|
21 |
+
"label",
|
22 |
+
examples=[["festival.jpg", "灯笼, 鞭炮, 对联"],
|
23 |
+
["cat-dog-music.png", "音乐表演, 体育运动"],
|
24 |
+
["football-match.jpg", "梅西, C罗, 马奎尔"]],
|
25 |
+
description="Add a picture and a list of labels separated by commas",
|
26 |
+
title="Zero-shot Image Classification")
|
27 |
+
|
28 |
+
iface.launch()
|