Instructions to use anymodality/llava-v1.5-7b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use anymodality/llava-v1.5-7b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="anymodality/llava-v1.5-7b")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForCausalLM processor = AutoProcessor.from_pretrained("anymodality/llava-v1.5-7b") model = AutoModelForCausalLM.from_pretrained("anymodality/llava-v1.5-7b", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use anymodality/llava-v1.5-7b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "anymodality/llava-v1.5-7b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "anymodality/llava-v1.5-7b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/anymodality/llava-v1.5-7b
- SGLang
How to use anymodality/llava-v1.5-7b with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "anymodality/llava-v1.5-7b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "anymodality/llava-v1.5-7b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "anymodality/llava-v1.5-7b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "anymodality/llava-v1.5-7b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use anymodality/llava-v1.5-7b with Docker Model Runner:
docker model run hf.co/anymodality/llava-v1.5-7b
Download code/inference.py from anymodality/llava-v1.5-7b: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/anymodality/llava-v1.5-7b/resolve/main/code/inference.py
- Command line
-
hf download hf://anymodality/llava-v1.5-7b/code/inference.py
-
curl -L -o inference.py https://huggingface.co/anymodality/llava-v1.5-7b/resolve/main/code/inference.py
3.19 kB
| import requests | |
| from PIL import Image | |
| from io import BytesIO | |
| import torch | |
| from transformers import AutoTokenizer | |
| from llava.model import LlavaLlamaForCausalLM | |
| from llava.utils import disable_torch_init | |
| from llava.mm_utils import tokenizer_image_token, KeywordsStoppingCriteria | |
| from llava.conversation import conv_templates, SeparatorStyle | |
| from llava.constants import ( | |
| IMAGE_TOKEN_INDEX, | |
| DEFAULT_IMAGE_TOKEN, | |
| DEFAULT_IM_START_TOKEN, | |
| DEFAULT_IM_END_TOKEN, | |
| ) | |
| def model_fn(model_dir): | |
| kwargs = {"device_map": "auto"} | |
| kwargs["torch_dtype"] = torch.float16 | |
| model = LlavaLlamaForCausalLM.from_pretrained( | |
| model_dir, low_cpu_mem_usage=True, **kwargs | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False) | |
| vision_tower = model.get_vision_tower() | |
| if not vision_tower.is_loaded: | |
| vision_tower.load_model() | |
| vision_tower.to(device="cuda", dtype=torch.float16) | |
| image_processor = vision_tower.image_processor | |
| return model, tokenizer, image_processor | |
| def predict_fn(data, model_and_tokenizer): | |
| # unpack model and tokenizer | |
| model, tokenizer, image_processor = model_and_tokenizer | |
| # get prompt & parameters | |
| image_file = data.pop("image", data) | |
| raw_prompt = data.pop("question", data) | |
| max_new_tokens = data.pop("max_new_tokens", 1024) | |
| temperature = data.pop("temperature", 0.2) | |
| conv_mode = data.pop("conv_mode", "llava_v1") | |
| if conv_mode == "raw": | |
| # use raw_prompt as prompt | |
| prompt = raw_prompt | |
| stop_str = "###" | |
| else: | |
| conv = conv_templates[conv_mode].copy() | |
| roles = conv.roles | |
| inp = f"{roles[0]}: {raw_prompt}" | |
| inp = ( | |
| DEFAULT_IM_START_TOKEN | |
| + DEFAULT_IMAGE_TOKEN | |
| + DEFAULT_IM_END_TOKEN | |
| + "\n" | |
| + inp | |
| ) | |
| conv.append_message(conv.roles[0], inp) | |
| conv.append_message(conv.roles[1], None) | |
| prompt = conv.get_prompt() | |
| stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2 | |
| if image_file.startswith("http") or image_file.startswith("https"): | |
| response = requests.get(image_file) | |
| image = Image.open(BytesIO(response.content)).convert("RGB") | |
| else: | |
| image = Image.open(image_file).convert("RGB") | |
| disable_torch_init() | |
| image_tensor = ( | |
| image_processor.preprocess(image, return_tensors="pt")["pixel_values"] | |
| .half() | |
| .cuda() | |
| ) | |
| keywords = [stop_str] | |
| input_ids = ( | |
| tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt") | |
| .unsqueeze(0) | |
| .cuda() | |
| ) | |
| stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids) | |
| with torch.inference_mode(): | |
| output_ids = model.generate( | |
| input_ids, | |
| images=image_tensor, | |
| do_sample=True, | |
| temperature=temperature, | |
| max_new_tokens=max_new_tokens, | |
| use_cache=True, | |
| stopping_criteria=[stopping_criteria], | |
| ) | |
| outputs = tokenizer.decode( | |
| output_ids[0, input_ids.shape[1] :], skip_special_tokens=True | |
| ).strip() | |
| return outputs | |