Instructions to use google/gemma-4-31B-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-4-31B-it with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="google/gemma-4-31B-it") 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 AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("google/gemma-4-31B-it") model = AutoModelForMultimodalLM.from_pretrained("google/gemma-4-31B-it", device_map="auto") 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?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- AMD Developer Cloud
- Local Apps Settings
- vLLM
How to use google/gemma-4-31B-it with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-4-31B-it" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-4-31B-it", "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/google/gemma-4-31B-it
- SGLang
How to use google/gemma-4-31B-it 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 "google/gemma-4-31B-it" \ --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": "google/gemma-4-31B-it", "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 "google/gemma-4-31B-it" \ --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": "google/gemma-4-31B-it", "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 google/gemma-4-31B-it with Docker Model Runner:
docker model run hf.co/google/gemma-4-31B-it
Support OpenAI-compatible multimodal content part aliases in chat template
#109
by yasu-oh - opened
- chat_template.jinja +9 -7
chat_template.jinja
CHANGED
|
@@ -296,11 +296,12 @@
|
|
| 296 |
{%- endfor -%}
|
| 297 |
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
|
| 298 |
{%- for part in tool_body -%}
|
| 299 |
-
{%-
|
|
|
|
| 300 |
{{- '<|image|>' -}}
|
| 301 |
-
{%- elif
|
| 302 |
{{- '<|audio|>' -}}
|
| 303 |
-
{%- elif
|
| 304 |
{{- '<|video|>' -}}
|
| 305 |
{%- endif -%}
|
| 306 |
{%- endfor -%}
|
|
@@ -322,19 +323,20 @@
|
|
| 322 |
{%- endif -%}
|
| 323 |
{%- elif message['content'] is sequence -%}
|
| 324 |
{%- for item in message['content'] -%}
|
| 325 |
-
{%-
|
|
|
|
| 326 |
{%- if role == 'model' -%}
|
| 327 |
{{- strip_thinking(item['text']) -}}
|
| 328 |
{%- else -%}
|
| 329 |
{{- item['text'] | trim -}}
|
| 330 |
{%- endif -%}
|
| 331 |
-
{%- elif
|
| 332 |
{{- '<|image|>' -}}
|
| 333 |
{%- set ns.prev_message_type = 'image' -%}
|
| 334 |
-
{%- elif
|
| 335 |
{{- '<|audio|>' -}}
|
| 336 |
{%- set ns.prev_message_type = 'audio' -%}
|
| 337 |
-
{%- elif
|
| 338 |
{{- '<|video|>' -}}
|
| 339 |
{%- set ns.prev_message_type = 'video' -%}
|
| 340 |
{%- endif -%}
|
|
|
|
| 296 |
{%- endfor -%}
|
| 297 |
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
|
| 298 |
{%- for part in tool_body -%}
|
| 299 |
+
{%- set part_type = part.get('type') -%}
|
| 300 |
+
{%- if part_type in ['image', 'image_url'] -%}
|
| 301 |
{{- '<|image|>' -}}
|
| 302 |
+
{%- elif part_type in ['audio', 'audio_url', 'input_audio'] -%}
|
| 303 |
{{- '<|audio|>' -}}
|
| 304 |
+
{%- elif part_type in ['video', 'video_url'] -%}
|
| 305 |
{{- '<|video|>' -}}
|
| 306 |
{%- endif -%}
|
| 307 |
{%- endfor -%}
|
|
|
|
| 323 |
{%- endif -%}
|
| 324 |
{%- elif message['content'] is sequence -%}
|
| 325 |
{%- for item in message['content'] -%}
|
| 326 |
+
{%- set item_type = item.get('type') -%}
|
| 327 |
+
{%- if item_type == 'text' -%}
|
| 328 |
{%- if role == 'model' -%}
|
| 329 |
{{- strip_thinking(item['text']) -}}
|
| 330 |
{%- else -%}
|
| 331 |
{{- item['text'] | trim -}}
|
| 332 |
{%- endif -%}
|
| 333 |
+
{%- elif item_type in ['image', 'image_url'] -%}
|
| 334 |
{{- '<|image|>' -}}
|
| 335 |
{%- set ns.prev_message_type = 'image' -%}
|
| 336 |
+
{%- elif item_type in ['audio', 'audio_url', 'input_audio'] -%}
|
| 337 |
{{- '<|audio|>' -}}
|
| 338 |
{%- set ns.prev_message_type = 'audio' -%}
|
| 339 |
+
{%- elif item_type in ['video', 'video_url'] -%}
|
| 340 |
{{- '<|video|>' -}}
|
| 341 |
{%- set ns.prev_message_type = 'video' -%}
|
| 342 |
{%- endif -%}
|