Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expo: Anyway of adding Speech to Text?

I want to include speech-to-text in my Expo app. There are api's available such as google's speech to text and watson etc...

Has anyone come up with a solution or has any advice on how to include Speech-to-Text in their Expo or React-Native application?

I have looked at various github repos that provide Speech-to-Text for React-Native applications but they do not look production ready, and are strictly React-Native solutions as you need access to Java/Swift code.

I am not disinclined towards that if that is the only option but would prefer a Expo solution if possible.

Regards, Emir

like image 962
eGlu Avatar asked Apr 08 '18 18:04

eGlu


People also ask

What is Expo speech?

expo-speech provides an API that allows you to utilize Text-to-speech functionality in your app.


1 Answers

If you want to implement Speech-To-Text on expo then you need to create an api and deploy it else you need to detach the project and use the library react-native-google-speech-api

This is what i have implemented using google app engine, google cloud storage and google speech to text.

const format = require('util').format;
const fs = require('fs');
const express = require('express');
const multer  = require('multer');
const requestHttp = require('request');
const {Storage} = require('@google-cloud/storage');

// Instantiate a storage client
const storage = new Storage();

// const upload = multer();
const app = express();

// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';

  const upload = multer({
    storage: multer.memoryStorage(),
    limits: {
      fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
    },
  });

  const bucket = storage.bucket(process.env.GCLOUD_STORAGE_BUCKET);


app.post('/upload', upload.single('file') , async (req, res) => {

    const file = await req.file
    if (!file) {
      const error = new Error('Please upload a file')
      error.httpStatusCode = 400
      return next(error)
    }

    // Create a new blob in the bucket and upload the file data.
    const blob = bucket.file(req.file.originalname);
    const blobStream = blob.createWriteStream({
      resumable: false,
    });

    blobStream.on('error', err => {
      next(err);
    });

    blobStream.on('finish', async () => {
        // The public URL can be used to directly access the file via HTTP.
        const publicUrl = await format(
          `https://storage.googleapis.com/${bucket.name}/${blob.name}`
        );
        const request = {
            config: {
              encoding: encoding,
              sampleRateHertz: sampleRateHertz,
              languageCode: languageCode,
            },
            audio: {
                uri: 'gs://YOUR-Bucket-Name/File-name.ext'
            } 
          };
        // Stream the audio to the Google Cloud Speech API
        const [response] = await client.recognize(request);
        const transcription = response.results
          .map(result => result.alternatives[0].transcript)
          .join('\n');
        console.log(`Transcription: `, transcription);
        res.status(200)
            .send({
            success: 'true',
            message: 'Text retrieved successfully',
            text: transcription
            })
            .end();

      });
      blobStream.end(req.file.buffer);
});


const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});
like image 59
Abhay Sharma Avatar answered Sep 25 '22 00:09

Abhay Sharma