|
from turtle import title |
|
import requests |
|
from io import BytesIO |
|
import gradio as gr |
|
from transformers import pipeline |
|
import numpy as np |
|
from PIL import Image |
|
import spaces |
|
|
|
pipe = pipeline("zero-shot-image-classification", model="patrickjohncyh/fashion-clip") |
|
images="dog.jpg" |
|
|
|
@spaces.GPU |
|
def shot(input, labels_text): |
|
|
|
if isinstance(input, str) and (input.startswith("http://") or input.startswith("https://")): |
|
|
|
response = requests.get(input) |
|
PIL_image = Image.open(BytesIO(response.content)).convert('RGB') |
|
else: |
|
|
|
PIL_image = Image.fromarray(np.uint8(input)).convert('RGB') |
|
|
|
|
|
labels = labels_text.split(",") |
|
|
|
|
|
res = pipe(images=PIL_image, |
|
candidate_labels=labels, |
|
hypothesis_template="This is a photo of a {}") |
|
|
|
|
|
return {dic["label"]: dic["score"] for dic in res} |
|
|
|
|
|
iface = gr.Interface( |
|
fn=shot, |
|
inputs=[ |
|
gr.inputs.Textbox(label="Image URL (starting with http/https) or Upload Image"), |
|
"text" |
|
], |
|
outputs="label", |
|
examples=[ |
|
["https://example.com/dog.jpg", "dog,cat,bird"], |
|
["https://example.com/germany.jpg", "germany,belgium,colombia"], |
|
["https://example.com/colombia.jpg", "germany,belgium,colombia"] |
|
], |
|
description="Add an image URL (starting with http/https) or upload a picture, and provide a list of labels separated by commas.", |
|
title="Zero-shot Image Classification" |
|
) |
|
|
|
|
|
iface.launch() |