Sitemap
Venkata Yedida

Venkata Yedida

Using Gemma from HugginFace in Python

3 min readFeb 25, 2024

In this article, I will try to explain step by step how to consume the gemma model from HuggingFace in Python code.

Gemma Graphic image
Geema LLM from Google

There are different ways to consume Gemma models, One of the ways is with HuggingFace and I will try to explain how we can consume them. Google made it easy to consume Gemma LLMs.

It is easy to consume gemma models but make sure you meet some configurations while you are implementing this. Will try to cover the configurations in the article.

Step 1: Project Set up

Create a new project (gemma-test) in IDE (I am using for this example IntelliJ IDEA) and create a new file with the .py extension (gemma-7b.py). We will try to consume the Gemma 7b model from Google.

If you are running the model in CPU, you can use the below code from HuggingFace. Basically in this code, we will consume gemma from transformers.

Note: you can use python3 as an interpreter for this project.

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b")

input_text = "what you know about universe"
input_ids = tokenizer(input_text, return_tensors="pt")

outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
  • We created a new project and Python file with transformer code to consumer Gemma. Select Python as an interpreter in the project, this comes if you already installed Python.

Step 2: Virtual Environment Set up

Before running the gemma-7b.py file. It is good to set up a virtual environment (venv) for several reasons like dependency management and version management.

To set up the virtual environment you can run,

Create the Virtual Environment:

cd path/to/your/project
python3 -m venv venv
source venv/bin/activate # On Linux or macOS
venv\Scripts\activate # On windows
  • We created a virtual environment for Python3 and activated it.

Step 3: Install Dependencies:

Get Venkata Yedida’s stories in your inbox

Join Medium for free to get updates from this writer.

To install the dependencies create a requirements.txt file and this will install dependencies in a new virtual environment. For consuming gemma from HuggingFace we will use a transformer and torch dependencies,

transformers==4.38.1
torch==2.2.1

requirements.txt is used to manage project dependencies. Run the pip command to get installed the dependencies.

pip install --upgrade pip # is used to upgrade python installer for package 
pip install -r requirements.txt
  • We installed the dependencies needed before running the Python file to the consumer gemma model from HuggingFace.

Step 4: Let’s run the gemma.py

To run the gemma-7b.py file created, we can use,

python3 gemma-7b.py

Step5: Final Outcome

Once the model is downloaded and loaded, you will answer for the input_text that is provided in the code (gemma-7b-py).

Press enter or click to view image in full size

Note: The first time when you run the py file it will take time to download the models and they will stored in the cache. When you run it next time it fetches from the cache.

Helpful Troubleshoot steps:

  • Before you run the Python file, log in to HiggingFace with the API key from CLI.
  • The most common error: “ValueError: Tokenizer class GemmaTokenizer does not exist or is not currently imported.” To solve this error try to uninstall the pip transformers package “pip uninstall transformers” and install again by running “pip install -U transformers”. It has solved the error.
  • If you get any errors please feel free to reach out, will try to help.

Summary:

  • We created a new project and Python file with transformer code to consumer Gemma. Select Python as an interpreter in the project, this comes if you already installed Python.
  • We created a virtual environment for Python3 and activated it.
  • We installed the dependencies needed before running the Python file to the consumer gemma model from HuggingFace.

References:

--

--