Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
from turtle import title
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
import numpy as np
|
|
@@ -9,21 +11,43 @@ pipe = pipeline("zero-shot-image-classification", model="patrickjohncyh/fashion-
|
|
| 9 |
images="dog.jpg"
|
| 10 |
|
| 11 |
@spaces.GPU
|
| 12 |
-
def shot(
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
labels = labels_text.split(",")
|
|
|
|
|
|
|
| 15 |
res = pipe(images=PIL_image,
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return {dic["label"]: dic["score"] for dic in res}
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
iface.launch()
|
|
|
|
| 1 |
from turtle import title
|
| 2 |
+
import requests
|
| 3 |
+
from io import BytesIO
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import pipeline
|
| 6 |
import numpy as np
|
|
|
|
| 11 |
images="dog.jpg"
|
| 12 |
|
| 13 |
@spaces.GPU
|
| 14 |
+
def shot(input, labels_text):
|
| 15 |
+
# Check if the input is a URL or an uploaded image
|
| 16 |
+
if isinstance(input, str) and (input.startswith("http://") or input.startswith("https://")):
|
| 17 |
+
# Input is a URL
|
| 18 |
+
response = requests.get(input)
|
| 19 |
+
PIL_image = Image.open(BytesIO(response.content)).convert('RGB')
|
| 20 |
+
else:
|
| 21 |
+
# Input is an uploaded image
|
| 22 |
+
PIL_image = Image.fromarray(np.uint8(input)).convert('RGB')
|
| 23 |
+
|
| 24 |
+
# Split labels into a list
|
| 25 |
labels = labels_text.split(",")
|
| 26 |
+
|
| 27 |
+
# Perform the zero-shot image classification
|
| 28 |
res = pipe(images=PIL_image,
|
| 29 |
+
candidate_labels=labels,
|
| 30 |
+
hypothesis_template="This is a photo of a {}")
|
|
|
|
| 31 |
|
| 32 |
+
# Return the classification results as a dictionary
|
| 33 |
+
return {dic["label"]: dic["score"] for dic in res}
|
| 34 |
+
|
| 35 |
+
# Define the Gradio interface
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=shot,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.inputs.Textbox(label="Image URL (starting with http/https) or Upload Image"),
|
| 40 |
+
"text"
|
| 41 |
+
],
|
| 42 |
+
outputs="label",
|
| 43 |
+
examples=[
|
| 44 |
+
["https://example.com/dog.jpg", "dog,cat,bird"],
|
| 45 |
+
["https://example.com/germany.jpg", "germany,belgium,colombia"],
|
| 46 |
+
["https://example.com/colombia.jpg", "germany,belgium,colombia"]
|
| 47 |
+
],
|
| 48 |
+
description="Add an image URL (starting with http/https) or upload a picture, and provide a list of labels separated by commas.",
|
| 49 |
+
title="Zero-shot Image Classification"
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
# Launch the interface
|
| 53 |
iface.launch()
|