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, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("google/gemma-4-31B-it") model = AutoModelForImageTextToText.from_pretrained("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?"} ] }, ] 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
- 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
Jobayer
#85
by Jy0018 - opened
- .eval_results/mmmu_pro.yaml +0 -8
- README.md +1 -3
- chat_template.jinja +10 -26
.eval_results/mmmu_pro.yaml
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
- dataset:
|
| 2 |
-
id: MMMU/MMMU_Pro
|
| 3 |
-
task_id: mmmu_pro_vision
|
| 4 |
-
value: 76.9
|
| 5 |
-
date: '2026-05-12'
|
| 6 |
-
source:
|
| 7 |
-
url: https://huggingface.co/google/gemma-4-31B-it
|
| 8 |
-
name: Model Card
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -3,8 +3,6 @@ library_name: transformers
|
|
| 3 |
license: apache-2.0
|
| 4 |
license_link: https://ai.google.dev/gemma/docs/gemma_4_license
|
| 5 |
pipeline_tag: image-text-to-text
|
| 6 |
-
base_model:
|
| 7 |
-
- google/gemma-4-31B
|
| 8 |
---
|
| 9 |
|
| 10 |
<div align="center">
|
|
@@ -512,4 +510,4 @@ The development of vision-language models (VLMs) raises several ethical concerns
|
|
| 512 |
|
| 513 |
### **Benefits**
|
| 514 |
|
| 515 |
-
At the time of release, this family of models provides high-performance open vision-language model implementations designed from the ground up for responsible AI development compared to similarly sized models.
|
|
|
|
| 3 |
license: apache-2.0
|
| 4 |
license_link: https://ai.google.dev/gemma/docs/gemma_4_license
|
| 5 |
pipeline_tag: image-text-to-text
|
|
|
|
|
|
|
| 6 |
---
|
| 7 |
|
| 8 |
<div align="center">
|
|
|
|
| 510 |
|
| 511 |
### **Benefits**
|
| 512 |
|
| 513 |
+
At the time of release, this family of models provides high-performance open vision-language model implementations designed from the ground up for responsible AI development compared to similarly sized models.
|
chat_template.jinja
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
{%- macro format_parameters(properties, required
|
| 2 |
{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
|
| 3 |
{%- set ns = namespace(found_first=false) -%}
|
| 4 |
{%- for key, value in properties | dictsort -%}
|
| 5 |
{%- set add_comma = false -%}
|
| 6 |
-
{%- if
|
| 7 |
{%- if ns.found_first %},{% endif -%}
|
| 8 |
{%- set ns.found_first = true -%}
|
| 9 |
{{ key }}:{
|
|
@@ -65,7 +65,7 @@
|
|
| 65 |
{%- elif value is mapping -%}
|
| 66 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 67 |
properties:{
|
| 68 |
-
{{- format_parameters(value, value['required'] | default([])
|
| 69 |
}
|
| 70 |
{%- endif -%}
|
| 71 |
{%- if value['required'] -%}
|
|
@@ -178,21 +178,18 @@
|
|
| 178 |
{#- Handle System/Tool Definitions Block -#}
|
| 179 |
{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
|
| 180 |
{{- '<|turn>system\n' -}}
|
|
|
|
| 181 |
{#- Inject Thinking token at the very top of the FIRST system turn -#}
|
| 182 |
{%- if enable_thinking is defined and enable_thinking -%}
|
| 183 |
{{- '<|think|>\n' -}}
|
| 184 |
{%- set ns.prev_message_type = 'think' -%}
|
| 185 |
{%- endif -%}
|
|
|
|
| 186 |
{%- if messages[0]['role'] in ['system', 'developer'] -%}
|
| 187 |
-
{
|
| 188 |
-
{{- messages[0]['content'] | trim -}}
|
| 189 |
-
{%- elif messages[0]['content'] is sequence -%}
|
| 190 |
-
{%- for item in messages[0]['content'] -%}
|
| 191 |
-
{{- item['text'] | trim + ' '-}}
|
| 192 |
-
{%- endfor -%}
|
| 193 |
-
{%- endif -%}
|
| 194 |
{%- set loop_messages = messages[1:] -%}
|
| 195 |
{%- endif -%}
|
|
|
|
| 196 |
{%- if tools -%}
|
| 197 |
{%- for tool in tools %}
|
| 198 |
{{- '<|tool>' -}}
|
|
@@ -201,6 +198,7 @@
|
|
| 201 |
{%- endfor %}
|
| 202 |
{%- set ns.prev_message_type = 'tool' -%}
|
| 203 |
{%- endif -%}
|
|
|
|
| 204 |
{{- '<turn|>\n' -}}
|
| 205 |
{%- endif %}
|
| 206 |
|
|
@@ -295,15 +293,6 @@
|
|
| 295 |
{%- endif -%}
|
| 296 |
{%- endfor -%}
|
| 297 |
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
|
| 298 |
-
{%- for part in tool_body -%}
|
| 299 |
-
{%- if part.get('type') == 'image' -%}
|
| 300 |
-
{{- '<|image|>' -}}
|
| 301 |
-
{%- elif part.get('type') == 'audio' -%}
|
| 302 |
-
{{- '<|audio|>' -}}
|
| 303 |
-
{%- elif part.get('type') == 'video' -%}
|
| 304 |
-
{{- '<|video|>' -}}
|
| 305 |
-
{%- endif -%}
|
| 306 |
-
{%- endfor -%}
|
| 307 |
{%- else -%}
|
| 308 |
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
|
| 309 |
{%- endif -%}
|
|
@@ -313,7 +302,6 @@
|
|
| 313 |
{%- endfor -%}
|
| 314 |
{%- endif -%}
|
| 315 |
|
| 316 |
-
{%- set captured_content -%}
|
| 317 |
{%- if message['content'] is string -%}
|
| 318 |
{%- if role == 'model' -%}
|
| 319 |
{{- strip_thinking(message['content']) -}}
|
|
@@ -340,14 +328,10 @@
|
|
| 340 |
{%- endif -%}
|
| 341 |
{%- endfor -%}
|
| 342 |
{%- endif -%}
|
| 343 |
-
{%- endset -%}
|
| 344 |
-
|
| 345 |
-
{{- captured_content -}}
|
| 346 |
-
{%- set has_content = captured_content | trim | length > 0 -%}
|
| 347 |
|
| 348 |
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
|
| 349 |
{{- '<|tool_response>' -}}
|
| 350 |
-
{%- elif not (ns_tr_out.flag and not
|
| 351 |
{{- '<turn|>\n' -}}
|
| 352 |
{%- endif -%}
|
| 353 |
{%- endif -%}
|
|
@@ -360,4 +344,4 @@
|
|
| 360 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 361 |
{%- endif -%}
|
| 362 |
{%- endif -%}
|
| 363 |
-
{%- endif -%}
|
|
|
|
| 1 |
+
{%- macro format_parameters(properties, required) -%}
|
| 2 |
{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
|
| 3 |
{%- set ns = namespace(found_first=false) -%}
|
| 4 |
{%- for key, value in properties | dictsort -%}
|
| 5 |
{%- set add_comma = false -%}
|
| 6 |
+
{%- if key not in standard_keys -%}
|
| 7 |
{%- if ns.found_first %},{% endif -%}
|
| 8 |
{%- set ns.found_first = true -%}
|
| 9 |
{{ key }}:{
|
|
|
|
| 65 |
{%- elif value is mapping -%}
|
| 66 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 67 |
properties:{
|
| 68 |
+
{{- format_parameters(value, value['required'] | default([])) -}}
|
| 69 |
}
|
| 70 |
{%- endif -%}
|
| 71 |
{%- if value['required'] -%}
|
|
|
|
| 178 |
{#- Handle System/Tool Definitions Block -#}
|
| 179 |
{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
|
| 180 |
{{- '<|turn>system\n' -}}
|
| 181 |
+
|
| 182 |
{#- Inject Thinking token at the very top of the FIRST system turn -#}
|
| 183 |
{%- if enable_thinking is defined and enable_thinking -%}
|
| 184 |
{{- '<|think|>\n' -}}
|
| 185 |
{%- set ns.prev_message_type = 'think' -%}
|
| 186 |
{%- endif -%}
|
| 187 |
+
|
| 188 |
{%- if messages[0]['role'] in ['system', 'developer'] -%}
|
| 189 |
+
{{- messages[0]['content'] | trim -}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
{%- set loop_messages = messages[1:] -%}
|
| 191 |
{%- endif -%}
|
| 192 |
+
|
| 193 |
{%- if tools -%}
|
| 194 |
{%- for tool in tools %}
|
| 195 |
{{- '<|tool>' -}}
|
|
|
|
| 198 |
{%- endfor %}
|
| 199 |
{%- set ns.prev_message_type = 'tool' -%}
|
| 200 |
{%- endif -%}
|
| 201 |
+
|
| 202 |
{{- '<turn|>\n' -}}
|
| 203 |
{%- endif %}
|
| 204 |
|
|
|
|
| 293 |
{%- endif -%}
|
| 294 |
{%- endfor -%}
|
| 295 |
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
{%- else -%}
|
| 297 |
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
|
| 298 |
{%- endif -%}
|
|
|
|
| 302 |
{%- endfor -%}
|
| 303 |
{%- endif -%}
|
| 304 |
|
|
|
|
| 305 |
{%- if message['content'] is string -%}
|
| 306 |
{%- if role == 'model' -%}
|
| 307 |
{{- strip_thinking(message['content']) -}}
|
|
|
|
| 328 |
{%- endif -%}
|
| 329 |
{%- endfor -%}
|
| 330 |
{%- endif -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
|
| 333 |
{{- '<|tool_response>' -}}
|
| 334 |
+
{%- elif not (ns_tr_out.flag and not message.get('content')) -%}
|
| 335 |
{{- '<turn|>\n' -}}
|
| 336 |
{%- endif -%}
|
| 337 |
{%- endif -%}
|
|
|
|
| 344 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 345 |
{%- endif -%}
|
| 346 |
{%- endif -%}
|
| 347 |
+
{%- endif -%}
|