Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers.utils import logging
|
2 |
+
from transformers import AutoProcessor
|
3 |
+
from transformers import CLIPModel
|
4 |
+
import gradio as gr
|
5 |
+
import torch
|
6 |
+
import requests
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
logging.set_verbosity_error()
|
10 |
+
|
11 |
+
model = CLIPModel.from_pretrained(
|
12 |
+
"openai/clip-vit-large-patch14")
|
13 |
+
processor = AutoProcessor.from_pretrained(
|
14 |
+
"openai/clip-vit-large-patch14")
|
15 |
+
|
16 |
+
def process_image(input_type, image_url, image_upload, labels):
|
17 |
+
if input_type == "URL":
|
18 |
+
raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
19 |
+
else:
|
20 |
+
raw_image = image_upload
|
21 |
+
|
22 |
+
labels = [l.strip() for l in labels.split(",")]
|
23 |
+
print(labels)
|
24 |
+
|
25 |
+
image = processor(images=raw_image, return_tensors="pt").pixel_values
|
26 |
+
inputs = processor(text=labels, images=raw_image, return_tensors="pt", padding=True)
|
27 |
+
outputs = model(**inputs)
|
28 |
+
probs = outputs.logits_per_image.softmax(dim=1)[0]
|
29 |
+
probs = list(probs)
|
30 |
+
for i in range(len(labels)):
|
31 |
+
print(f"label: {labels[i]} - probability of detected object being {probs[i].item():.4f}")
|
32 |
+
|
33 |
+
answer = str(labels[probs.index(max(probs))]).capitalize()
|
34 |
+
print(answer)
|
35 |
+
answer = (
|
36 |
+
f"""<div>
|
37 |
+
<h2 style='text-align: center; font-size: 30px; color: blue;'>The detected object is </h2>
|
38 |
+
<h1 style='text-align: center; font-size: 50px; color: orange;'>{answer}</h1>
|
39 |
+
<h2 style='text-align: center; font-size: 30px; color: blue;'> with a probability of </h2>
|
40 |
+
<h1 style='text-align: center; font-size: 50px; color: orange;'>{max(probs)*100:.2f}</h1>
|
41 |
+
</div>"""
|
42 |
+
)
|
43 |
+
return answer
|
44 |
+
|
45 |
+
def display_image_from_url(image_url):
|
46 |
+
if image_url:
|
47 |
+
image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
48 |
+
return image
|
49 |
+
return None
|
50 |
+
|
51 |
+
def toggle_inputs(input_type):
|
52 |
+
if input_type == "URL":
|
53 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
|
54 |
+
else:
|
55 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
56 |
+
|
57 |
+
sample_image = Image.open("./kittens.jpeg")
|
58 |
+
sample_labels = "a photo of a man, a photo of a dog, cats, two cats"
|
59 |
+
|
60 |
+
with gr.Blocks() as demo:
|
61 |
+
gr.Markdown(
|
62 |
+
"""
|
63 |
+
# Give possible labels to the object in the picture - test & demo app by Srinivas.V..
|
64 |
+
Paste either URL of an image or upload the image, type-in your label choices for the image,
|
65 |
+
seperated by comma (',') and submit.
|
66 |
+
""")
|
67 |
+
|
68 |
+
input_type = gr.Radio(choices=["URL", "Upload"], label="Input Type")
|
69 |
+
image_url = gr.Textbox(label="Image URL", visible=False)
|
70 |
+
url_image = gr.Image(value=sample_image, type="pil", label="URL Image", visible=False)
|
71 |
+
image_upload = gr.Image(value=sample_image,type="pil", label="Upload Image", visible=False)
|
72 |
+
labels = gr.Textbox(value=sample_labels, label="Type a list of labels seperated by comma (',')", visible=False, lines=2)
|
73 |
+
|
74 |
+
input_type.change(fn=toggle_inputs, inputs=input_type, outputs=[image_url, url_image, image_upload, labels])
|
75 |
+
image_url.change(fn=display_image_from_url, inputs=image_url, outputs=url_image)
|
76 |
+
|
77 |
+
submit_btn = gr.Button("Submit")
|
78 |
+
processed_image = gr.HTML(label="The Answer")
|
79 |
+
submit_btn.click(fn=process_image, inputs=[input_type, image_url, image_upload, labels], outputs=processed_image)
|
80 |
+
|
81 |
+
demo.launch(debug=True, share=True)
|