36
loading...
This website collects cookies to deliver better user experience
Open the api folder in VSCode and install the following libraries using the terminal.
pip install fastapi
pip install uvicorn[standard]
pip install scikit-learn
On VScode click on extensions and install Thunder Client.
# Importing necessary libraries
import uvicorn
import pickle
from pydantic import BaseModel
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# Initializing the fast API server
app = FastAPI()
origins = [
"http://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost",
"http://localhost:8080",
"http://localhost:3000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Loading up the trained model
model = pickle.load(open('../model/hireable.pkl', 'rb'))
# Defining the model input types
class Candidate(BaseModel):
gender: int
bsc: float
workex: int
etest_p: float
msc: float
# Setting up the home route
@app.get("/")
def read_root():
return {"data": "Welcome to online employee hireability prediction model"}
# Setting up the prediction route
@app.post("/prediction/")
async def get_predict(data: Candidate):
sample = [[
data.gender,
data.bsc,
data.workex,
data.etest_p,
data.msc
]]
hired = model.predict(sample).tolist()[0]
return {
"data": {
'prediction': hired,
'interpretation': 'Candidate can be hired.' if hired == 1 else 'Candidate can not be hired.'
}
}
# Configuring the server host and port
if __name__ == '__main__':
uvicorn.run(app, port=8080, host='0.0.0.0')