I am trying to send a request on a model on sagemaker using .NET. The code I am using is:
var data = File.ReadAllBytes(#"C:\path\file.csv");
var credentials = new Amazon.Runtime.BasicAWSCredentials("","");
var awsClient = new AmazonSageMakerRuntimeClient(credentials, RegionEndpoint.EUCentral1);
var request = new Amazon.SageMakerRuntime.Model.InvokeEndpointRequest
{
EndpointName = "EndpointName",
ContentType = "text/csv",
Body = new MemoryStream(data),
};
var response = awsClient.InvokeEndpoint(request);
var predictions = Encoding.UTF8.GetString(response.Body.ToArray());
the error that I am getting on awsClient.InvokeEndpoint(request)
is:
Amazon.SageMakerRuntime.Model.ModelErrorException: 'The service
returned an error with Error Code ModelError and HTTP Body:
{"ErrorCode":"INTERNAL_FAILURE_FROM_MODEL","LogStreamArn":"arn:aws:logs:eu-central-1:xxxxxxxx:log-group:/aws/sagemaker/Endpoints/myEndpoint","Message":"Received
server error (500) from model with message \"\". See
"https:// url_to_logs_on_amazon"
in account xxxxxxxxxxx for more
information.","OriginalMessage":"","OriginalStatusCode":500}'
the url that the error message suggests for more information does not help at all.
I believe that it is a data format issue but I was not able to find a solution.
Does anyone has encountered this behavior before?
The problem relied on the data format as suspected. In my case all I had to do is send the data as a json serialized string array and use ContentType = application/json because the python function running on the endpoint which is responsible for sending the data to the predictor was only accepting json strings.
Another way to solve this issues is to modify the python function which is responsible for the input handling to accept all content types and modify the data in a way that the predictor will understand.
example of working code for my case:
var data = new string[] { "this movie was extremely good .", "the plot was very boring ." };
var serializedData = JsonConvert.SerializeObject(data);
var credentials = new Amazon.Runtime.BasicAWSCredentials("","");
var awsClient = new AmazonSageMakerRuntimeClient(credentials, RegionEndpoint.EUCentral1);
var request = new Amazon.SageMakerRuntime.Model.InvokeEndpointRequest
{
EndpointName = "endpoint",
ContentType = "application/json",
Body = new MemoryStream(Encoding.ASCII.GetBytes(serializedData)),
};
var response = awsClient.InvokeEndpoint(request);
var predictions = Encoding.UTF8.GetString(response.Body.ToArray());
Related
I'm trying to process the resulting URLs from the marketing/contacts/exports/{id} call I used the following code however the data is dataresponse is zipped/encrypted.
SendGridClient client = new SendGridClient(AppConfig.ReadSetting("SENDGRID_API_KEY"));
SingleSendContactList allListsSingleSend = new SingleSendContactList();
var response = await client.RequestAsync(method: SendGridClient.Method.GET, urlPath: "marketing/lists?page_size=500");
allListsSingleSend = JsonConvert.DeserializeObject<SingleSendContactList>(response.Body.ReadAsStringAsync().Result);
SingleSendExportSetup SSExportSetup = new SingleSendExportSetup();
SSExportSetup.file_type = "json";
SSExportSetup.max_file_size = 5000;
// marketing/contacts/exports, POST returns JSON with id as main field
SingleSendExportID ExportContactsSingleSend = new SingleSendExportID();
response = await client.RequestAsync(method: SendGridClient.Method.POST, urlPath: "marketing/contacts/exports", requestBody: JsonConvert.SerializeObject(SSExportSetup));
ExportContactsSingleSend = JsonConvert.DeserializeObject<SingleSendExportID>(response.Body.ReadAsStringAsync().Result);
//Use this id to then call
// marketing/contacts/exports/id, GET returns JSON with urls as a property, use urls[0]
SingleSendExportURL ExportURLSingleSend = new SingleSendExportURL();
response = await client.RequestAsync(method: SendGridClient.Method.GET, urlPath: "marketing/contacts/exports/" + ExportContactsSingleSend.id);
ExportURLSingleSend = JsonConvert.DeserializeObject<SingleSendExportURL>(response.Body.ReadAsStringAsync().Result);
// Call the URL to get the returned JSON
List<SingleSendExportData> ExportDataSingleSend = new List<SingleSendExportData>();
var dataresponse = await client.RequestAsync(method: SendGridClient.Method.GET, urlPath: ExportURLSingleSend.urls[0]);
ExportDataSingleSend = JsonConvert.DeserializeObject<List<SingleSendExportData>>(dataresponse.Body.ReadAsStringAsync().Result);
I can take the URL in ExportURLSingleSend.urls[0] and drop it in a browser and it will force the download of the file and the data is fine. How do I get this data to convert to my class List<SingleSendExportData>?
I posted this question to SendGrid support and they sent me here.
Thanks.
UPDATE:
This post solved my issue:
https://weblog.west-wind.com/posts/2007/jun/29/httpwebrequest-and-gzip-http-responses
However, now I am realizing that the JSON coming from SendGrid is not properly formatted. There is no comma between rows of data.
UPDATE 2:
This post solved my handling of line delimited JSON files;
Line delimited json serializing and de-serializing
Now I am able to read these zipped JSON files without saving them and then handle the JSON format to load into my list List ExportDataSingleSend.
I am trying to upload an image to the Microsoft custom vision API prediction endpoint using Restsharp, I am trying to use the AddFile method but I am getting a BadRequest as the result, here is the code I am using
public IRestResponse<PredictionResponse> Predict(string imageFileName)
{
var file = new FileInfo(imageFileName);
var serviceUrl = ConfigurationManager.AppSettings["api.custom-vision.prediction.url.file"];
var serviceKey = ConfigurationManager.AppSettings["api.custom-vision.key"];
var client = new RestClient(serviceUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/octet-stream");
request.AddHeader("Prediction-Key", serviceKey);
request.AddFile(file.Name, file.FullName);
var response = client.Execute<PredictionResponse>(request);
return response;
}
When I execute the method I am getting the following response back from the service
{
"code": "BadRequestImageFormat",
"message": "Bad Request Image Format, Uri: 1062fe0480714281abe2daf17beb3ac5"
}
After looking for ways in the restsharp documentation to properly upload a file, I came to the solution that it needs to be passed as parameter with an array of bytes with the parameter type of ParameterType.RequestBody
Here is the example of the method that actually works
public IRestResponse<PredictionResponse> Predict(string imageFileName)
{
var file = new FileInfo(imageFileName);
var serviceUrl = ConfigurationManager.AppSettings["api.custom-vision.prediction.url.file"];
var serviceKey = ConfigurationManager.AppSettings["api.custom-vision.key"];
var client = new RestClient(serviceUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/octet-stream");
request.AddHeader("Prediction-Key", serviceKey);
request.AddParameter("content", File.ReadAllBytes(file.FullName), ParameterType.RequestBody);
var response = client.Execute<PredictionResponse>(request);
return response;
}
I'm using an online Api Json but I get this error:
Newtonsoft.Json.JsonSerializationException: 'Error converting value "{"Id":82800,"Name":"Pet Cage","Level":20,"Class":"Battle Pets","SubClass":"BattlePet","VendorBuy":0,"VendorSell":0,"MarketValue":0,"MinBuyout":0,"Quantity":0,"NumAuctions":0,"HistoricalPrice":0,"RegionMarketAvg":0,"RegionMinBuyoutAvg":0,"RegionQuantity":0,"RegionHistoricalPrice":38045754,"RegionSaleAvg":152352186,"RegionAvgDailySold":0,"RegionSaleRate":0,"URL":"https://www.tradeskillmaster.com/items/pet-cage-82800?realm=EU-ragnaros"}" to type 'System.Collections.Generic.List`1[TSM.Results]'. Path '', line 1, position 495.'
I want to get the Api Json (containing data) to show up in a list. I can get the data from the API but somewhere in my JSON conversion things go wrong.
I'm using the following code:
HttpClient Connection = new HttpClient();
Connection.BaseAddress = new Uri("http://api.tradeskillmaster.com/v1/item/EU/ragnaros/82800?format=json&apiKey=***hidden***");
Connection.DefaultRequestHeaders.Add("User-Agent", "TSM battle pet");
HttpResponseMessage response = Connection.GetAsync("").Result;
//var emp = response.Content.ReadAsAsync<IEnumerable<Results>>().Result;
var emp = response.Content.ReadAsStringAsync().Result;
var New_Json = emp.Replace("\\\\", "");
string json = JsonConvert.SerializeObject(New_Json);
var parsedObject = JsonConvert.DeserializeObject<JObject>(emp);
foreach (var dataset in parsedObject.Properties())
{
Console.WriteLine(dataset.Name);
}
List<Results> items = JsonConvert.DeserializeObject<List<Results>>(json); // <----- THIS ONE GIVE's THE ERROR
Does anyone know how I can fix this error so I can correctly get it in the list?
Thanks in advance!
I have consumed the web service in windows application, when pass the request to get response getting an error.
I have used WSE 2.0 to pass the credentials.
public string GetResponse(string sPersonnelAreaCode, string sCompanyCode)
{
try
{
WebReference.RIL_STAR_HCM_QueryEmployeeDetails_serviceagent objService1 = new WebReference.RIL_STAR_HCM_QueryEmployeeDetails_serviceagent();
WebReference.fetchEmployeeListRequestEmployeeList[] objReqs = new WebReference.fetchEmployeeListRequestEmployeeList[1];
WebReference.fetchEmployeeListRequestEmployeeList objReq1 = new WebReference.fetchEmployeeListRequestEmployeeList();
WebReference.fetchEmployeeListResponseEmployeeList[] objResponse = new WebReference.fetchEmployeeListResponseEmployeeList[0];
DataSet dsresult = new DataSet();
objReq1.PersonnelAreaCode = sPersonnelAreaCode;
objReq1.CompanyCode = sCompanyCode.ToString();
UsernameToken token = new UsernameToken("***", "***", PasswordOption.SendPlainText);
objService1.RequestSoapContext.Security.Tokens.Add(token);
objReqs[0] = objReq1;
//In the below line getting that error
objResponse = objService1.fetchEmployeeList(objReqs);
}
}
Can anyone please help me?
This kind of error usually comes when the report server path is not proper. Double check your ReportServerUrl
Also refer Report viewer Error message "client found response content type of '' but expected 'text xml' The request failed with an empty response."
Try setting the ContentType:
objReq.ContentType = "text/xml";
Assuming you're using HttpWebRequest..
I am trying to post a share on Jive using the /Shares REST API in .net using C#. However I am not able to do this and getting the following error:
"The remote server returned an error: (400) Bad Request."
Following is the code which I have written:
string response = string.Empty;
using(WebClient client = new WebClient())
{
string strJiveShareURL = "https://<JiveURL>";
strJiveShareURL += "/api/core/v3/shares";
var SharedJSON = new AddShareJSON
{
participants = new string[] {"https://<JiveURL>/api/core/v3/people/{username}" },
shared = "https://<<Content URL to be shared>>",
content= new Content
{
type = "text/html",
text = "This is a test share from SharePoint to Jive"
}
};
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string shareJSON = serializer.Serialize(SharedJSON);
Console.WriteLine("Setting Credentials:");
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("UID:PWD"));
client.Headers[HttpRequestHeader.Authorization]= "Basic " + credentials;
client.Headers[HttpRequestHeader.Accept] = "application/json";
client.Headers[HttpRequestHeader.ContentType] = "application/json";
//BypassCertificateError();
response = client.UploadString(strJiveShareURL, "POST", shareJSON);
Console.WriteLine("Response:" + response);
Console.ReadLine();
}
and following is the JSON which is created for posting the share:
{
"content": {
"type":"text/html",
"text":"This is a test share from SharePoint to Jive"
},
"participants": ["https://<<Jive URL>>/api/core/v3/people/<<username>>"],
"Shared":"https://<<URL of the Content To be Shared>>"
}
Please let me know if there is anything which I have been doing incorrectly.
I figured this out myself, I was getting the error because I was passing an invalid URI object to the shared parameter of the Share REST endpoint. The shared parameter requires a content URI in the form of
http://[[JiveURL]]/api/core/v3/contents/[[ContentID]]
Earlier I was trying to pass URLs external to Jive resulting in the bad request error.