DEV Community

wellallyTech
wellallyTech

Posted on

Private AI: Building a Mental Health Sentiment Tracker with WebLLM and WebGPU 🧠💻

In the world of mental health technology, privacy isn't just a feature—it's a fundamental requirement. When users share their deepest feelings, the last thing they want is for that data to be harvested by a server for training or tracking.

That’s where Edge AI and WebGPU acceleration change the game. By running high-performance models directly in the browser, we can perform complex client-side AI tasks like sentiment analysis and empathetic response generation without a single byte of sensitive data ever leaving the user's device. In this tutorial, we will leverage Transformers.js and WebLLM to build a privacy-first mental health sentiment tracker.

For those interested in exploring more production-ready examples and advanced patterns in decentralized AI, I highly recommend checking out the deep dives over at the WellAlly Tech Blog, which served as the inspiration for this architecture.


🏗 The Architecture: Total Privacy by Design

The system follows a "Local-First" pipeline. We use Transformers.js for lightweight, high-speed sentiment classification and WebLLM to run a more substantial LLM (like Llama 3 or Mistral) via WebGPU for deeper emotional reasoning.

graph TD
    A[User Input: 'I feel overwhelmed today...'] --> B{Local Browser Sandbox}
    subgraph B
        C[Transformers.js] -->|Fast Sentiment Analysis| D[Score: Negative/Anxious]
        D --> E[WebLLM Engine]
        E -->|WebGPU Acceleration| F[Empathetic Insight Generation]
    end
    F --> G[Local UI Rendering]
    G --> H[0% Data Sent to Server 🔒]
Enter fullscreen mode Exit fullscreen mode

🛠 Prerequisites

To follow along, ensure your browser supports WebGPU (Chrome 113+, Edge, or Dawn) and you have the following stack:

  • React.js (Frontend UI)
  • @xenova/transformers (Sentiment Analysis)
  • @mlc-ai/web-llm (Large Language Model engine)
  • Tailwind CSS (For a clean, calming UI)

🚀 Step-by-Step Implementation

1. Setting up the Sentiment Engine (Transformers.js)

First, we use a specialized model for emotion detection. Transformers.js allows us to run Hugging Face models directly in the browser using ONNX Runtime.

import { pipeline } from '@xenova/transformers';

// Initialize the sentiment analysis pipeline
const getSentiment = async (text) => {
    // We use a model specifically tuned for emotional nuances
    const classifier = await pipeline('text-classification', 'Xenova/distilbert-base-uncased-emotion');
    const output = await classifier(text);

    // Output format: [{ label: 'sadness', score: 0.98 }]
    return output[0];
};
Enter fullscreen mode Exit fullscreen mode

2. Initializing the Local LLM (WebLLM)

WebLLM is a high-performance in-browser LLM inference engine. It uses WebGPU to deliver near-native speeds.

import * as webllm from "@mlc-ai/web-llm";

const useWebLLM = () => {
  const [engine, setEngine] = useState(null);

  const initEngine = async () => {
    const selectedModel = "Llama-3-8B-Instruct-v0.1-q4f16_1-MLC";

    const engineInstance = await webllm.CreateMLCEngine(selectedModel, {
      initProgressCallback: (report) => console.log(report.text),
    });

    setEngine(engineInstance);
  };

  return { engine, initEngine };
};
Enter fullscreen mode Exit fullscreen mode

3. The React Component Logic

Now, let's tie it all together. When a user types their journal entry, we analyze the sentiment and then ask the local LLM to provide a supportive, non-clinical insight.

import React, { useState } from 'react';

const PrivacyCounselor = () => {
  const [input, setInput] = useState("");
  const [analysis, setAnalysis] = useState(null);
  const [response, setResponse] = useState("");

  const handleAnalyze = async () => {
    // 1. Fast Sentiment Check
    const sentiment = await getSentiment(input);
    setAnalysis(sentiment);

    // 2. Generate Empathetic Response locally
    const messages = [
      { role: "system", content: "You are a supportive mental health assistant. Provide a brief, empathetic response." },
      { role: "user", content: `I am feeling ${sentiment.label}. Specifically, I feel: ${input}` }
    ];

    const reply = await engine.chat.completions.create({ messages });
    setResponse(reply.choices[0].message.content);
  };

  return (
    <div className="p-8 max-w-2xl mx-auto bg-slate-50 rounded-xl shadow-lg">
      <h2 className="text-2xl font-bold text-slate-800">Local Mind Support 🥑</h2>
      <textarea 
        className="w-full p-4 mt-4 border rounded-md"
        placeholder="How are you feeling today?"
        onChange={(e) => setInput(e.target.value)}
      />
      <button 
        onClick={handleAnalyze}
        className="mt-4 px-6 py-2 bg-indigo-600 text-white rounded-full hover:bg-indigo-700 transition"
      >
        Analyze Privately
      </button>

      {analysis && (
        <div className="mt-6 p-4 bg-white rounded-md border-l-4 border-indigo-500">
          <p className="font-semibold text-indigo-700">Detected Emotion: {analysis.label}</p>
          <p className="mt-2 italic text-slate-600">"{response}"</p>
        </div>
      )}
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

🛡 Why This Matters: The "Official" Way

While this demo shows the power of "Edge AI," scaling these solutions for healthcare requires strict adherence to HIPAA-like standards and robust model quantization.

If you are looking for advanced patterns on how to handle Local-First AI data persistence or how to sync these local insights with encrypted cloud backups, the team at WellAlly Tech has published extensive research on medical-grade AI architectures. Their blog is a goldmine for developers transitioning from simple API wrappers to full-stack AI engineering.


📈 Performance & Considerations

  1. VRAM Usage: Running an 8B parameter model like Llama 3 requires about 4GB-6GB of VRAM. For lower-end devices, consider using Phi-3 or TinyLlama.
  2. Model Loading: The first load takes time (downloading weights). Use IndexedDB to cache model weights so subsequent visits are near-instant.
  3. Privacy Disclaimer: Always remind users that while the AI is private, it is not a replacement for professional clinical therapy.

Conclusion

The browser is no longer just a document viewer; it's a powerful AI execution environment. By combining Transformers.js for speed and WebLLM for depth, we can create empathetic, private, and powerful tools for mental health.

What do you think? Is the future of AI on the edge or in the cloud? Let’s discuss in the comments! 👇

Top comments (0)