Spaces:
Runtime error
Runtime error
import os | |
import cohere | |
import gradio as gr | |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel | |
COHERE_API_KEY = os.getenv('COHERE_API_KEY') | |
co_client = cohere.Client(COHERE_API_KEY) | |
device = 'cpu' | |
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning" | |
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning" | |
model_checkpoint = "nlpconnect/vit-gpt2-image-captioning" | |
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint) | |
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint) | |
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device) | |
def predict(image): | |
"""Predict the generic image caption from the image """ | |
# image = image.convert('RGB') | |
image = feature_extractor(image, return_tensors="pt").pixel_values.to(device) | |
clean_text = lambda x: x.replace('<|endoftext|>', '').split('\n')[0] | |
caption_ids = model.generate(image, max_length=125)[0] | |
img_caption_text = clean_text(tokenizer.decode(caption_ids)) | |
caption_text = creative_caption(img_caption_text) | |
hashtags = caption_hashtags(img_caption_text) | |
return caption_text, hashtags | |
def creative_caption(text): | |
return co_client.generate(prompt=f"Write some trendy instagram captions for the following prompt - {text}").generations[0].text | |
def caption_hashtags(text): | |
return co_client.generate(prompt=f"Write some trendy instagram hashtags for the following prompt - {text}").generations[0].text | |
input_upload = gr.Image(label="Upload any Image") | |
output = [ | |
gr.Textbox(label="Captions"), | |
gr.Textbox(label="Hashtags"), | |
] | |
title = "Instagram Image Captioning" | |
description = "Made for Linesh" | |
interface = gr.Interface( | |
fn=predict, | |
description=description, | |
inputs=input_upload, | |
theme="grass", | |
outputs=output, | |
title=title, | |
) | |
interface.launch(debug=True) | |