代码封装 whisper_ggml 插件
import 'package:whisper_ggml/whisper_ggml.dart';
import 'injection.dart';
class WhisperGgmlSttService {
final controller = WhisperController();
Future<String> speechToText(String audioPath) async {
final result = await controller.transcribe(
model: WhisperModel.small,
keepModelLoaded: true,
// audioPath: '/path/to/audio.wav',
audioPath: audioPath,
// lang: 'en',
lang: 'auto',
);
logger.i(result?.transcription.text);
return result?.transcription.text ?? '';
}
}
去 huggingface 下载模型文件
https://huggingface.co/ggerganov/whisper.cpp/tree/main
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../services/injection.dart';
class AudioInput extends StatefulWidget {
const new({super.key});
@override
State<AudioInput> createState() => _AudioInputState();
}
class _AudioInputState extends State<AudioInput> {
bool isRecording = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onLongPressStart: (details) {
isRecording = true;
recorder.start();
},
onLongPressEnd: (details) async {
isRecording = false;
final recordedAudioPath = await recorder.stop();
logger.i('recordedAudioPath: $recordedAudioPath');
if (recordedAudioPath != null) {
audioPlayer.play(recordedAudioPath);
final text = await whisperStt.speechToText(recordedAudioPath);
logger.i('text: $text');
}
},
onLongPressCancel: () {
isRecording = false;
recorder.cancel();
},
child: ElevatedButton(
onPressed: () {},
child: Text(isRecording ? "Recording..." : "Hold to speak"),
),
);
}
}
初次摸索 whisper_ggml: ^2.7.1 的用法,不知道怎么用 android device explorer 把模型上传到指定位置/data/user/0/com.example.chatgpt_juejin/files/ggml-small.bin
正确的方法应该是 使用代码写入,更好的办法慢慢摸索
效果截图
。。。