How to interface to web-based predictive endpoints using Python - python

The following web-page provides example code for using a predictive endpoint using c#
https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/use-prediction-api
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CVSPredictionSample
{
public static class Program
{
public static void Main()
{
Console.Write("Enter image file path: ");
string imageFilePath = Console.ReadLine();
MakePredictionRequest(imageFilePath).Wait();
Console.WriteLine("\n\nHit ENTER to exit...");
Console.ReadLine();
}
public static async Task MakePredictionRequest(string imageFilePath)
{
var client = new HttpClient();
// Request headers - replace this example key with your valid Prediction-Key.
client.DefaultRequestHeaders.Add("Prediction-Key", "<Your prediction key>");
// Prediction URL - replace this example URL with your valid Prediction URL.
string url = "<Your prediction URL>";
HttpResponseMessage response;
// Request body. Try this sample with a locally stored image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(url, content);
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
private static byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
}
How can it be done using Python code ?
Many thanks

A similar documentation exists for Python:
For classification projects here
For object detection projects here
See language selector in the page:

Related

HttpResponseMessage returns 403 in .NET

I have a async method which make a HttpClient call repeatedly in .Net. I simulate my code as a small console app below:
private static HttpClient req { get; set; } = new HttpClient();
static async Task Main(string[] args)
{
Console.WriteLine("Please press enter to start healthCheck");
Console.ReadLine();
healthCheck();
Console.ReadLine();
}
private static async Task healthCheck()
{
while (true)
{
req.DefaultRequestHeaders.Add("apikey", "myPassword");
string strUrl = "http://myUrl";
HttpResponseMessage hrm = await req.GetAsync(strUrl);
Console.WriteLine("=> statusCode:" + (int)hrm.StatusCode);
await Task.Delay(5000);
}
}
The output is:
The problem is when I use Postman or write this code with python as below, every time it responded 200 instead of 403.
import requests as req
import time as t
url = "http://adpsms.adpdigital.com/report/?date=2021-08-30"
customHeader = {"apikey": "sssrjdIiGisbViKA"}
i = 10
while (i > 0):
response = req.get(url, headers = customHeader)
print("statusCode: " + str(response.status_code))
i -= 1
t.sleep(5)
I supposed it is a server error but when I responded 200 every time with python I understand it would be a problem with my code or something client based.
Since my project is based on .NET I want to make it work on it.
Any suggestion would be appreciated.
On every iteration of your loop, you are adding DefaultRequestHeaders.
It means that they will be added again and again on each iteration of your cycle to the global instance of HttpClient
According to the official docs these headers will be sent with each request.
For your particular task you might add them only once(that's why the name contains prefix default.)
So if you slightly rewrite your code like this:
private static HttpClient req { get; set; } = new HttpClient();
static async Task Main(string[] args)
{
Console.WriteLine("Please press enter to start healthCheck");
Console.ReadLine();
AddDefaultHeaders();
healthCheck();
Console.ReadLine();
}
private static void AddDefaultHeaders()
{
req.DefaultRequestHeaders.Add("apiKey", "myPassword");
}
private static async Task healthCheck()
{
while (true)
{
string strUrl = "http://myUrl";
HttpResponseMessage hrm = await req.GetAsync(strUrl);
Console.WriteLine("=> statusCode:" + (int)hrm.StatusCode);
await Task.Delay(5000);
}
}
It should works fine.

node request vs python session

I have a working node.js script which written using the node.js request module.
I'm trying to convert this script to python with the session module.
I'm new to python and I followed the python docs as it mentioned. but I'm struggling to get my code works.
the problem I'm having is sending the cookie values in the subsequent requests with the session module.
as per the docs it is saving cookies and send them automatically in any requests after that. but
here is my working node.js script
const request = require('request');
const fs = require('fs');
const getOptions = {
jar:true,
followAllRedirects:true,
method:'GET',
url:'https://dummyurl.com'
};
request.get(getOptions,(err,response,html)=>{
if(err){
console.log('error in request');
console.log(err);
}
else {
const postOptions = {
jar:true,
followAllRedirects: true,
method:'POST',
url:'https://dummyurl.com',
form:{
'data':{
'page':2
}
}
};
request.post(postOptions,(err,response,html)=>{
if(err){
console.log('post err');
console.log(err);
}
else {
fs.writeFileSync('pyres.html',html,'utf8');
}
})
}
});
this is my python conversion of above script
s = requests.Session()
url= 'https://dummyurl.com'
response = s.get(url)
print(response.cookies)
data_url = 'https://dummyurl.com/'
postData = {
"data":{
"page":2
}
}
resultResponse = s.post(data_url,data=postData)
print(resultResponse.content)
Can anyone points me out any mistake in this code?
actually the problem was in data format.
in nodejs I post it like this
{'data':{'page':2} }
but in python it should be converted like this
{
'data[page]': '2'
}
not sure why it was not worked in normal json format in python

Firebase value event listener in python

I have the following code
public static class Post {
public String author;
public String title;
public Post(String author, String title) {
}
}
// Get a reference to our posts
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog/posts");
// Attach a listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Post post = dataSnapshot.getValue(Post.class);
System.out.println(post);
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
I would like to write it in python. But it seems that Firebase library for python doesn't support it? Any ideas?

Thrift python - TApplicationException: Invalid method name

I have 2 services that are defined in the same thrift file and share a port. I can use any method from serviceA no problem but whenever i try to call any of ServiceB's methods i get the exception.
this is my thrift file (service-a.thrift):
service ServiceA extends common.CommonService {
list<i64> getByIds(1: list<i64> ids)
...
}
service ServiceB extends common.CommonService {
list<i64> getByIds(1: list<i64> ids)
...
}
notes:
I'm working with a python client
Thrift version 0.8.0
Any ideas?
We had this need as well and solved by writing a new implementation of TProcessor that creates a map of multiple processors. The only gotcha is that with this implementation you need to ensure no method names overlap - i.e. don't use nice generic names like Run() in different servers. Apologies on not converting C# to Python...
Example Class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Thrift;
using Thrift.Protocol;
/// <summary>
/// Processor that allows for multiple services to run under one roof. Requires no method name conflicts across services.
/// </summary>
public class MultiplexProcessor : TProcessor {
public MultiplexProcessor(IEnumerable<TProcessor> processors) {
ProcessorMap = new Dictionary<string, Tuple<TProcessor, Delegate>>();
foreach (var processor in processors) {
var processMap = (IDictionary) processor.GetType().GetField("processMap_", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(processor);
foreach (string pmk in processMap.Keys) {
var imp = (Delegate) processMap[pmk];
try {
ProcessorMap.Add(pmk, new Tuple<TProcessor, Delegate>(processor, imp));
}
catch (ArgumentException) {
throw new ArgumentException(string.Format("Method already exists in process map: {0}", pmk));
}
}
}
}
protected readonly Dictionary<string, Tuple<TProcessor, Delegate>> ProcessorMap;
internal protected Dictionary<string, Tuple<TProcessor, Delegate>> GetProcessorMap() {
return new Dictionary<string, Tuple<TProcessor, Delegate>>(ProcessorMap);
}
public bool Process(TProtocol iprot, TProtocol oprot) {
try {
TMessage msg = iprot.ReadMessageBegin();
Tuple<TProcessor, Delegate> fn;
ProcessorMap.TryGetValue(msg.Name, out fn);
if (fn == null) {
TProtocolUtil.Skip(iprot, TType.Struct);
iprot.ReadMessageEnd();
var x = new TApplicationException(TApplicationException.ExceptionType.UnknownMethod, "Invalid method name: '" + msg.Name + "'");
oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));
x.Write(oprot);
oprot.WriteMessageEnd();
oprot.Transport.Flush();
return true;
}
Console.WriteLine("Invoking service method {0}.{1}", fn.Item1, fn.Item2);
fn.Item2.Method.Invoke(fn.Item1, new object[] {msg.SeqID, iprot, oprot});
}
catch (IOException) {
return false;
}
return true;
}
}
Example Usage:
Processor = new MultiplexProcessor(
new List<TProcessor> {
new ReportingService.Processor(new ReportingServer()),
new MetadataService.Processor(new MetadataServer()),
new OtherService.Processor(new OtherService())
}
);
Server = new TThreadPoolServer(Processor, Transport);
As far as I know, there's no straightforward way to bind several services to a single port without adding this field to TMessage and recompiling thrift. If you want to have two services using the same Server, you should reimplement Thrift Server, which it doesn't seem an easy task

How to accept POST data in Node.js

I am using NodeJS to send notifications to my clients using NowJS but the data that I need to send in the notifications has to come from my database.
Since I am using Django backend, I can make HTTP requests to my Node.js server and send the required data. But I need to be able to accept this data using Node.js. How can I do this?
How can I do this?
require("http").createServer(function (req, res) {
doStuff();
}).listen(PORT);
I'm a fan of using Connect/Express, so here's a trivial example you could use:
var express = require('express');
var app = express.createServer();
var nowjs = require("now");
var everyone = nowjs.initialize(app);
app.use(express.bodyParser()); //If you want nice JSON parsing
app.post('/sendNotification', function(req, res){
console.log(req.params) //Look at the POST params
console.log(req.body) //Look at the POST body
everyone.now.receiveMessage(req.body.dataYouCareAbout);
res.send('Notification Sent!');
});
You could then use that "sendNotification" endpoint to accept POST data and send it (or some piece of it, or anything really) down to NowJS.
Use formidable. Here's a short example from their docs:
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}

Categories

Resources