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 def format_output(output): formatted_output = "" for idx, pred in enumerate(output): formatted_output += f"{idx}: Score: {pred['score']}, Label: {pred['label']}\n" return formatted_output description = """ """ iface = gr.Interface( fn=predict, inputs=[ gr.components.Image(label="Image to classify", type="pil"), ], outputs=gr.outputs.JSON(), title="Image Classifier", description=description ) # Apply custom formatting to the JSON output iface.outputs[0].format = format_output iface.launch()