Instructions to use google/gemma-2b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-2b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="google/gemma-2b")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b") model = AutoModelForCausalLM.from_pretrained("google/gemma-2b") - llama-cpp-python
How to use google/gemma-2b with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="google/gemma-2b", filename="gemma-2b.gguf", )
output = llm( "Once upon a time,", max_tokens=512, echo=True ) print(output)
- Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- llama.cpp
How to use google/gemma-2b with llama.cpp:
Install from brew
brew install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf google/gemma-2b # Run inference directly in the terminal: llama-cli -hf google/gemma-2b
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf google/gemma-2b # Run inference directly in the terminal: llama-cli -hf google/gemma-2b
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf google/gemma-2b # Run inference directly in the terminal: ./llama-cli -hf google/gemma-2b
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf google/gemma-2b # Run inference directly in the terminal: ./build/bin/llama-cli -hf google/gemma-2b
Use Docker
docker model run hf.co/google/gemma-2b
- LM Studio
- Jan
- vLLM
How to use google/gemma-2b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-2b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-2b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/google/gemma-2b
- SGLang
How to use google/gemma-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 "google/gemma-2b" \ --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": "google/gemma-2b", "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 "google/gemma-2b" \ --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": "google/gemma-2b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Ollama
How to use google/gemma-2b with Ollama:
ollama run hf.co/google/gemma-2b
- Unsloth Studio new
How to use google/gemma-2b with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for google/gemma-2b to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for google/gemma-2b to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for google/gemma-2b to start chatting
- Docker Model Runner
How to use google/gemma-2b with Docker Model Runner:
docker model run hf.co/google/gemma-2b
- Lemonade
How to use google/gemma-2b with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull google/gemma-2b
Run and chat with the model
lemonade run user.gemma-2b-{{QUANT_TAG}}List all available models
lemonade list
Cannot access gated repo issue!
It(The exact file, codes, and the gradio environment) worked on my local device just fine but when I was trying to run/deploy the space here, it gave me the following error: "Cannot access gated repo for URL https://huggingface.co/google/gemma-2b/resolve/main/config.json."
Repo model google/gemma-2b is gated. You must be authenticated to access it.
So, to solve that, I added a secret key to my 'space' setting and passed it to the huggingface_hub login like this:
HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
login(token=HUGGINGFACE_TOKEN)
It didn't solve the issue. Could anyone please help me with that? Thanks in advance!
Running into the same issue, bypass was to get through Kaggle.
Running into the same issue, the bypass was to get through Kaggle.
Were you able to deploy it here? I could run it on Kaggle and also on my local PC, but not here on HuggingFace.
Hi @asif00 , sorry you're facing this issue. Could you create a token in your user settings (https://huggingface.co/settings/tokens) and set it as HF_TOKEN secret in your Space. No need to use login(...) anymore when doing so. If you have yourself access to https://huggingface.co/google/gemma-2b/tree/main from the browser (which seems to be the case), it should work in the Space as well. Please let me know if this doesn't solve your issue.
Also failed with Cannot access gated repo for url https://huggingface.co/google/gemma-2b/resolve/main/config.json. Repo model google/gemma-2b is gated. You must be authenticated to access it.
running local and my code as follow:
from transformers import AutoTokenizer, AutoModelForCausalLM
access_token='hf_******'
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b")
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", token=access_token)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
@chuanfengx64 Could you check this Space I've just created: https://huggingface.co/spaces/Wauplin/tmp_for_gemma_discussion_11/blob/main/app.py.
Instead of hardcoding my user token, I set it as HF_TOKEN in the Space secrets (e.g. in https://huggingface.co/spaces/Wauplin/tmp_for_gemma_discussion_11/settings) which a valid token that I generated for it. And then everything works fine and the files are downloaded correctly.
Also failed with
Cannot access gated repo for url https://huggingface.co/google/gemma-2b/resolve/main/config.json. Repo model google/gemma-2b is gated. You must be authenticated to access it.running local and my code as follow:
from transformers import AutoTokenizer, AutoModelForCausalLM access_token='hf_******' tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b") model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", token=access_token) input_text = "Write me a poem about Machine Learning." input_ids = tokenizer(input_text, return_tensors="pt") outputs = model.generate(**input_ids) print(tokenizer.decode(outputs[0]))
Oh! tokenizer should be also with token!!
@Yhnf How do you try to access it? If I go to https://huggingface.co/spaces/Wauplin/tmp_for_gemma_discussion_11 with incognito mode (e.g. not logged in), it works for me. This Space is not really interesting though, it was just to demonstrate how to load gemma weights in transformers in a Space by handling authentication with a HF_TOKEN secret.
No worries, glad it's figured out :)
Hi @asif00 , sorry you're facing this issue. Could you create a token in your user settings (https://huggingface.co/settings/tokens) and set it as
HF_TOKENsecret in your Space. No need to uselogin(...)anymore when doing so. If you have yourself access to https://huggingface.co/google/gemma-2b/tree/main from the browser (which seems to be the case), it should work in the Space as well. Please let me know if this doesn't solve your issue.
@Wauplin Thank you so much for your kind response. It worked like a charm!