|
import os |
|
from PIL import Image |
|
import numpy as np |
|
import torch as T |
|
import transformers |
|
|
|
|
|
PATH_LLAVA = '_ckpt/LLaVA-7B-v1' |
|
tokenizer = transformers.AutoTokenizer.from_pretrained(PATH_LLAVA) |
|
model = transformers.AutoModelForCausalLM.from_pretrained(PATH_LLAVA).cuda() |
|
|
|
def remove_alter(s): |
|
if 'ASSISTANT:' in s: s = s[s.index('ASSISTANT:')+10:].strip() |
|
if '</s>' in s: s = s[:s.index('</s>')].strip() |
|
if 'alternative' in s.lower(): s = s[:s.lower().index('alternative')] |
|
if '[IMG0]' in s: s = s[:s.index('[IMG0]')] |
|
s = '.'.join([s.strip() for s in s.split('.')[:2]]) |
|
if s[-1]!='.': s += '.' |
|
return s.strip() |
|
|
|
def load_image_and_generate_instruction(image_path): |
|
|
|
img = Image.open(image_path) |
|
img.show() |
|
|
|
|
|
|
|
|
|
instruction = "Describe what to do with this image." |
|
|
|
|
|
expressive_instruction = remove_alter(instruction) |
|
print("Expressive Instruction:", expressive_instruction) |
|
|
|
|
|
image_path = './path/to/your/image.jpg' |
|
load_image_and_generate_instruction(image_path) |
|
|