image-post / app.py
dominguezdaniel's picture
Create app.py
0a17934 verified
raw
history blame
970 Bytes
import gradio as gr
from transformers import pipeline
def predict(image):
model_id = "google/vit-base-patch16-224"
classifier = pipeline("image-classification", model=model_id)
predictions = classifier(image)
# Sort predictions based on confidence and select the top one
top_prediction = sorted(predictions, key=lambda x: x['score'], reverse=True)[0]
# Prepare a mockup tweet text
tweet_text = f"Predicted Label: {top_prediction['label']}, Confidence: {top_prediction['score']:.2f}"
return tweet_text
title = "Image Classifier to Tweet"
description = "This demo recognizes and classifies images using the 'google/vit-base-patch16-224' model and generates a mock tweet with the top prediction."
input_component = gr.Image(type="pil", label="Upload an image here")
output_component = gr.Textbox(label="Mock Tweet")
gr.Interface(fn=predict, inputs=input_component, outputs=output_component, title=title, description=description).launch()