Spaces:
Sleeping
Sleeping
File size: 1,111 Bytes
dfec65b 87fe2e2 dfec65b 87fe2e2 dfec65b ea9eea4 dfec65b ea9eea4 dfec65b b30a1c0 ea9eea4 dfec65b ea9eea4 |
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 |
import torch
import gradio as gr
from transformers import pipeline
device = "cuda" if torch.cuda.is_available() else "cpu"
def predict(image):
classifier = pipeline(task="image-classification")
preds = classifier(image)
preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]
return preds
description = """
"""
def format_output(output):
# Format the JSON output for better display
formatted_output = ""
for i, pred in enumerate(output):
formatted_output += f"{i}: {{\n"
formatted_output += f" score: {pred['score']},\n"
formatted_output += f" label: {pred['label']}\n"
formatted_output += "}\n"
return formatted_output
# Create the Gradio interface
iface = gr.Interface(
fn=predict,
inputs=[
gr.inputs.Image(label="Image to classify", type="pil"),
],
outputs=gr.outputs.JSON(),
title="Image Classifier",
description=description
)
# Set the post-processing hook to format the output
iface.interface_output_postprocessing = format_output
# Launch the interface
iface.launch()
|