whisper_onnx_stt 0.0.1
whisper_onnx_stt: ^0.0.1 copied to clipboard
A Flutter plugin for offline Speech-to-Text inference using Whisper models exported to ONNX format. Runs on the highly optimized C/C++ sherpa-onnx framework.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:whisper_onnx_stt/whisper_onnx_stt.dart';
import 'package:file_picker/file_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _whisperPlugin = WhisperOnnxStt();
String _status = 'Idle';
double _downloadProgress = 0.0;
String? _selectedFilePath;
@override
void initState() {
super.initState();
}
Future<void> _initModel() async {
setState(() => _status = 'Downloading/Initializing Model...');
try {
await _whisperPlugin.ensureModelDownloaded(
'http://localhost:8000', // Example fallback link
onProgress: (fileName, progress) {
setState(() {
_status = 'Downloading $fileName...';
_downloadProgress = progress;
});
},
);
setState(() => _status = 'Model Ready');
} catch (e) {
setState(() => _status = 'Error initializing: $e');
}
}
Future<void> _pickAndTranscribeMedia() async {
final result = await FilePicker.platform.pickFiles(
type: FileType.media,
allowMultiple: false,
);
if (result != null && result.files.single.path != null) {
setState(() {
_selectedFilePath = result.files.single.path!;
_status = 'Transcribing $_selectedFilePath...';
});
try {
final text = await _whisperPlugin.transcribeMedia(
_selectedFilePath!,
language: 'it', // Force italian testing as user requested
);
setState(() => _status = 'Transcription:\n$text');
} catch (e) {
setState(() => _status = 'Error transcribing: $e');
}
} else {
setState(() => _status = 'File picking aborted by user');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Whisper ONNX Example')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Status: $_status', textAlign: TextAlign.center),
if (_downloadProgress > 0 && _status.contains('Downloading'))
LinearProgressIndicator(value: _downloadProgress),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _initModel,
child: const Text('Download Models & Init'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _pickAndTranscribeMedia,
child: const Text('Pick Media (Audio/Video) & Transcribe'),
),
],
),
),
),
),
),
);
}
}