6
$\begingroup$

I have a function whose argument is Audio and I'd like to deploy it as an API (and then call it from Python). This works as with FormPage[] as follows:

CloudDeploy[
 FormPage[{{"ex", "Upload WAV file:"} -> "UploadedFile"}, 
  AudioPlot@Import[#ex] &]]

But I'm not sure how to set it up with APIFunction[] and then call it as a HTTP GET request. I can assume they are WAV files if that makes it easier.

$\endgroup$
5
$\begingroup$

You can create an APIFunction with a very similar syntax and return e.g. a png"

CloudDeploy[
  APIFunction[
    {{"ex", "Upload WAV file:"} -> Restricted["UploadedFile", "WAV"]}
  , HTTPResponse[ExportString[AudioPlot@Import[#ex, "WAV"], "PNG"]] &
  ]
, "api/audioPlot"
, Permissions -> "Public"
];

Can't say how to call it from Python but from WL it goes like this:

req = HTTPRequest[
  CloudObject["api/audioPlot"]
, <|"Body" -> {"ex" -> File[path/to/file.wav]}|>
];

URLExecute[req, "PNG"]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.