BUAADreamer/llava-en-zh-2k
Viewer • Updated • 2k • 976 • 10
How to use hiyouga/PaliGemma-3B-Chat-v0.1 with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("image-text-to-text", model="hiyouga/PaliGemma-3B-Chat-v0.1")
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("hiyouga/PaliGemma-3B-Chat-v0.1")
model = AutoModelForImageTextToText.from_pretrained("hiyouga/PaliGemma-3B-Chat-v0.1")
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]:]))How to use hiyouga/PaliGemma-3B-Chat-v0.1 with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "hiyouga/PaliGemma-3B-Chat-v0.1"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "hiyouga/PaliGemma-3B-Chat-v0.1",
"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 run hf.co/hiyouga/PaliGemma-3B-Chat-v0.1
How to use hiyouga/PaliGemma-3B-Chat-v0.1 with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "hiyouga/PaliGemma-3B-Chat-v0.1" \
--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": "hiyouga/PaliGemma-3B-Chat-v0.1",
"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 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 "hiyouga/PaliGemma-3B-Chat-v0.1" \
--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": "hiyouga/PaliGemma-3B-Chat-v0.1",
"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"
}
}
]
}
]
}'How to use hiyouga/PaliGemma-3B-Chat-v0.1 with Docker Model Runner:
docker model run hf.co/hiyouga/PaliGemma-3B-Chat-v0.1
An improved version can be find at BUAADreamer/PaliGemma-3B-Chat-v0.2.
This model is fine-tuned from google/paligemma-3b-mix-448 for multiturn chat completions.
import requests
import torch
from PIL import Image
from transformers import AutoModelForVision2Seq, AutoProcessor, AutoTokenizer, TextStreamer
model_id = "hiyouga/PaliGemma-3B-Chat-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_id)
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
pixel_values = processor(images=[image], return_tensors="pt").to(model.device)["pixel_values"]
messages = [
{"role": "user", "content": "What is in this image?"}
]
input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
image_token_id = tokenizer.convert_tokens_to_ids("<image>")
image_prefix = torch.empty((1, getattr(processor, "image_seq_length")), dtype=input_ids.dtype).fill_(image_token_id)
input_ids = torch.cat((image_prefix, input_ids), dim=-1).to(model.device)
generate_ids = model.generate(input_ids, pixel_values=pixel_values, streamer=streamer, max_new_tokens=50)
We used LLaMA Factory to fine-tune this model. During fine-tuning, we freezed the vision tower and adjusted the parameters in the language model and projector layer.
The following hyperparameters were used during training:
### model
model_name_or_path: google/paligemma-3b-mix-448
visual_inputs: true
### method
stage: sft
do_train: true
finetuning_type: full
### ddp
ddp_timeout: 180000000
deepspeed: examples/deepspeed/ds_z3_config.json
### dataset
dataset: identity,llava_1k_en,llava_1k_zh
template: gemma
cutoff_len: 1536
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: saves/paligemma-chat
logging_steps: 10
save_steps: 100
plot_loss: true
### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 0.00001
num_train_epochs: 3.0
lr_scheduler_type: cosine
warmup_steps: 50
bf16: true
Base model
google/paligemma-3b-mix-448