rstudio / vetiver-python

Version, share, deploy, and monitor models.
https://rstudio.github.io/vetiver-python/stable/
MIT License
60 stars 17 forks source link

Error when predicting with a dataframe using an API deployed version 0.1.6 #94

Closed xuf12 closed 2 years ago

xuf12 commented 2 years ago

Describe the bug I get this error when predicting with both 0.1.5 and 0.1.6 versions of vetiver using an endpoint deployed with version 0.1.6 of vetiver from PyPI.

Traceback (most recent call last): File "/usr/home/xu.fei/bike_predict_python/app/.venv/lib/python3.9/site-packages/requests/models.py", line 971, in json return complexjson.loads(self.text, **kwargs) File "/opt/python/3.9.6/lib/python3.9/json/init.py", line 346, in loads return _default_decoder.decode(s) File "/opt/python/3.9.6/lib/python3.9/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/opt/python/3.9.6/lib/python3.9/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/home/xu.fei/bike_predict_python/app/.venv/lib/python3.9/site-packages/shiny/session/_session.py", line 977, in output_obs message[output_name] = fn() File "/usr/home/xu.fei/bike_predict_python/app/.venv/lib/python3.9/site-packages/shiny/render/_render.py", line 205, in call return _utils.run_coro_sync(self._run()) File "/usr/home/xu.fei/bike_predict_python/app/.venv/lib/python3.9/site-packages/shiny/_utils.py", line 178, in run_coro_sync coro.send(None) File "/usr/home/xu.fei/bike_predict_python/app/.venv/lib/python3.9/site-packages/shiny/render/_render.py", line 219, in _run x = await self._fn() File "/usr/home/xu.fei/bike_predict_python/app/.venv/lib/python3.9/site-packages/shiny/_utils.py", line 132, in fn_async return fn() File "/usr/home/xu.fei/bike_predict_python/app/app.py", line 202, in plot df_to_plot_id["pred"] = predict(endpoint, df_to_pred) File "/usr/home/xu.fei/bike_predict_python/app/.venv/lib/python3.9/site-packages/vetiver/server.py", line 227, in predict response_df = pd.DataFrame.from_dict(response.json()) File "/usr/home/xu.fei/bike_predict_python/app/.venv/lib/python3.9/site-packages/requests/models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

To Reproduce Steps to reproduce the behavior:

import pandas as pd from vetiver.server import predict, vetiver_endpoint data = { "id": ["1", "2"], "hour": [1, 10], "month": [7, 8], "Friday": [0,0], "Monday": [0,0], "Saturday": [1,0], "Sunday": [0,0], "Thursday": [0,0], "Tuesday": [0,0], "Wednesday": [0,1], } df_to_test = pd.DataFrame.from_dict(data)

# works with API deployed with 0.1.5 endpoint_150 = vetiver_endpoint( "https://colorado.rstudio.com/rsc/new-bikeshare-model/predict/" ) predict(endpoint_150, df_to_test)

# doesn't work with API deployed with 0.1.6 endpoint_160 = vetiver_endpoint( "https://colorado.rstudio.com/rsc/bike-predict-python-api/predict/" ) predict(endpoint_160, df_to_test)

Expected behavior I expect endpoint_160 to work the same way as endpoint_150.

Screenshots If applicable, add screenshots to help explain your problem.

@gsingh91

isabelizimm commented 2 years ago

Hey, thanks for bringing this up! To help me diagnose--are you able to make predictions in the browser (say url/__docs__)?

gsingh91 commented 2 years ago

yes that works

image

I will also add you as a user to the new API deployment so that you can access it for further testing.

isabelizimm commented 2 years ago

Hey! Just checked this out-- try removing the trailing slash (should look like /predict, not /predict/) on the v0.1.6 endpoint. :) this was updated to match the R side! I tested this on your endpoint, and it seems to work okay, so I am closing this issue. Feel free to reopen it if this is not the case!

gsingh91 commented 2 years ago

@isabelizimm reopening this as I am running in the same issue as @xuf12 with a new Vetiver API deployed using 0.1.6 https://colorado.rstudio.com/rsc/bike-predict-python-api/

gsingh91 commented 2 years ago
from vetiver.server import predict, vetiver_endpoint
endpoint = vetiver_endpoint("https://colorado.rstudio.com/rsc/bike-predict-python-api/predict")
endpoint
import pandas as pd
new_dict = {
  "id": [121],
  "hour": [1],
  "month": [1],
  "Friday": [0],
  "Monday": [0],
  "Saturday": [0],
  "Sunday": [1],
  "Thursday": [0],
  "Tuesday": [0],
  "Wednesday": [0]
}

new_df = pd.DataFrame(new_dict)
new_df
predict(endpoint, new_df)
isabelizimm commented 2 years ago

Hi Gagan-- it looks like you need authentication for your endpoint. Try this ⬇️

h = { 'Authorization': f'Key {api_key}' }

predict(endpoint, new_df, headers = h)