Google audio to text API but return null - google-cloud-speech

I have send below JSON request but I am getting below error.
JSON Request:-
{
"config": {
"enableAutomaticPunctuation": "true",
"encoding": "MULAW",
"languageCode": "en-US",
"model": "default",
"sampleRateHertz": 8000
},
"audio": {
"content": "QzpcU3BlZWNoVG9UZXh0XGVuZ2xpc2hcUENNXDIud2F2"
}
}
Output: null
Method for Encoding of wav file as given below
byte[] encodedAudio = Base64.encodeBase64(pcmFilePath.getBytes());
String s = new String(encodedAudio);

If pcmFilePath is a String, then pcmFilePath.getBytes() will return the path to the file. Therefore, encodedAudio will contain the path to the file, not the encoded audio.
One way to get the contents of the file would be to use java.nio.file.Files.readAllBytes() introduced in JDK 7.
import java.nio.file.Files;
import java.nio.file.Paths;
byte[] pcmBytes = Files.readAllBytes(Paths.get(pcmFilePath));
byte[] encodedAudio = Base64.encodeBase64(pcmBytes);

Related

How do I upload an image and embed with base64 format in ckeditor5?

I am using ckeditor 5 classic, this is what I have:
ClassicEditor.create(document.querySelector('#editor'),
{
toolbar: ['imageUpload', .....],
ckfinder: {
uploadUrl: '/CKEditorUpload/UploadImage'
}
})
Then I created my own server side controller and I was able to get the data and saved the file on the server. I passed back the JSON and ckeditor 5 was able to insert the image successfully.
return Json(new {uploaded="true", url="/uploads/xxxxx.jpeg"});
In JSON, successful respond looks like this:
{
"uploaded": "true",
"url": "/uploads/xxxxx.jpeg"
}
However, what I really want is to base64 encode the image and return it back to ckeditor 5 so that it will embed it:
<img src="data:image/jpeg;base64, xxxxxxxxx" />
Indeed, I DID THAT successfully in ckeditor 4, but for ckeditor 5 there is no "window.parent.CKEDITOR.tools.callFunction" anymore. How can my server controller return the base64 data back to the client properly, so that Ckeditor 5 would be able to embed it? Thanks for help in advance.
I figured out myself... In C#, that's how I returned the encoded data in Controller back to client and it worked.
var data= "data:image/png;base64,xxxxxxxxxxxxxxxxxxx";
return Json(new { uploaded = "true", url = HttpUtility.JavaScriptStringEncode(data ?? "") });
In other words, the JSON message would look like this. I didn't expect "url" would take base64. Anyways, it works. Hope this helps other people.
{
"uploaded": "true",
"url": "data:image/png;base64,xxxxxxxxxxxxxxxxxxx"
}
C#:
if (Request.Files.Count > 0)
{
HttpPostedFileWrapper arquivo = (HttpPostedFileWrapper)Request.Files[0];
var tempFile = Path.GetTempFileName();
arquivo.SaveAs(tempFile);
var fileBytes = System.IO.File.ReadAllBytes(tempFile);
var fileBase64 = Convert.ToBase64String(fileBytes);
return new JsonResult { Data = new { uploaded = "true", url = $"data:image/gif;base64,{fileBase64}" }, MaxJsonLength = Int32.MaxValue };
}
response:
{
"uploaded": "true",
"url": "data:image/png;base64,xxxxxxxxxxxxxxxxxxx"
}

Parsing boto3 invoke_endpoint response from AWS SageMaker

I have a Sagemaker endpoint that I can infer to from boto3 client and get response.
Per boto3 doc, the Body of the response result is a Byte object StreamingBody type. I convert it to a dictionary
response = client.invoke_endpoint(EndpointName=endpoint_name, Body=json.dumps(data))
response_body = response['Body']
dict_response = response_body.read().decode('utf-8')
print(dict_response)
The above code gives me a response like below (stripped down for this post)
I need to retrieve the array from the "floatVal" key. How do I do that?
{
"outputs": {
"score": {
"dtype": "DT_FLOAT",
"floatVal": [
0.00012408883776515722,
...........
-0.8316119909286499,
-0.24423488974571228
],
"tensorShape": {
"dim": [
{
"size": "1"
},
{
"size": "1024"
}
]
}
}
},
"modelSpec": {
"version": "1",
"name": "generic_model",
"signatureName": "serving_default"
}
}
Actually the dict_response is not really a dictionary here, rather a string type. So I had to convert the dict_response to an actual dictionary and then I could retrieve the floatVal key.
Updated code
response = client.invoke_endpoint(EndpointName=endpoint_name, Body=json.dumps(data))
response_body = response['Body']
response_str = response_body.read().decode('utf-8')
response_dict = eval(response_str)
print(response_dict['outputs']['score']['floatVal'])

