image-post / app.py
dominguezdaniel's picture
Update app.py
31a0b27 verified
raw
history blame
1.7 kB
import gradio as gr
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
# Initialize the image classification pipeline
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
# Initialize the tokenizer and model for the generative text (GPT-like model)
tokenizer = AutoTokenizer.from_pretrained("gpt2") # Example model, replace with your choice
model = AutoModelForCausalLM.from_pretrained("gpt2")
def generate_tweet(label):
# Generate a promotional tweet using a GPT-like model
prompt = f"Write a creative and promotional tweet about {label}:"
inputs = tokenizer.encode(prompt, return_tensors="pt")
outputs = model.generate(inputs, max_length=280, num_return_sequences=1)
tweet = tokenizer.decode(outputs[0], skip_special_tokens=True)
return tweet
def predict(image):
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]
label = top_prediction['label'].split(',')[0] # Clean up label if necessary
# Generate the tweet
tweet = generate_tweet(label)
return tweet
title = "Image Classifier to Generative Tweet"
description = "This demo recognizes and classifies images using the 'google/vit-base-patch16-224' model and generates a creative promotional tweet about the top prediction using a generative text model."
input_component = gr.Image(type="pil", label="Upload an image here")
output_component = gr.Textbox(label="Generated Promotional Tweet")
gr.Interface(fn=predict, inputs=input_component, outputs=output_component, title=title, description=description).launch()