| # Use a pipeline as a high-level helper | |
| from transformers import pipeline | |
| import gradio as gr | |
| image_processor = pipeline("image-classification", model="google/vit-base-patch16-224") | |
| # Define a Gradio function for classification | |
| def classify_image(image): | |
| # Use the image_classification pipeline to classify the image | |
| result = image_processor(image) | |
| # Return the class label and confidence score | |
| return result[0]["label"], round(result[0]["score"], 4) | |
| # Create a Gradio interface | |
| interface = gr.Interface( | |
| fn=classify_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| live=True, | |
| title="Image Classification", | |
| ) | |
| # Start the Gradio interface | |
| interface.launch() |