Google speech to text gives very late response in mobile device - speech-to-text

I'm using google speech to text converter and it works fine on desktop, but in mobile devices, it gives a very late response. Even on google's example page, it gives a late response.
I'm using live streaming.
In the response, google returns is_final keyword which is used to identify if its the final response from google or not, which is coming very late, sometimes around after 90 seconds.
Below is the code I'm using.
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US'; //en-US
var request = {
config: {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
enableAutomaticPunctuation: true,
model: "default",
},
interimResults: true, // If you want interim results, set this to true
};
Let me know if I'm missing anything.

Related

Using requestretry to call a REST api which responses with xlsx file. I want to read the file without saving it

const xlsx = require("xlsx");
const requestretry = require("requestretry");
let response = await requestretry({
url: "https://dummy.com/filename",
json: false,
headers: { Authorization: token },
maxAttempts: 5,
retryDelay: 5000,
retryStrategy: requestretry.RetryStrategies.HTTPOrNetworkError
})
const workbook = xlsx.read(response.body, { type: "buffer" });
exceldata = await xlsx.utils.sheet_to_json(
workbook.Sheets[workbook.SheetNames[0]], { range: 4, raw: false })
console.log(exceldata);// didn't get my excel data
when I use Postman's Send button to call that external api I receive a unicode chracters in respose tab
But when I use Postman's Send and Download button to call that external api I can get the xlsx file to save.
Now, I am able to replicate same unicode as in I am receiving with Postman's Send button in my NodeJS code using requestretry module but don't know what to do next.
I thought its a buffer and tried xlsx.read(response.body, { type: "buffer" }) but got wrong output.
xlsx file

How can I use Axios to access the JSON data within a response sent using Expressjs?

I'm creating a web application that generates a pdf on a server then sends it to the client for display within the browser.
The client is using Vuejs / Axios to send a POST request. Afterwards, The server is receiving it with Expressjs, generating a unique PDF, converting the file to a base64 value then sending it back as a response.
I cannot seem to get the response correct. When I attempt to display response.data.pdfData within the client I get undefined in the console. I can see that there is indeed a response with the key and value pair using inspection tools within the Network tab under the Preview section but cannot seem to access it.
// FILE: ./Client/src/App.vue
submit(personalInfo) {
this.cardInfo.personalInfo = personalInfo;
console.log('Sending POST preview_card...');
axios({
url: 'http://localhost:5000/api/preview_card',
method: 'POST',
responseType: 'blob',
data: {
cardInfo: this.cardInfo,
},
}).then((response) => {
console.log(response.data.pdfData);
});
},
// FILE: ./Server/app.js
app.post('/api/preview_card', (req, res) => {
// Generate pdf
const doc = new jsPDF('p');
doc.setFontSize(40);
doc.text(req.body.cardInfo.templateInfo, 100, 100);
doc.save('response.pdf');
// Convert pdf to base64
var tempFile = path.resolve(__dirname, './response.pdf');
var pdfBase64 = fs.readFileSync(tempFile).toString('base64');
res.setHeader('Content-Type', 'application/json');
return res.send(JSON.stringify({ pdfData: pdfBase64 }));
});
I find it necessary to serve the pdf this way due to my client's compnents as well as to enforce a level of data coherency between concurrent users.

Send Puppeteer Generated Pdf to another microservice using requestPromise npm

I have two microservices: 1) in which I am generating pdf using Puppeteer, which is essentially a Buffer object. From this service I want to send the pdf to another microservice, 2) which receives the pdf in request and attaches it in email using mailgun (once I am able to send pdf from one service to another, attaching as an email wont be difficult).
The way I m sending the pdf in requestpromise is this:
import requestPromise from "request-promise";
import {Readable} from "stream";
//pdfBuffer is result of 'await page.pdf({format: "a4"});' (Puppeteer method).
const stream = Readable.from(pdfBuffer);
/*also tried DUPLEX and Readable.from(pdfBuffer.toString()) and this code too.
const readable = new Readable();
readable._read = () => {}
readable.push(pdf);
readable.push(null);
*/
requestPromise({
method: "POST",
url: `${anotherServiceUrl}`,
body: {data},
formData: {
media: {
value: stream,
options: {
filename: "file.pdf",
knownLength: pdfBuffer.length,
contentType: "application/pdf"
}
}
},
json: true
}
});
But doing so, I get "ERR_STREAM_WRITE_AFTER_END" error. How can I send this pdf from one service to another, since the other service sends the email to the user?
I've done it from the frontend with:
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/pdf'
},
body: pdfData
}
In this case pdfData is a blob so you would need a polyfill for that plus node-fetch
const buffer = Buffer.from(pdfBuffer).toString("base64").toString();
sending the buffer in the body.
On Receiving service:
const pdf = Buffer.from(body.buffer, "base64");
fs.write("file.pdf", pdf, ()=> {});

