🌍"Local server running… because world domination starts on localhost"
Let's run a Powerful Open Model Locally!
In this issue, we will cover:
- Benefits of running Gemma Locally
- Hardware requirement
- Intro on Ollama and ways to run
- List of some helpful commands
After going through a brief understanding of Gemma in the previous blog, it would be a good idea to do some hands-on. Gemma, Google's family of lightweight, state-of-the-art open models inspired by the same technology behind Gemini.
Ever wanted to experiment with a powerful language model without worrying about API keys, usage costs, or sending your data to the cloud? Let's go through the steps needed to use it!
This post will guide you through setting up and running Google's Gemma model right on your own laptop or desktop. It's surprisingly accessible and opens up a world of possibilities.
Before we answer How, let's answer Why.
There are benefits of running the model locally:
- Privacy: The data and prompts remain in the local server, which is crucial for sensitive information or just peace of mind.
- Cost-Effective: Free to download, no API fees, and no per-token charges.
- Offline Access: No internet is needed to use it.
- Control & Customization: direct access that means it can be customized.
- Learning: a great way to understand the model
Hardware Requirement:
- RAM: For Gemma 2B, 8GB of RAM is often the minimum, but 16GB or more is strongly recommended for smoother performance and to run other applications alongside it.
- Disk Space: Gemma 2B is roughly 3-5GB, while Gemma 7B is significantly larger (around 10-15GB), depending on the specific version/quantization.
- CPU: Multi-Core would work
- GPU (optional but recommended)
Running Gemma 2B with Ollama
Run using Command Prompt
Integrate with Code
- Accepting HTTP API requests
- Managing model inheritance
- Managing model sessions/conversations
Process name(System Level)
- ollama
- A child process for model inference (can be threads, depending on the system)
Integrate to Python Web App
from flask import Flask, request, render_template
import requests
app = Flask(__name__)
def query_gemma(prompt):
response = requests.post(
'http://localhost:11434/api/generate',
json={"model": "gemma", "prompt": prompt, "stream": False}
)
return response.json()['response']
@app.route('/', methods=['GET', 'POST'])
def index():
answer = ""
if request.method == 'POST':
user_input = request.form['prompt']
answer = query_gemma(user_input)
return render_template('index.html', answer=answer)
if __name__ == '__main__':
app.run(debug=True)
Bonus 💥
| Command | Description |
|---|---|
| ollama run gemma | Start chatting with Gemma model |
| ollama pull gemma | Download Gemma model locally |
| ollama list | Show all models installed on your machine |
| ollama ps | Show running model sessions (like a "process list") |
| ollama stop | Stop the Ollama server and all sessions |
| ollama rm gemma | Remove (delete) a specific model |
| ollama create | Create a custom model (fine-tune a base model!) |
| ollama server | Start Ollama server manually without running a model |
| ollama help | Show the command help menu |
| ollama pull gemma:2b | Pull a specific version (like 2B variant) |
| ollama run gemma --gpu | Force model to run on GPU |
| ollama run gemma --system "You are a helpful assistant." | Set a system prompt (personality setup) |
| ollama run gemma --temperature 0.2 | Control randomness (lower = more predictable) |
| ollama run gemma --num-predict 512 | Limit the number of tokens to predict |
| ollama run gemma --top-p 0.9 --top-k 40 | Control sampling diversity (fine-tuning generation style) |
| ollama serve --port 12345 | Serve Ollama API on a custom port |
Hope this gives an idea of how to get started with Gemma and integrate into the code.
Comments
Post a Comment