Image_captioning / caption_refiner.py
Prashanthsrn's picture
Update caption_refiner.py
5d238a1 verified
raw
history blame
769 Bytes
from transformers import T5Tokenizer, T5ForConditionalGeneration
import torch
# Load the T5 model and tokenizer
tokenizer = T5Tokenizer.from_pretrained("t5-base")
model = T5ForConditionalGeneration.from_pretrained("t5-base")
def refine_caption(initial_caption):
# Prepare the input for the T5 model
input_text = f"refine caption: {initial_caption}"
inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=512)
# Generate the refined caption
with torch.no_grad():
outputs = model.generate(inputs.input_ids, max_new_tokens=100, num_return_sequences=1)
# Decode the refined caption
refined_caption = tokenizer.decode(outputs[0], skip_special_tokens=True)
return refined_caption