DEV Community

wellallyTech
wellallyTech

Posted on

Your Grandma’s New Best Friend: A Private, Offline AI Medication Assistant on Raspberry Pi 🥑

Privacy is not just a feature; it's a human right—especially when it comes to healthcare data. In a world where every "Smart Home" device seems to be a glorified data-harvesting machine, building a private voice assistant that runs entirely offline is the ultimate flex.

Today, we are building a Local Medication Reminder & Interaction Checker. This system uses Edge AI to help elderly users track their pills, check for dangerous drug interactions, and log daily intake—all without a single packet of data leaving your home network. By leveraging Raspberry Pi, Ollama, and Whisper.cpp, we're turning a $70 computer into a life-saving, privacy-first healthcare companion.

Why Edge AI for Healthcare?

When dealing with sensitive medical logs, "the cloud" is just someone else's computer that can be hacked or sold to advertisers. By using a Local LLM and offline Speech-to-Text (STT), we ensure:

  1. Zero Latency: No waiting for a server in Virginia to tell you if you can take Aspirin with Warfarin.
  2. Total Privacy: Your medical history stays on the SD card.
  3. Resilience: It works even if the Wi-Fi goes down.

The System Architecture

To make this work on a resource-constrained device like the Raspberry Pi 5 (or even a Pi 4 with 8GB RAM), we need an efficient pipeline. We use Whisper.cpp for its incredible C++ optimization and Piper for lightning-fast text-to-speech.

graph TD
    A[User Speaks] -->|Audio Capture| B(Whisper.cpp STT)
    B -->|Text String| C{Local LLM - Ollama}
    C -->|Drug Interaction Check| D[Medication Database/Knowledge]
    C -->|Response Text| E(Piper TTS)
    E -->|Audio Out| F[Speaker]
    C -->|Log Entry| G[(Local SQLite/JSON Log)]
    style C fill:#f96,stroke:#333,stroke-width:2px
Enter fullscreen mode Exit fullscreen mode

Prerequisites

Before we dive in, make sure you have:

  • Raspberry Pi 5 (Recommended) or Pi 4 (8GB).
  • USB Microphone & a Speaker (3.5mm or USB).
  • Ollama installed on your Pi.
  • Python 3.10+.
  • Tech Stack: Whisper.cpp, Ollama, Piper TTS, Python.

Step 1: Speech-to-Text with Whisper.cpp

We don't want the heavy Python overhead of the original Whisper. Whisper.cpp is optimized for ARM processors.

# Clone and build whisper.cpp
git clone https://github.com/ggerganov/whisper.cpp
cd whisper.cpp
make
# Download the "base" or "tiny" model for speed
bash ./models/download-ggml-model.sh base.en
Enter fullscreen mode Exit fullscreen mode

You can now run a command to transcribe audio locally. In our final Python script, we'll use a subprocess or a Python binding to capture the "Hey, I just took my Ibuprofen" command.


Step 2: The Brain - Ollama & Drug Interaction Logic

We need a model that is small but smart. Phi-3 or Llama-3-8B (quantized) works wonders here. We will give the LLM a specific "System Prompt" to act as a medical safety checker.

The System Prompt:

"You are a private medical assistant. You track medications. If a user says they are taking a drug, check it against their history for interactions. Be concise and prioritize safety."

import ollama

def check_medication_safety(new_drug, history):
    prompt = f"""
    User wants to take: {new_drug}
    Patient History: {history}
    Are there any known interactions? Answer in 2 sentences max. 
    If safe, say 'SAFE'. If dangerous, say 'WARNING'.
    """

    response = ollama.generate(model='phi3', prompt=prompt)
    return response['response']

# Example usage
current_history = ["Warfarin", "Vitamin D"]
new_input = "Aspirin"
print(check_medication_safety(new_input, current_history))
Enter fullscreen mode Exit fullscreen mode

Step 3: Giving it a Voice with Piper TTS

Google TTS requires an API key. Piper is a local, neural TTS that sounds remarkably human and runs in milliseconds on a Pi.

# Install Piper
echo "I am checking your medication logs now." | \
  ./piper/piper --model en_US-lessac-medium.onnx --output_file welcome.wav
Enter fullscreen mode Exit fullscreen mode

Step 4: The "Glue" Script

Here is a simplified logic flow of our main Python application. This handles the "Listen -> Think -> Speak" loop.

import subprocess
import json
import datetime

def record_and_transcribe():
    # Call whisper.cpp to listen for 5 seconds
    # Simplified for demonstration
    result = subprocess.run(["./whisper.cpp/main", "-m", "models/ggml-base.en.bin", "-t", "4"], capture_output=True)
    return result.stdout.decode('utf-8')

def main_loop():
    print("🚀 Local Health Assistant Active...")
    while True:
        user_text = record_and_transcribe()

        if "took" in user_text.lower():
            # Extract drug name and log it
            response = check_medication_safety(user_text, "Current Logs...")

            # Speak the safety check
            subprocess.run(["./piper/piper", "--model", "model.onnx", "--text", response])

            # Save to local log
            with open("med_logs.json", "a") as f:
                json.dump({"time": str(datetime.datetime.now()), "event": user_text}, f)

if __name__ == "__main__":
    main_loop()
Enter fullscreen mode Exit fullscreen mode

Going Beyond the Prototype 🚀

Building a "hello world" for Edge AI is easy, but making it production-ready for an actual elderly care scenario requires more robust handling of medical ontologies and fail-safe mechanisms.

For more advanced patterns on Edge AI deployment, LLM quantization for ARM, and secure healthcare data architectures, I highly recommend checking out the deep-dive articles at WellAlly Tech Blog. They provide excellent resources on how to scale these local models while maintaining strict HIPAA-like privacy standards in a DIY environment.


Conclusion

We’ve successfully outlined a system that:

  1. Listens using Whisper.cpp.
  2. Reasons about drug safety using Ollama (Phi-3).
  3. Speaks back via Piper TTS.
  4. Stores data locally for 100% privacy.

This project isn't just about code; it's about using Edge AI to provide dignity and safety to those who might struggle with complex medication schedules.

What would you add to this? A pill-bottle camera using the Pi Camera? A notification sent to a family member's phone? Let's discuss in the comments! 👇

Top comments (0)