跳到正文
DEV Community· DEEPANJAN PRASAD·· 3 小時前AI 評分29

Shadow Guardian:使用 Gemma 2、Pygame 和 Rich 建構氣隙隔離的本地 AI 時光膠囊

Shadow Guardian: Building an Air-Gapped, Local AI Time Capsule with Gemma 2, Pygame, and Rich

AI 導讀

Shadow Guardian 是以 Python 建構的離線本地記憶保存 CLI,使用 Ollama 執行 Gemma 2(gemma2:2b),並以 SQLite3 儲存兒童的成長里程碑、引言和回憶。它使用 pygame 播放背景音訊,並以 rich 和自訂打字效果呈現回應;資料保存在加密的本地磁碟區,不連接網絡。作者指出,採用本地開放權重模型可讓家庭記憶留在自己控制的基礎設施內。

正文

Cover image for Shadow Guardian: Building an Air-Gapped, Local AI Time Capsule with Gemma 2, Pygame, and Rich

DEEPANJAN PRASAD

What I Built

I built Shadow Guardian—an air-gapped, local memory-keeping command-line interface (CLI) designed to preserve precious childhood milestones, quotes, and memories for a child, Casey.

Powered entirely offline by a local instance of Gemma 2 (gemma2:2b) running via Ollama and backed by SQLite3, the tool acts as a protective, elegant digital guardian. It greets parents with atmospheric dark-fairy ambiance (using pygame to loop background audio), renders regal responses with a custom typewriter effect, and locks every entry securely inside an un-networked local database.

Demo

Code

Here is a look at the core engine that hooks into Ollama locally, manages SQLite persistence, and handles the atmospheric typewriter pacing:

async def summarize_and_reflect(raw_note: str):
    prompt = f"{SYSTEM_PROMPT}\n\nParent's Note: {raw_note}\nShadow Guardian:"
    payload = {
        "model": MODEL_NAME,
        "prompt": prompt,
        "stream": False
    }

    async with aiohttp.ClientSession() as session:
        try:
            async with session.post(OLLAMA_URL, json=payload) as resp:
                if resp.status == 200:
                    data = await resp.json()
                    return data.get("response", "").strip()
                else:
                    return "🌙 *The guardian glares silently through the mist... (Ollama error)*"
        except Exception:
            return "🌙 *The shadow realm slumbers. Verify Ollama is running at localhost:11434!*"

def dark_typewriter_effect(text: str):
    """Prints response with regal pacing and triggers soft ambient audio."""
    for char in text:
        sys.stdout.write(char)
        sys.stdout.flush()
        if char in ['.', '!', '?']:
            time.sleep(0.12)
        elif char == ',':
            time.sleep(0.05)
        else:
            time.sleep(0.018)
    print()

How I Built It

Shadow Guardian is built with a lightweight, privacy-first Python stack:

  1. Local Inference Engine: Leveraged Ollama running Gemma 2 (gemma2:2b) locally via asynchronous HTTP (aiohttp). No cloud roundtrips, no API keys, and zero telemetry.
  2. Persistence Layer: Used SQLite3 (whispering_spirit.db) for lightweight, self-contained data storage that lives entirely on an encrypted, local volume.
  3. Atmospheric Interface: Integrated pygame to handle looping background audio (mmaudio-a-cute-ghost-story-512522.mp3), giving the CLI an immersive, storybook-like mood whenever a parent interacts with it.
  4. Terminal UI & Pacing: Utilized rich for gorgeous purple panels, status spinners (Weaving dark enchantments...), and paired it with a custom character-by-character typewriter delay for dramatic effect.

Why Does Open Innovation Matter?

When building a digital time capsule or memory vault for your family—especially your children—trust is everything.

Relying on closed APIs means shipping your most intimate family milestones and journal entries to remote, third-party server farms. Open innovation and local open-weight models (gemma2:2b) make it possible to own your infrastructure entirely, ensuring that your memories remain air-gapped, sovereign, and entirely under your control forever.

The Spirit of It

Building Shadow Guardian started as an exploration of local AI tooling, but the moment it clicked was when I tested it with a real milestone for Casey. Hearing the soft, atmospheric audio loop in the background while local Gemma 2 generated a warm, fiercely protective reflection on screen transformed cold terminal text into a living, breathing digital keepsake that will stand the test of time.

來源:DEV Community · dev.to