|
import streamlit as st |
|
from transformers import pipeline, AutoProcessor, AutoModelForCausalLM, MBart50TokenizerFast, MBartForConditionalGeneration, AutoProcessor, AutoModel |
|
from PIL import Image |
|
import requests |
|
from IPython.display import Audio |
|
import sys |
|
from PIL import Image |
|
|
|
|
|
image_processor = AutoProcessor.from_pretrained("sezenkarakus/image-GIT-description-model-v3") |
|
image_to_text_model = AutoModelForCausalLM.from_pretrained("sezenkarakus/image-GIT-description-model-v3") |
|
|
|
|
|
ckpt = 'Narrativa/mbart-large-50-finetuned-opus-en-pt-translation' |
|
|
|
tokenizer = MBart50TokenizerFast.from_pretrained(ckpt) |
|
translation_model = MBartForConditionalGeneration.from_pretrained(ckpt) |
|
|
|
tokenizer.src_lang = 'en_XX' |
|
|
|
|
|
audio_processor = AutoProcessor.from_pretrained("suno/bark-small") |
|
audio_model = AutoModel.from_pretrained("suno/bark-small") |
|
|
|
|
|
|
|
|
|
def generate_caption(image): |
|
pixel_values = image_processor(images=image, return_tensors="pt").pixel_values |
|
generated_ids = image_to_text_model.generate(pixel_values=pixel_values, max_length=200) |
|
generated_caption = image_processor.batch_decode(generated_ids, skip_special_tokens=True)[0] |
|
|
|
return generated_caption |
|
|
|
def translate(text): |
|
inputs = tokenizer(text, return_tensors='pt') |
|
input_ids = inputs.input_ids |
|
attention_mask = inputs.attention_mask |
|
|
|
try: |
|
input_ids = input_ids.to('cuda') |
|
attention_mask = attention_mask.to('cuda') |
|
model = translation_model.to("cuda") |
|
except: |
|
print('No NVidia GPU, model performance may not be as good') |
|
model = translation_model |
|
|
|
output = model.generate(input_ids, attention_mask=attention_mask, forced_bos_token_id=tokenizer.lang_code_to_id['pt_XX']) |
|
translated = tokenizer.decode(output[0], skip_special_tokens=True) |
|
|
|
return translated |
|
|
|
|
|
|
|
img_url = 'http://images.cocodataset.org/val2017/000000039769.jpg' |
|
|
|
|
|
|
|
|
|
image = Image.open(requests.get(img_url, stream=True).raw) |
|
|
|
|
|
|
|
caption = generate_caption(image) |
|
print(caption) |
|
|
|
|
|
translated_caption = translate(caption) |
|
print(translated_caption) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|