SoraHeart
SoraHeart

Reputation: 420

how do I make API request after deploying uvicorn via fastapi in mylaptop

I am new to FastAPI/model deployment, I managed to created a model which can return a result using the FastAPI docs path in my local server/laptop.

My question is, how can i access this model in jupyter notebook?

Trying to relate into other API request such as Foursquare API. How do I wrap a params dictionary

Tried the following however it is not working

import requests

url = 'http://127.0.0.1:8000/predict'
params = { 'title' : ['Product Operations Software Engineer (DevOps / SRE)']}

result = requests.post(url, params)

<Response [422]>
result.text
'{"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}'

Input enter image description here

Output enter image description here

My Code

from  fastapi import FastAPI
import numpy as np
import dill
import pandas as pd
import uvicorn
import re
from pydantic import BaseModel

app = FastAPI()

class JobTitle(BaseModel):
    title: list


#load model
with open("logisticmodel", "rb") as to_load:
    logistic_model = dill.load(to_load)

@app.get("/")
def home():
    return 'welcome'

@app.post("/predict")
def predict(request: JobTitle):
    
    X = pd.Series(request.title)
    result = logistic_model.predict(X)
    return {'Type' : list(result) }

Upvotes: 2

Views: 2956

Answers (3)

klee1611
klee1611

Reputation: 241

Your code here indicates that your data should be put inside the HTTP request body

@app.post("/predict")
def predict(request: JobTitle):
    ...

But when you issue the HTTP request with the requests module,
you didn't put anything to your request body.
You should use json argument so the data can be put to the right place:

result = requests.post(
    url,
    json={'title': ['Product Operations Software Engineer (DevOps / SRE)']}
)

You can check this for sending an HTTP request with request body using requests module,
and this for building an API with a request body.

Upvotes: 1

Shod
Shod

Reputation: 925

This works:

>>> result = requests.post(url, json=params)
>>> result
<Response [200]>

Upvotes: 0

Ashraful
Ashraful

Reputation: 1964

You need to use .dict() to get the dictionary of BaseModel instance. In your case, request.dict()

Also, Define your model like this,

class JobTitle(BaseModel):
    title: List[str]

Upvotes: 0

Related Questions