Image-Text-to-Text
Transformers
Safetensors
multilingual
internvl_chat
feature-extraction
internvl
custom_code
conversational
Instructions to use OpenGVLab/InternVL2-2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenGVLab/InternVL2-2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="OpenGVLab/InternVL2-2B", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("OpenGVLab/InternVL2-2B", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use OpenGVLab/InternVL2-2B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "OpenGVLab/InternVL2-2B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGVLab/InternVL2-2B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/OpenGVLab/InternVL2-2B
- SGLang
How to use OpenGVLab/InternVL2-2B 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 "OpenGVLab/InternVL2-2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGVLab/InternVL2-2B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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 "OpenGVLab/InternVL2-2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGVLab/InternVL2-2B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use OpenGVLab/InternVL2-2B with Docker Model Runner:
docker model run hf.co/OpenGVLab/InternVL2-2B
Upload folder using huggingface_hub
Browse files
modeling_internvl_chat.py
CHANGED
|
@@ -35,6 +35,7 @@ def version_cmp(v1, v2, op='eq'):
|
|
| 35 |
class InternVLChatModel(PreTrainedModel):
|
| 36 |
config_class = InternVLChatConfig
|
| 37 |
main_input_name = 'pixel_values'
|
|
|
|
| 38 |
_supports_flash_attn_2 = True
|
| 39 |
_no_split_modules = ['InternVisionModel', 'LlamaDecoderLayer', 'InternLM2DecoderLayer']
|
| 40 |
|
|
@@ -101,7 +102,7 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 101 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 102 |
|
| 103 |
image_flags = image_flags.squeeze(-1)
|
| 104 |
-
input_embeds = self.language_model.get_input_embeddings()(input_ids)
|
| 105 |
|
| 106 |
vit_embeds = self.extract_feature(pixel_values)
|
| 107 |
vit_embeds = vit_embeds[image_flags == 1]
|
|
@@ -234,8 +235,8 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 234 |
|
| 235 |
tokenizer.padding_side = 'left'
|
| 236 |
model_inputs = tokenizer(queries, return_tensors='pt', padding=True)
|
| 237 |
-
input_ids = model_inputs['input_ids'].
|
| 238 |
-
attention_mask = model_inputs['attention_mask'].
|
| 239 |
eos_token_id = tokenizer.convert_tokens_to_ids(template.sep)
|
| 240 |
generation_config['eos_token_id'] = eos_token_id
|
| 241 |
generation_output = self.generate(
|
|
@@ -283,8 +284,8 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 283 |
query = query.replace('<image>', image_tokens, 1)
|
| 284 |
|
| 285 |
model_inputs = tokenizer(query, return_tensors='pt')
|
| 286 |
-
input_ids = model_inputs['input_ids'].
|
| 287 |
-
attention_mask = model_inputs['attention_mask'].
|
| 288 |
generation_config['eos_token_id'] = eos_token_id
|
| 289 |
generation_output = self.generate(
|
| 290 |
pixel_values=pixel_values,
|
|
|
|
| 35 |
class InternVLChatModel(PreTrainedModel):
|
| 36 |
config_class = InternVLChatConfig
|
| 37 |
main_input_name = 'pixel_values'
|
| 38 |
+
base_model_prefix = 'language_model'
|
| 39 |
_supports_flash_attn_2 = True
|
| 40 |
_no_split_modules = ['InternVisionModel', 'LlamaDecoderLayer', 'InternLM2DecoderLayer']
|
| 41 |
|
|
|
|
| 102 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 103 |
|
| 104 |
image_flags = image_flags.squeeze(-1)
|
| 105 |
+
input_embeds = self.language_model.get_input_embeddings()(input_ids).clone()
|
| 106 |
|
| 107 |
vit_embeds = self.extract_feature(pixel_values)
|
| 108 |
vit_embeds = vit_embeds[image_flags == 1]
|
|
|
|
| 235 |
|
| 236 |
tokenizer.padding_side = 'left'
|
| 237 |
model_inputs = tokenizer(queries, return_tensors='pt', padding=True)
|
| 238 |
+
input_ids = model_inputs['input_ids'].to(self.device)
|
| 239 |
+
attention_mask = model_inputs['attention_mask'].to(self.device)
|
| 240 |
eos_token_id = tokenizer.convert_tokens_to_ids(template.sep)
|
| 241 |
generation_config['eos_token_id'] = eos_token_id
|
| 242 |
generation_output = self.generate(
|
|
|
|
| 284 |
query = query.replace('<image>', image_tokens, 1)
|
| 285 |
|
| 286 |
model_inputs = tokenizer(query, return_tensors='pt')
|
| 287 |
+
input_ids = model_inputs['input_ids'].to(self.device)
|
| 288 |
+
attention_mask = model_inputs['attention_mask'].to(self.device)
|
| 289 |
generation_config['eos_token_id'] = eos_token_id
|
| 290 |
generation_output = self.generate(
|
| 291 |
pixel_values=pixel_values,
|