Spaces:
Sleeping
Sleeping
Add files.
Browse files- README.md +10 -5
- app.py +90 -0
- images/example-01-two-cats.jpg +0 -0
- images/example-02-two-cats.jpg +0 -0
- images/example-03-woman-with-pink-shirt-and-a-printer.jpg +0 -0
- images/example-04-printer-in-the-left.jpg +0 -0
- images/example-05-car-in-the-left-and-motorbike-in-the-right.jpg +0 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,13 +1,18 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.39.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license: openrail
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Gradio Openai Clip Vit Large Patch14
|
3 |
+
emoji: 🏢
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.39.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: creativeml-openrail-m
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
+
|
15 |
+
# Examples:
|
16 |
+
|
17 |
+
## A. Input example:
|
18 |
+
the photo contains one cat, the photo contains a animal,the photo contains two cats, the photo contains something else
|
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import os
|
4 |
+
|
5 |
+
MODEL_OPENAI_CLIP_VIT_LARGE_PATCH_14 = "openai/clip-vit-large-patch14"
|
6 |
+
MODEL_OPENAI_CLIP_VIT_BASE_PATCH_16 = "openai/clip-vit-base-patch16"
|
7 |
+
MODEL_OPENAI_CLIP_VIT_BASE_PATCH_32 = "openai/clip-vit-base-patch32"
|
8 |
+
|
9 |
+
input_examles = [
|
10 |
+
[
|
11 |
+
os.path.join(os.path.dirname(__file__), "images/example-01-two-cats.jpg"),
|
12 |
+
'the photo contains one cat,' \
|
13 |
+
'the photo contains a animal,' \
|
14 |
+
'the photo contains two cats,' \
|
15 |
+
'the photo contains something else',
|
16 |
+
MODEL_OPENAI_CLIP_VIT_LARGE_PATCH_14
|
17 |
+
],
|
18 |
+
[
|
19 |
+
os.path.join(os.path.dirname(__file__), "images/example-02-two-cats.jpg"),
|
20 |
+
'the photo contains one cat,' \
|
21 |
+
'the photo contains a animal,' \
|
22 |
+
'the photo contains two cats,' \
|
23 |
+
'the photo contains something else',
|
24 |
+
MODEL_OPENAI_CLIP_VIT_BASE_PATCH_16
|
25 |
+
],
|
26 |
+
[
|
27 |
+
os.path.join(os.path.dirname(__file__), "images/example-03-woman-with-pink-shirt-and-a-printer.jpg"),
|
28 |
+
'the photo contains a man and a printer,' \
|
29 |
+
'the photo contains a woman with blue shirt and a printer,' \
|
30 |
+
'the photo contains a woman and a printer,' \
|
31 |
+
'the photo contains a printer,' \
|
32 |
+
'the photo contains a woman with pink shirt and a printer,' \
|
33 |
+
'the photo contains something else',
|
34 |
+
MODEL_OPENAI_CLIP_VIT_LARGE_PATCH_14
|
35 |
+
],
|
36 |
+
[
|
37 |
+
os.path.join(os.path.dirname(__file__), "images/example-04-printer-in-the-left.jpg"),
|
38 |
+
'the photo contains a printer in the left,' \
|
39 |
+
'the photo contains a printer in the right,' \
|
40 |
+
'the photo contains a printer in the middle' \
|
41 |
+
'the photo contains something else',
|
42 |
+
MODEL_OPENAI_CLIP_VIT_LARGE_PATCH_14
|
43 |
+
],
|
44 |
+
[
|
45 |
+
os.path.join(os.path.dirname(__file__), "images/example-05-car-in-the-left-and-motorbike-in-the-right.jpg"),
|
46 |
+
'the photo contains a car in the left and a motorbike in the right,' \
|
47 |
+
'the photo contains a car in the right and a motorbike in the left',
|
48 |
+
MODEL_OPENAI_CLIP_VIT_LARGE_PATCH_14
|
49 |
+
]
|
50 |
+
]
|
51 |
+
|
52 |
+
def launch(image_input, labels_input, dropdown_input):
|
53 |
+
|
54 |
+
return predict(image_input, labels_input, dropdown_input)
|
55 |
+
|
56 |
+
def predict(image_input, labels_input, dropdown_input):
|
57 |
+
|
58 |
+
labels_candidate = labels_input.split(",")
|
59 |
+
|
60 |
+
selected_model = f"""{dropdown_input}"""
|
61 |
+
pipe = pipeline(task="zero-shot-image-classification", model=selected_model)
|
62 |
+
|
63 |
+
predictions = pipe(image_input, candidate_labels=labels_candidate)
|
64 |
+
|
65 |
+
return {p["label"]: p["score"] for p in predictions}
|
66 |
+
|
67 |
+
# SYSTEM INPUTS
|
68 |
+
image_input = gr.Image(label="Upload iamge candidate", type="filepath")
|
69 |
+
text_input = gr.Textbox(type="text", label="Possible classes to detect.")
|
70 |
+
dropdown_input = gr.Dropdown(
|
71 |
+
[
|
72 |
+
MODEL_OPENAI_CLIP_VIT_LARGE_PATCH_14,
|
73 |
+
MODEL_OPENAI_CLIP_VIT_BASE_PATCH_16,
|
74 |
+
MODEL_OPENAI_CLIP_VIT_BASE_PATCH_32
|
75 |
+
],
|
76 |
+
label="OpenAI ClipVit Model")
|
77 |
+
|
78 |
+
# SYSTEM OUTPUTS
|
79 |
+
output_label = gr.outputs.Label()
|
80 |
+
|
81 |
+
demo = gr.Interface(
|
82 |
+
launch,
|
83 |
+
inputs=[image_input, text_input, dropdown_input],
|
84 |
+
outputs=output_label,
|
85 |
+
title="Demo aobut zero-shot-image-classification",
|
86 |
+
examples = input_examles
|
87 |
+
)
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
demo.launch()
|
images/example-01-two-cats.jpg
ADDED
images/example-02-two-cats.jpg
ADDED
images/example-03-woman-with-pink-shirt-and-a-printer.jpg
ADDED
images/example-04-printer-in-the-left.jpg
ADDED
images/example-05-car-in-the-left-and-motorbike-in-the-right.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|