Views
No views yet
1from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration
2import torch
3from PIL import Image
4import requests
5
6# Define your input image and instruction here:
7## image
8url = "https://cdn-uploads.huggingface.co/production/uploads/650801ced5578ef7e20b33d4/bRu85CWwP9129bSCRzos2.png"
9image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
10
11instruction = "What's in the image?"
12
13model_path='AdaptLLM/food-LLaVA-NeXT-Llama3-8B'
14
15# =========================== Do NOT need to modify the following ===============================
16# Load the processor
17processor = LlavaNextProcessor.from_pretrained(model_path)
18
19# Define image token
20image_token = "<|reserved_special_token_4|>"
21
22# Format the prompt
23prompt = (
24 f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n"
25 f"You are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language."
26 f"<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n"
27 f"{image_token}\n{instruction}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
28)
29
30# Load the model
31model = LlavaNextForConditionalGeneration.from_pretrained(model_path, torch_dtype=torch.float16, device_map="auto")
32
33# Prepare inputs and generate output
34inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
35answer_start = int(inputs["input_ids"].shape[-1])
36output = model.generate(**inputs, max_new_tokens=512)
37
38# Decode predictions
39pred = processor.decode(output[0][answer_start:], skip_special_tokens=True)
40print(pred)1@article{adamllm,
2 title={On Domain-Adaptive Post-Training for Multimodal Large Language Models},
3 author={Cheng, Daixuan and Huang, Shaohan and Zhu, Ziyu and Zhang, Xintong and Zhao, Wayne Xin and Luan, Zhongzhi and Dai, Bo and Zhang, Zhenliang},
4 journal={arXiv preprint arXiv:2411.19930},
5 year={2024}
6}1@inproceedings{
2cheng2024adapting,
3title={Adapting Large Language Models via Reading Comprehension},
4author={Daixuan Cheng and Shaohan Huang and Furu Wei},
5booktitle={The Twelfth International Conference on Learning Representations},
6year={2024},
7url={https://openreview.net/forum?id=y886UXPEZ0}
8}