| import gradio as gr | |
| from PIL import Image | |
| # Use a pipeline as a high-level helper | |
| from transformers import pipeline | |
| import torch | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| pipe = pipeline("zero-shot-image-classification", model="openai/clip-vit-large-patch14",device=0 if device == "cuda" else -1) | |
| def classify_image(image): | |
| result = pipe(image, ["cat", "dog", "car", "person", "building"]) | |
| return result | |
| demo = gr.Interface(fn=classify_image, inputs=gr.Image(type="pil"), outputs="json") | |
| demo.launch() | |