Skip to main content

Complete Example

Here’s a complete example to get you started with on-device text generation:
App.tsx

Step-by-Step Guide

1. Initialize the SDK

Initialize RunAnywhere once when your app starts:

Environment Options

2. Register Backend Modules

Register the backend modules you need:

3. Add Models

Add models from HuggingFace or any direct URL:

4. Download the Model

Download with progress tracking:

5. Load and Generate

Unlike LLM loading which takes just a file path, STT and TTS model loading requires both the file path AND the engine name ('whisper' for STT, 'piper' for TTS). Use getModelInfo() to retrieve the downloaded model’s local path.

6. Stream Responses

For real-time token streaming:

Using in a React Component

Here’s a pattern for using the SDK in React components with hooks:
useAI.ts

What’s Next?

LLM Chat

Simple one-liner for quick responses

Streaming

Real-time token streaming

Speech-to-Text

Transcribe audio to text

Text-to-Speech

Convert text to spoken audio