Flutter Speech to text tutorial

Speech to text app in flutter 


Step1 : Create a new flutter project 


Step 2. Remove Default Counter App 


Step 3. Create a Constant.dart file for your secret key 

 const apiSecretKey = "";


Step 4. Create HomeScreen.dart 


Method for covert Speech to Text 


 Future<String> convertSpeechToText(String filePath) async {
    const apiKey = apiSecretKey;
    var url = Uri.https("api.openai.com", "v1/audio/transcriptions");
    var request = http.MultipartRequest('POST', url);
    request.headers.addAll(({"Authorization": "Bearer $apiKey"}));
    request.fields["model"] = 'whisper-1';
    request.fields["language"] = "en";
    request.files.add(await http.MultipartFile.fromPath('file', filePath));
    var response = await request.send();
    var newresponse = await http.Response.fromStream(response);
    final responseData = json.decode(newresponse.body);

    return responseData['text'];
  }


onClick()

var var text = "";

 FilePickerResult? result = await FilePicker.platform.pickFiles();
              if (result != null) {
                //call openai's transcription api
                convertSpeechToText(result.files.single.path!).then((value) {
                  setState(() {
                    text = value;
                  });
                });
              }

Complete Code : 


Post a Comment

Previous Post Next Post