Skip to content

Add TFLite lightweight local AI solution with user-imported models#42

Draft
Copilot wants to merge 10 commits intomainfrom
copilot/implement-tflite-lightweight-imports
Draft

Add TFLite lightweight local AI solution with user-imported models#42
Copilot wants to merge 10 commits intomainfrom
copilot/implement-tflite-lightweight-imports

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 28, 2025

Implements 方案 D: TFLite-based on-device AI with user-imported models for embedding, ASR, and OCR. No pre-bundled models—users download/import as needed, keeping the app lightweight (50-100MB total depending on choices).

Core Services

  • LocalEmbeddingService: TFLite inference for paraphrase-multilingual-MiniLM (25MB, 384d, 50+ languages). O(1) vocab lookups via Map.
  • LocalSpeechRecognitionService: Interface for user-imported Whisper ONNX (tiny 39MB / base 74MB)
  • LocalOCRService: Supports Tesseract traineddata or PaddleOCR Lite (~8MB)
  • LocalVectorStorageService: SQLite BLOB storage with brute-force cosine similarity (suitable for <10k notes)
  • LocalAIModelManager: File picker-based model import with validation

Models

  • LocalAIModelConfig: Model paths, status, metadata
  • Extended LocalAISettings with modelConfig property
  • LocalAIModelRecommendations: Download URLs for recommended models

UI

Model management section in LocalAISettingsPage:

  • Per-model status (Ready/Not installed)
  • Import/delete actions
  • Storage statistics
  • Recommendation dialogs with copy-to-clipboard download links

Usage

// Initialize embedding service with user-provided model
final embeddingService = LocalEmbeddingService();
await embeddingService.initialize(
  '/path/to/model.tflite',
  '/path/to/vocab.txt',
);

// Generate embeddings
final embedding = await embeddingService.generateEmbedding('Hello world');

// Store and search
await vectorStorage.storeEmbedding(noteId, embedding!);
final results = await vectorStorage.searchSimilar(queryEmbedding, topK: 10);

Dependencies

  • tflite_flutter: ^0.10.4 (Apache-2.0)
  • record: ^5.1.0

Limitations

  • No LLM support (纠错、来源识别 not available)
  • Brute-force search—consider dedicated vector DB for >10k notes
Original prompt

方案 D:TFLite 轻量级(全用户导入)

技术栈

  • 嵌入: tflite_flutter (Apache-2.0) - paraphrase-multilingual-MiniLM (TFLite 25MB)
  • ASR: sherpa_onnx 推理用户导入的 Whisper ONNX 模型
  • OCR: 用户导入 Tesseract traineddata 或 PaddleOCR Lite 模型
  • 向量存储: SQLite 暴力搜索

核心实现

1. 多语言嵌入 (TFLite)

模型: paraphrase-multilingual-MiniLM-L12-v2 TFLite 版

  • 量化后 25MB, 384维
  • 支持50+语言
  • 用户在应用内下载或手动导入 .tflite 文件
class LocalEmbeddingService {
  Future<void> initialize(String userTflitePath, String vocabPath);
  Future<List<double>> generateEmbedding(String text);
}

2. ASR (用户导入模型)

用户导入 Whisper ONNX 模型 (tiny 39MB / base 74MB):

class LocalSpeechRecognitionService {
  Future<void> initialize(String userWhisperPath);
  Future<String> transcribeFile(String audioPath);
}

3. OCR (用户导入模型)

选项 A: Tesseract traineddata

await FlutterTesseractOcr.extractText(
  imagePath,
  language: 'chi_sim+eng',
  args: {'tessdata': userTessdataPath},
);

选项 B: PaddleOCR Lite (~8MB, 中文识别更优)

// 使用 paddle_ocr_flutter 或 tflite_flutter 加载用户导入的模型

4. SQLite 向量存储

暴力搜索(适合 < 10k 笔记):

// 数据库存储 BLOB 格式向量
CREATE TABLE note_embeddings (
  note_id TEXT PRIMARY KEY,
  embedding BLOB NOT NULL
)

// 搜索时遍历计算余弦相似度

依赖配置

dependencies:
  tflite_flutter: ^0.10.4
  sherpa_onnx: ^1.11.3
  flutter_tesseract_ocr: ^0.4.23  #
  paddle_ocr_flutter: ^0.1.0       # PaddleOCR
  record: ^5.1.0
  camera: ^0.11.0

模型管理 UI

在设置页提供:

  • 嵌入模型: 推荐下载链接 + 手动导入
  • ASR 模型: 推荐 Whisper tiny/base 下载 + 手动导入
  • OCR 模型: Tesseract traineddata 或 PaddleOCR 模型导入

模型总大小: 50-100MB(取决于用户选择)

优势

  • 最轻量: 无预装模型,用户按需下载
  • 灵活性: ASR/OCR 模型用户自选
  • 低内存: TFLite 量化模型占用小
  • 商业安全: 全部开源许可

功能限制

  • ❌ 无 LLM(不支持 AI 纠错、来源识别)
  • ❌ 暴力搜索不适合大规模数据(>10k 笔记考虑方案 C)
  • ✅ 支持语义搜索、相关笔记、简单分类

适用场景

  • 低端设备
  • 存储空间有限
  • 不需要复杂 LLM 功能
  • 快速原型验证

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link
Copy Markdown

vercel bot commented Dec 28, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
thought-echo Ready Ready Preview, Comment Jan 2, 2026 0:49am

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 28, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

- Add tflite_flutter and record dependencies to pubspec.yaml
- Create LocalAIModelConfig model for model path configurations
- Update LocalAISettings to include model configuration
- Create LocalEmbeddingService for TFLite text embeddings
- Create LocalSpeechRecognitionService for Whisper ONNX ASR
- Create LocalOCRService for Tesseract/PaddleOCR OCR
- Create LocalVectorStorageService for SQLite vector storage
- Create LocalAIModelManager for model imports
- Add localization strings for model management (zh and en)
- Update LocalAISettingsPage with model management UI

Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
- Fix attention mask padding logic to handle truncation properly
- Use dart:async unawaited() for async dispose
- Add helper methods to LocalAIModelRecommendations
- Simplify recommendation dialog logic
- Check vocabMap instead of vocabulary for initialization

Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement lightweight user import for TFLite features Add TFLite lightweight local AI solution with user-imported models Dec 28, 2025
Copilot AI requested a review from Shangjin-Xiao December 28, 2025 03:16
Copilot AI and others added 6 commits December 28, 2025 08:21
Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
…able-issue

Implement Local AI Features: Speech-to-Text and OCR with On-Device Processing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants