Views
No views yet
https://github.com/LLaVA-VL/LLaVA-NeXTtransformers library (v4.40.0).1from llava.model.builder import load_pretrained_model
2tokenizer, model, image_processor, max_length = load_pretrained_model(
3 model_name_or_path="inaf-oact-ai/radiollava-7b-qacapt",
4 model_base=None,
5 model_name="llava_qwen",
6 device_map="auto"
7)1import torch
2from PIL import Image
3from llava.model.builder import load_pretrained_model
4from llava.mm_utils import process_images, tokenizer_image_token
5from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN
6from llava.conversation import conv_templates
7# - Load model
8tokenizer, model, image_processor, max_length = load_pretrained_model(
9 model_name_or_path="inaf-oact-ai/radiollava-7b-qa",
10 model_base=None,
11 model_name="llava_qwen",
12 device_map="auto"
13)
14# - Load image
15image_path= ...
16image= Image.fromarray(data).convert("RGB")
17# - Process image
18image_tensor = process_images([image], image_processor, model.config)
19image_tensor = [_image.to(dtype=torch.float16, device=model.device) for _image in image_tensor]
20# - Create prompt
21query= "Describe the input image" # Replace it with your query
22question = DEFAULT_IMAGE_TOKEN + "\n" + query
23conv = copy.deepcopy(conv_templates[conv_template])
24conv.system= '<|im_start|>system\nYou are an AI assistant specialized in radio astronomical topics.'
25conv.append_message(conv.roles[0], question)
26conv.append_message(conv.roles[1], None)
27prompt_question = conv.get_prompt()
28# - Create model inputs
29input_ids = tokenizer_image_token(prompt_question, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(model.device)
30image_sizes = [image.size]
31# - Generate model response
32# Change generation parameters as you wish
33do_sample=True
34temperature= 0.3
35max_new_tokens=4096
36output = model.generate(
37 input_ids,
38 images=image_tensor,
39 image_sizes=image_sizes,
40 do_sample=do_sample,
41 temperature=temperature if do_sample else None,
42 max_new_tokens=max_new_tokens,
43)
44output_parsed= tokenizer.decode(output[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
45
46# - Process response as you wish ...
47#response= output_parsed.strip("\n").strip()https://github.com/LLaVA-VL/LLaVA-NeXT/blob/main/docs/LLaVA_OneVision_Tutorials.ipynbhttps://github.com/SKA-INAF/radio-llava.git