How do I use the Spotify api endpoint (the URL of the API is included below) to play a specific song from my index.js file?

I am making a Google assistant app which (1). takes the emotion of the user, (2). retrieves a song from a pre analysed database of the same emotion and (3). plays that song via Spotify.
I have finished with parts 1 and 2 but am struggling with the 3rd part. I have found on here ( https://developer.spotify.com/console/put-play ) the API for sending a POST request to Spotify which plays a certain song or album. How do I convert this information into a POST request from my index.js file?
For example, if I wanted to POST the Spotify code for Red Hot Chili Peppers' "Suck My Kiss", what would the code look like for sending the spotify track id?
3
artist:
"Red Hot Chili Peppers"
id:
"4"
maxEmotion:
"anger"
score:
"0.578864"
song:
"Suck My Kiss"
spotifyCode:
"spotify:track:0psB5QzGb4653K0uaPgEyh"
I have tried using the Webhook format but am not sure if I understand the right way to use this and also it would mean that my entire index.js file is useless (since you can only have one or the other on the Google Assistant). So I am interested to see how to do this in the index.js file if possible? I have included the most important code below:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'ws://mood-magic-four-ptwvjb.firebaseio.com/'
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
//4
function playAngrySong (agent) {
// Get the database collection 'dialogflow' and document 'agent'
return admin.database().ref((`3`) ).once('value').then((snapshot) => {
const song = snapshot.child('song').val();
const artist = snapshot.child('artist').val();
agent.add(`I will play ${song} by ${artist}`);
// THIS IS WHERE I NEED THE POST TO THE SPOTIFY API
});
}
// Map from Dialogflow intent names to functions to be run when the intent is matched
let intentMap = new Map();
intentMap.set('-Angry - yes', playAngrySong);
agent.handleRequest(intentMap);
});
SOLVED: Here is the code below (using the Google assistant agent to say 'Enjoy' before it plays):
function playMusic(agent){
agent.add('Enjoy!');
var request = require("request");
var options = { method: 'PUT',
url: 'https://api.spotify.com/v1/me/player/play',
headers:
{ 'cache-control': 'no-cache,no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${Access.accessToken}`,
Accept: 'application/json' },
body: { uris: [ `${SongInfo.Spotify_uri}` ] },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log('This is the body of the play music request ' + body);
});
}

Why am I getting 404 ("Resource media not found") on the media upload from the docs?

The docs for uploading rich media to LinkedIn (https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/rich-media-shares#upload-rich-media) say to make a POST to https://api.linkedin.com/media/upload with form data. As far as I can tell I am doing that correctly using request-promise on my Node server, but I am still getting a 404.
Initially I had a problem with my file, but now I think I am properly creating a Buffer. Even if I'm not, that was preventing me from even making the request, and now I am and I don't think that would cause a 404.
I have also tried using 1.0.0 and 2.0.0 versions of the X-Restli-Protocol-Version (LinkedIn API thing).
// See LinkedIn docs on Rich Media shares https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/rich-media-shares
const stream = require('stream');
const rp = require('request-promise')
async function postRichMediaShare(accessToken) {
try {
const file = await rp({
method: 'get',
url: 'https://local-image-bucket.s3.amazonaws.com/Artboard+copy.png'
});
// Buffer magic
const buffer = new Buffer.from(file);
const bufferStream = new stream.PassThrough();
bufferStream.end( buffer );
bufferStream.pipe( process.stdout );
const options = {
method: 'post',
url: 'https://api.linkedin.com/v2/media/upload',
headers: { 'X-Restli-Protocol-Version': '2.0.0',
"Authorization": `Bearer ${accessToken}` },
formData: {
file: {
value: bufferStream,
options: {
filename: 'Artboard+copy.png',
contentType: 'image/png'
}
}
},
};
const response = await rp(options);
console.log("response", response);
return response;
} catch (error) {
throw new Error(error);
}
}
Instead of the response suggested in the docs, I'm getting this error message from LinkedIn:
error: "{"serviceErrorCode":0,"message":"Resource media does not exist","status":404}"
I'm an idiot. 404 should be expected because I'm requesting https://api.linkedin.com/v2/media/upload and the docs say https://api.linkedin.com/media/upload (no v2/). I believe every other call is versioned. Perhaps an empowered LinkedIn employee reading this could make a route for v2/ that does all the same stuff.
Note, there may be other problems with the code above, I am still struggling but now I'm working on things outside the scope of this question about 404.

Resources