jrosebr1 / simple-keras-rest-api

A simple Keras REST API using Flask
MIT License
374 stars 197 forks source link

JSON decode error with simple_request.py #2

Closed kraigrs closed 6 years ago

kraigrs commented 6 years ago

I'm having some trouble using requests to programatically call the REST API. Submitting the request via cURL works without issues, but when I attempt to run the same image through simple_request.py, I receive the following error:

  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-19-0c3919566f71>", line 1, in <module>
    r = requests.post(KERAS_REST_API_URL, files=payload).json()
  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py", line 885, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

When I run the script without .json() at the end of the request, it returns a very strange HTML response that I can't make heads or tails of.

I am on a Windows 10 machine (corporate environment), Python 3.6.1, Keras v2.0.8, TensorFlow v1.3.0, requests v2.14.2

Any help with this would be greatly appreciated!

jrosebr1 commented 6 years ago

It looks like there may be a problem decoding the image. I'm not sure what may be causing that problem but you'll likely want to ensure the image can be loaded via Keras' load_img function locally. If it can be loaded locally, then there is an encoding/decoding issue that you may need to debug further.

kraigrs commented 6 years ago

load_img works as expected (which is what I was afraid of), still very peculiar that it runs fine from cURL but not using requests.

I'll get back to debugging and let you know what, if anything, works.

jrosebr1 commented 6 years ago

Hm, unfortunately I'm not sure what the issue may be then. I have two other tutorials on building a Keras API available here:

My only guess is that it could be related to C-contiguous ordering, but outside of that, I'm unfortunately not sure what the root cause is. Sorry I couldn't be of more help here!

kraigrs commented 6 years ago

I figured out the issue. Because of certain web services we were accessing that wouldn't allow a proxy to be specified, I edited the requests library to use our corporate proxies by default. When I turned this off, it magically worked, so going through our proxy seemed to be messing up the encoding/decoding for the image being posted. I would call this resolved, thank you for creating these resources!

jrosebr1 commented 6 years ago

Awesome, congrats on resolving the issue Kraig!

hasibzunair commented 6 years ago

@kraigrs could you say how exactly you fixed the problem?

Thanks

kraigrs commented 5 years ago

@hasibzunair I had originally edited the source code in the requests library to reflect corporate proxy rules, and when I returned them to their default, it worked. If you haven't made such changes to requests, I don't suspect that would be the problem.