|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
checkpoint = "openai/clip-vit-large-patch14" |
|
|
detector = pipeline(model=checkpoint, task="zero-shot-image-classification") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def classify_image(image): |
|
|
try: |
|
|
labels = ["cat", "dog"] |
|
|
image = image.convert("RGB") |
|
|
results = detector(image, candidate_labels=labels) |
|
|
return {res["label"]: round(res["score"], 3) for res in results} |
|
|
except Exception as e: |
|
|
return {"error": str(e)} |
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface(fn=classify_image, inputs=gr.Image(type="pil"), outputs=gr.Label()) |
|
|
iface.launch() |
|
|
|