Spaces:
Runtime error
Runtime error
File size: 1,887 Bytes
d89154e e9757be abb46f8 d89154e e9757be 4d75d06 62eb258 e9757be 4d75d06 e9757be 4d75d06 e9757be 4d75d06 e9757be 6702c8a e9757be 6702c8a e9757be b8738cc e9757be e55a64f e9757be 4d75d06 e9757be 2951498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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)
|