Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FastAPI {"detail":"Method Not Allowed"}

I am using FAST API for my ML Model.

I have a pipeline.

lr_tfidf = Pipeline([('vect', tfidf),
                     ('clf', LogisticRegression(penalty='l2'))])


Now In Fast API, when I want to predict, and display result as API, my code is

app = FastAPI()


@app.post('/predict')
def predict_species(data: str):
    data = np.array([data])

    prob = lr_tfidf.predict_proba(data).max()
    pred = lr_tfidf.predict(data)
    return {'Probability': f'{prob}', 
            'Predictions':f'{pred}'}

I copied it from a tutorial. When I test it on GUI by FASTAPI, it works good as shown in Image, i.e it shows probability and predictions.

enter image description here

When I go to request URL, as provided by the GUI, which is http://127.0.0.1:8000/predict?data=hello (test data is hello) It gives me error.

{"detail":"Method Not Allowed"}

On my Terminal, the error message is

INFO:     127.0.0.1:42568 - "GET /predict?data=hello HTTP/1.1" 405 Method Not Allowed
like image 503
Ahmad Anis Avatar asked Jan 25 '23 10:01

Ahmad Anis


1 Answers

The method of the endpoint is defined as POST (@app.post('/predict')). When you call the URL from your browser, the HTTP Method is GET.

A simply solution is to change the endpoints method to GET via @app.get.

But this will most likely violates how REST-API endpoints should be named and when to use what HTTP method. A good starting point is https://restfulapi.net/resource-naming/.

Or maybe you are implementing an RPC (remote procedure call)? Than it can be different as well.

like image 191
finswimmer Avatar answered Feb 02 '23 10:02

finswimmer