Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
def predict(image):
|
| 5 |
+
model_id = "google/vit-base-patch16-224"
|
| 6 |
+
classifier = pipeline("image-classification", model=model_id)
|
| 7 |
+
predictions = classifier(image)
|
| 8 |
+
# Sort predictions based on confidence and select the top one
|
| 9 |
+
top_prediction = sorted(predictions, key=lambda x: x['score'], reverse=True)[0]
|
| 10 |
+
# Prepare a mockup tweet text
|
| 11 |
+
tweet_text = f"Predicted Label: {top_prediction['label']}, Confidence: {top_prediction['score']:.2f}"
|
| 12 |
+
return tweet_text
|
| 13 |
+
|
| 14 |
+
title = "Image Classifier to Tweet"
|
| 15 |
+
description = "This demo recognizes and classifies images using the 'google/vit-base-patch16-224' model and generates a mock tweet with the top prediction."
|
| 16 |
+
input_component = gr.Image(type="pil", label="Upload an image here")
|
| 17 |
+
output_component = gr.Textbox(label="Mock Tweet")
|
| 18 |
+
|
| 19 |
+
gr.Interface(fn=predict, inputs=input_component, outputs=output_component, title=title, description=description).launch()
|