Module 11: The local API · Lesson 11.2 · about 30 min
Calling it from Python and scripts
Install Python, send your first question to Gemma 4 from a script, then build a real workflow that summarises a whole folder of files into a spreadsheet, offline.
Module 11 · about 30 minutes · read first: 11.1. You’ll be able to call your local model from Python and run a workflow over a folder of files.
1. Why this matters
This is the step that turns local AI from a chat toy into a tool for your work. A short script can do in ten minutes what would take you a day by hand, on files you would never upload, with no usage limit.
A script is like a very patient office junior with a checklist: open the next file, ask the model, write the answer in the spreadsheet, repeat, and never get bored.
2. Set up Python once
- Install Python 3 from python.org (Windows: tick “Add python.exe to PATH” in the installer). macOS and Linux usually have it already as
python3. - Make a project folder and a virtual environment inside it:
mkdir local-ai && cd local-ai
python -m venv .venv
# Windows: .venv\Scripts\activate Mac/Linux: source .venv/bin/activate
pip install ollama openai
On a Mac, type python3 where these steps say python.
3. Your first script
Save this as hello.py and run python hello.py with Ollama running:
import ollama
reply = ollama.chat(
model="gemma4:e4b",
messages=[{"role": "user", "content": "Give me three tips for writing clear emails."}],
)
print(reply["message"]["content"])
The same thing with the openai library, which works with any engine’s /v1 address (Lesson 11.1):
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama") # the key is ignored locally
reply = client.chat.completions.create(
model="gemma4:e4b",
messages=[{"role": "user", "content": "Give me three tips for writing clear emails."}],
)
print(reply.choices[0].message.content)
Use the first for Ollama-only scripts; use the second when you might switch engines or go to the cloud later (Lesson 11.4).
4. A real workflow: summarise a folder
This script reads every .txt and .md file in a folder, asks the model for a two-sentence summary and three keywords, and writes the results to summaries.csv, which opens in Excel or Google Sheets.
import csv
import sys
from pathlib import Path
import ollama
MODEL = "gemma4:e4b"
MAX_CHARS = 12000 # about 3,000 tokens: keeps each file well inside the context window
def summarise(text: str) -> str:
reply = ollama.chat(
model=MODEL,
messages=[
{"role": "system", "content": "You summarise documents for a busy reader. Be accurate and brief."},
{"role": "user", "content": "Summarise this in two sentences, then give three keywords on a new line "
"starting with 'Keywords:'.\n\n" + text[:MAX_CHARS]},
],
options={"temperature": 0.2},
)
return reply["message"]["content"].strip()
def main(folder: str) -> None:
files = sorted(p for p in Path(folder).iterdir() if p.suffix.lower() in {".txt", ".md"})
with open("summaries.csv", "w", newline="", encoding="utf-8") as out:
writer = csv.writer(out)
writer.writerow(["file", "summary"])
for i, path in enumerate(files, 1):
text = path.read_text(encoding="utf-8", errors="ignore")
writer.writerow([path.name, summarise(text)])
print(f"{i}/{len(files)} {path.name}")
print("Saved summaries.csv")
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else ".")
Find the files. The script lists the folder and keeps the .txt and .md files, sorted by name: say a.txt, b.md and c.txt.
Read one file. It opens a.txt (2,000 tokens) and keeps at most the first 12,000 characters, about 3,000 tokens, so the request always fits the context window.
Ask the model. It sends the system prompt, the instruction and the text to localhost at temperature 0.2. At 400 tokens a second the laptop reads the file in 5 seconds; at 16 tokens a second it writes an 80-token summary in 5 seconds.
Save the row. It writes a.txt and the summary as one row of summaries.csv and prints "1/3 a.txt", so you can see progress.
Repeat. The same for b.md and c.txt: about 10 seconds each, 30 seconds in all. If it stops halfway, the rows already written are safe.
Run it on a folder: python summarise_folder.py "C:\Users\you\Documents\notes" (Windows) or python summarise_folder.py ~/Documents/notes (Mac/Linux). Switch the Wi-Fi off first if you like: it makes no difference. PDFs and Word files need a reading step first, covered in Lesson 12.6.
Maths and principle
Principle: a batch job’s time is the number of items times the time per item, and the time per item is mostly the model writing.
Equation:
Analogy: a checkout queue. Each customer’s time is scanning (reading the file) plus bagging (writing the summary); the queue’s time is the sum.
Try it: 200 files, each 2,000 tokens in and 80 tokens out, reading at 400 tokens per second and writing at 16? (Each: 5 + 5 = 10 s. All: 2,000 s, about 33 minutes.)
- files
- how many items the batch job works through, one request each
- input tokens ÷ reading speed
- the seconds the machine spends reading one file (prefill); fast, often hundreds of tokens a second
- output tokens ÷ writing speed
- the seconds it spends writing one answer; slower, so short answers save the most time
- Physical meaning
- A batch job is a queue at a till: total time is the number of customers times the time each one takes. Asking for a two-sentence summary instead of a page cuts the time per customer, and the whole queue moves faster.
“Connection refused means the script needs the internet.” It means nothing is listening at that local address: Ollama is not running, or the port is wrong. Start the Ollama app (or ollama serve) and run the script again. The internet plays no part.
5. Worked example
Made-up numbers. Ben has 150 meeting notes as text files. He runs the script on his 16 GB laptop with gemma4:e4b before lunch. It takes 22 minutes. He sorts the spreadsheet by keyword and finds every meeting that mentioned the budget in seconds.
6. Practice
- Run
hello.pywith both libraries. - Make a folder with three short text files and run the workflow on it.
- Change the prompt so it also returns “Action needed: yes/no”. Run it again.
- How would you make the script use
gemma4:12binstead?
Answers
- If you see “connection refused”, Ollama is not running: start the app, or
ollama servein another terminal. - Open
summaries.csvin a spreadsheet to check. - Add the request to the user message; Lesson 11.3 shows how to get it back as clean JSON fields instead of text.
- Change
MODEL = "gemma4:12b"(and pull it first).
New words: script (a small program you run), virtual environment (a private set of Python packages for one project), batch job (the same task run over many items), CSV (a plain-text table any spreadsheet opens).
- Install Python once, make a virtual environment, and
pip install ollama openai. ollama.chat(...)talks to Ollama; the openai library withbase_urlset to/v1talks to any engine.- The folder script reads each file, asks for a short summary at a low temperature, and writes one row per file to a CSV.
- Sending files one at a time keeps each request inside the context window and saves progress as it goes.
- Total time ≈ files × (input ÷ reading speed + output ÷ writing speed); short answers save the most time.
Check yourself
Sources
- ollama Python library, https://github.com/ollama/ollama-python
- openai Python library (works with any OpenAI-style server), https://github.com/openai/openai-python
- Python documentation: venv, creation of virtual environments, https://docs.python.org/3/library/venv.html
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. Prices and model names change fast: check the official source before you buy.