How to get a sub-field of a struct type map, in the search response of YQL query in Vespa?

Sample Data:
"fields": {
"key1":0,
"key2":"no",
"Lang": {
"en": {
"firstName": "Vikrant",
"lastName":"Thakur"
},
"ch": {
"firstName": "维克兰特",
"lastName":"塔库尔"
}
}
}
Expected Response:
"fields": {
"Lang": {
"en": {
"firstName": "Vikrant",
"lastName":"Thakur"
}
}
}
I have added the following in my search-definition demo.sd:
struct lang {
field firstName type string {}
field lastName type string {}
}
field Lang type map <string, lang> {
indexing: summary
struct-field key {
indexing: summary | index | attribute
}
}
I want to write a yql query something like this (This doesn't work):
http://localhost:8080/search/?yql=select Lang.en from sources demo where key2 contains 'no';
My temporary workaround approach
I have implemented a custom searcher in MySearcher.java, through which I am able to extract the required sub-field and set a new field 'defaultLang', and remove the 'Lang' field. The response generated by the searcher:
"fields": {
"defaultLang": {
"firstName": "Vikrant",
"lastName":"Thakur"
}
}
I have written the following in MySearcher.java:
for (Hit hit: result.hits()) {
String language = "en"; //temporarily hard-coded
StructuredData Lang = (StructuredData) hit.getField("Lang");
Inspector o = Lang.inspect();
for (int j=0;j<o.entryCount();j++){
if (o.entry(j).field("key").asString("").equals(language)){
SlimeAdapter value = (SlimeAdapter) o.entry(j).field("value");
hit.setField("defaultLang",value);
break;
}
}
hit.removeField("Lang");
}
Edit-1: A more efficient way instead is to make use of the Inspectable interface and Inspector, like above (Thanks to #Jo Kristian Bergum)
But, in the above code, I am having to loop through all the languages to filter out the required one. I want to avoid this O(n) time-complexity and make use of the map structure to access it in O(1). (Because the languages may increase to 1000, and this would be done for each hit.)
All this is due to the StructuredData data type I am getting in the results. StructureData doesn't keep the Map Structure and rather gives an array of JSON like:
[{
"key": "en",
"value": {
"firstName": "Vikrant",
"lastName": "Thakur"
}
}, {
"key": "ch",
"value": {
"firstName": "维克兰特",
"lastName": "塔库尔"
}
}]
Please, suggest a better approach altogether, or any help with my current one. Both are appreciated.
The YQL sample query I guess is to illustrate what you want as that syntax is not valid. Picking a given key from the field Lang of type map can be done as you do in your searcher but deserializing into JSON and parsing the JSON is probably inefficient as StructuredData implements the Inspectable interface and you can inspect it directly without the need to go through JSON format. See https://docs.vespa.ai/documentation/reference/inspecting-structured-data.html

Avro: Keep backwards capability using default value in new field without using writer/old schema

AVRO can handle most common forwards and backwards compatibility. But it somehow needs the writer schema when reading the data using a newer schema.
Saying there is a old schema:
{
"type": "record",
"name": "com.test.Contact",
"fields": [
{
"name": "address",
"type": "string"
}
]
}
If using the following new schema to decode bytes that written by old schema, it is needed to have both old and new schema.
{
"type": "record",
"name": "com.test.Contact",
"fields": [
{
"name": "address",
"type": "string"
},
{
"name": "phone",
"type": ["null", "string"],
"default": null
}
]
}
The code to read
static void sede3() throws IOException {
System.out.println("----------------------sede3---------------------");
Schema.Parser parserNew = new Schema.Parser();
Schema.Parser parserOld = new Schema.Parser();
Schema addrSchemaNew = parserNew.parse(AppMainCore.class.getClassLoader()
.getResourceAsStream("compatibility-new.schema.json"));
Schema addrSchemaOld = parserOld.parse(AppMainCore.class.getClassLoader()
.getResourceAsStream("compatibility-old.schema.json"));
GenericRecord addressOld = new GenericData.Record(addrSchemaOld);
addressOld.put("address", "ABCDEF");
// Generate bytes using old schema.
ByteArrayOutputStream bbos = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().binaryEncoder(bbos, null);
DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(addrSchemaOld);
writer.write(addressOld, encoder);
encoder.flush();
bbos.close();
byte[] oldBytes = bbos.toByteArray();
System.out.println(Hex.encodeHexString(oldBytes) + " is old record bytes");
// Try to deserialize old bytes using new schema, with old schema's help
DatumReader<GenericRecord> readerGood = new GenericDatumReader<>(addrSchemaOld, addrSchemaNew);
Decoder decoderGood = DecoderFactory.get().binaryDecoder(oldBytes, null);
GenericRecord goodAddress = readerGood.read(null, decoderGood);
System.out.println(goodAddress + " is record from old bytes using new schema");
}
What I am trying is deserializing old bytes using new schema, WITHOUT old schema's help.
like:
DatumReader<GenericRecord> readerGood = new GenericDatumReader<>(addrSchemaNew);
But not
DatumReader<GenericRecord> readerGood = new GenericDatumReader<>(addrSchemaOld, addrSchemaNew);
Is it supported in Avro?
I don't think that's possible, see documentation here: https://avro.apache.org/docs/1.8.2/spec.html#Schema+Resolution. Avro needs both old and new schema to perform the conversion.

pkpass not showing in the simulator

i am implementing a ios app with Passbook. so i have implemented ASP.net server to generate a pass. the pass was generated successfully from server side. and also when i double click on the pass.pkpass file from my mac, pass viewer is showing my pass which i created from the server. but when im trying to drag it to IOS simulator it is said that "safari can not download this file". i have tried by regenerating certificates. but nothing worked.
here is my pass.json file
{
"passTypeIdentifier": "<mypassIdenttity>",
"formatVersion": 3,
"organizationName": "<my organization name>",
"serialNumber": "<my serial number>",
"teamIdentifier": "<my team identifier>",
"description": "123",
"foregroundColor": "rgb(255,255,255)",
"backgroundColor": "rgb(55,117,50)",
"logoText": "Zekes",
"barcode": {
"message": "123456789",
"format": "PKBarcodeFormatPDF417",
"messageEncoding": "iso-8859-1"
},
"storeCard": {
"primaryFields": [
{
"key": "balance",
"label": "remaining balance",
"value": 21.75
}
],
"auxiliaryFields": [
{
"key": "deal",
"label": "Deal of the Day",
"value": "Lemons"
}
]
}
}
here is my manifest.json
{
"pass.json": "285c63c6580d946eb360e0c6e68d1fa072ea20e9",
"logo.png": "25de09e2d3b01ce1fe00c2ca9a90a2be1aaa05cf",
"icon.png": "0296b01347b3173e98438a003b0e88986340b2d8",
"icon#2x.png": "5afd9585b08c65fdf105a90c8bd643407cba2787",
"strip.png": "736d01f84cb73d06e8a9932e43076d68f19461ff",
"strip#2x.png": "468fa7bc93e6b55342b56fda09bdce7c829d7d46"
}
i have exported my pass certificate as P12 file and the privateKey as P12 file. and copied them into my project path[in the server implementation]
here is how i generating my signature file using manifest and the certificate file.
static void SignManifest(ZipFile zipFile, byte[] manifestFileData, X509Certificate2 certificate)
{
var cert = DotNetUtilities.FromX509Certificate(certificate);
var privateKey = DotNetUtilities.GetKeyPair(certificate.PrivateKey).Private;
var generator = new CmsSignedDataGenerator();
generator.AddSigner(privateKey, cert, CmsSignedDataGenerator.DigestSha1);
var certList = new System.Collections.ArrayList();
////////////////////////////////////////////////////////
var a1Cert = new X509Certificate2(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwrd.cer"));
// var a2Cert = new X509Certificate2(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppleIncRootCertificate.cer"));
///////////////////////////////////////////////////////////
certList.Add(cert);
certList.Add(DotNetUtilities.FromX509Certificate(a1Cert));
//certList.Add(DotNetUtilities.FromX509Certificate(a2Cert));
Org.BouncyCastle.X509.Store.X509CollectionStoreParameters PP = new Org.BouncyCastle.X509.Store.X509CollectionStoreParameters(certList);
Org.BouncyCastle.X509.Store.IX509Store st1 = Org.BouncyCastle.X509.Store.X509StoreFactory.Create("CERTIFICATE/COLLECTION", PP);
generator.AddCertificates(st1);
var content = new CmsProcessableByteArray(manifestFileData);
var signedData = generator.Generate(content, false);
var data = signedData.GetEncoded();
zipFile.AddEntry("signature", data);
}
i can successfully generate the pass.pkpass but why it is not runing in the simulator?.. can someone tell me the reason ? im really stuck in here for two days now.. please help me.
According to the Passbook documentation, the formatVersion should always be 1.
https://developer.apple.com/library/ios/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/TopLevel.html

Resources