I have a simple API that takes a recipe and returns the list of ingredients:

api = CloudDeploy[APIFunction[{"img"->"Image"},
    (ResourceFunction["DeleteFailures"]@Map[Function[w, Interpreter["FoodType"][w]], Flatten @ TextRecognize[#img, "Word"]])&
], Permissions->"Public", IncludeDefinitions->True]

So here's the example I'm trying to make work:

recipe = CloudGet@"https://www.wolframcloud.com/obj/fe25d585-acc3-4931-9223-da5ee1dce6bd";

enter image description here

The function inside the API works just fine locally (without the APIFunction & CloudDeploy) as you can see:

AbsoluteTiming[Function[{img}, ResourceFunction["DeleteFailures"]@Map[Function[w, 
     Interpreter["FoodType"][w]], Flatten@TextRecognize[img, "Word"]]][recipe]]

enter image description here

But when deployed as an API in the Wolfram Cloud, it refuses to return anything, how can I get this to work?

What I've Tried

A simple POST request with URLExecute gives a 408 error:

AbsoluteTiming[URLExecute[api, <|"img"->recipe|>, TimeConstraint->\[Infinity]]]

enter image description here

Another attempt with the obsolete-but-still-super-useful URLFetch with multi-part data gives a 503 error (btw, does anyone know how to accomplish this without using an obsolete symbol?):

AbsoluteTiming[URLFetch[api, "Method" -> "POST",  "MultipartElements" -> {{"img", "image/jpeg"} -> ExportString[recipe, "JPEG"]}]]

enter image description here

  • 1
    $\begingroup$ TextRecognize seems to crash in the public cloud. If I run this simple example in an interactive cloud notebook the kernel quits: ((TextRecognize[ChanVeseBinarize[#1, "TargetColor" -> Black]] &)[ Import["https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"]]) === "Google" And that is the reason your APIFunction does not work. ( I've chose that example because it evaluates to True both on Mathematica 12.0 and 12.1, and because it works in a WEPC 1.54.1 environment (with 12.0) ) $\endgroup$ May 12, 2020 at 15:21
  • 4
    $\begingroup$ Right using TextRecognize crashes on Public cloud. We will investigate this. Thanks. $\endgroup$
    – dsingh
    May 13, 2020 at 14:08
  • 1
    $\begingroup$ TextRecognize crash is now fixed on Public cloud. $\endgroup$
    – dsingh
    Dec 14, 2020 at 22:52
  • 1
    $\begingroup$ @dsingh Yes, but the URLExecute api request still times-out... $\endgroup$
    – M.R.
    Dec 15, 2020 at 1:05
  • 1
    $\begingroup$ @dsingh the URLFetch looks like it works slowly, but then URLExecute should work too, or at least message what the issue is no? $\endgroup$
    – M.R.
    Dec 16, 2020 at 4:02


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.