File size: 1,763 Bytes
a09a6e6
a4dc223
 
f30185d
 
 
 
b66857f
f30185d
6e0ffb7
f30185d
 
a792718
a4dc223
 
 
 
 
 
 
 
 
 
 
f30185d
a4dc223
 
f30185d
a4dc223
 
f30185d
a4dc223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f30185d
a4dc223
f30185d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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):
    # Check if the input is a URL or an uploaded image
    if isinstance(input, str) and (input.startswith("http://") or input.startswith("https://")):
        # Input is a URL
        response = requests.get(input)
        PIL_image = Image.open(BytesIO(response.content)).convert('RGB')
    else:
        # Input is an uploaded image
        PIL_image = Image.fromarray(np.uint8(input)).convert('RGB')
    
    # Split labels into a list
    labels = labels_text.split(",")
    
    # Perform the zero-shot image classification
    res = pipe(images=PIL_image, 
               candidate_labels=labels,
               hypothesis_template="This is a photo of a {}")
    
    # Return the classification results as a dictionary
    return {dic["label"]: dic["score"] for dic in res}

# Define the Gradio interface
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"
)

# Launch the interface
iface.launch()