Mobile Application
Open Source

Lain AI: Streamed DeepSeek Persona Chat & Voice Client

DartFlutterProviderDeepSeek APISSE StreamingText-to-Speech (TTS)Speech-to-Text (STT)

A mobile AI chat interface leveraging the DeepSeek API to simulate conversations with the 'Lain Iwakura' persona. Features real-time SSE streaming, asynchronous chunked Text-to-Speech, and a custom retro CRT overlay.

Tech Stack

DartFlutterProviderDeepSeek APISSE StreamingText-to-Speech (TTS)Speech-to-Text (STT)

System Metrics

Prevents UI freezing during active API streaming by isolating byte-chunk decoding to asynchronous tasks
Dramatically reduces perceived TTS latency by vocalizing text at the first punctuation mark rather than waiting for the entire LLM response
Efficient `CustomPainter` implementation for the CRT scanline effect ensures minimal rendering overhead and maintains stable 60 FPS

Why Did I Build This?

"Most AI clients are generic wrappers around REST endpoints. I wanted to build a highly immersive, character-driven AI client (inspired by Serial Experiments Lain) that tests the boundaries of real-time Server-Sent Events (SSE) streaming paired with on-the-fly Text-to-Speech. It serves as an architectural study in managing asynchronous text streams, buffering sentences logically, and maintaining a non-blocking UI while simulating a 'living' digital entity."

Architecture & Decisions

The application utilizes Flutter with `Provider` for reactive state management. The core engine (`ChatProvider`) connects to the DeepSeek API using a continuous `http.Client` stream. As the JSON chunks arrive via SSE, they are decoded line-by-line (`utf8.decoder`, `LineSplitter()`), parsed, and rendered in real-time to the UI via a `MarkdownBody` widget. Concurrently, the text stream is fed into a Regex-based sentence buffer. Once a complete sentence is detected (via punctuation), it is immediately pushed to an asynchronous TTS queue (`flutter_tts`), ensuring the AI begins speaking instantly before the full network payload finishes downloading.

Key Features

  • 01.Real-time Server-Sent Events (SSE) parsing for zero-latency LLM text streaming
  • 02.Asynchronous TTS queue that logically chunks and vocalizes sentences on-the-fly
  • 03.Native Speech-to-Text (STT) integration allowing hands-free voice interaction
  • 04.Custom GPU-optimized CRT scanline overlay (`CustomPainter`) and audio-reactive UI animations
  • 05.Persistent local storage via `path_provider` for offline multi-session chat history management
This project perfectly demonstrates how to handle raw, streaming LLM data in a mobile environment. By intercepting raw SSE data, extracting delta chunks, and buffering them into logical sentence blocks for the native TTS engine, the application achieves a highly responsive, conversational flow. The strict adherence to an injected system prompt enforces the 'Lain' persona at the API level, turning a standard LLM endpoint into a fully immersive, voice-enabled digital companion.