Team Ai
Apppublic

Kwasiasomani/Sepsis_Machine_Learning_API_using_FastAPI

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
API_app.py96 linesDownload Raw Back to root
1"""2FastAPI script for Sepssis and model prediction3Author: Equity4Date: May.30th 20235"""6 7 8# The library for the API Code9from fastapi import FastAPI10import pickle11import uvicorn12from pydantic import BaseModel13import pandas as pd14 15 16 17# Declare the data with its components and their type18class model_input(BaseModel):19    20    PRG: int21    PL: int22    PR: int23    SK: int24    TS: int25    M11: float26    BD2: float27    Age: int28    Insurance:int29 30 31app = FastAPI(title = 'Sepssis API',32              description = 'An API that takes input and display the predictions',33              version = '0.1.0')34 35# Load the saved data36toolkit = "P6_toolkit"37 38def load_toolkit(filepath = toolkit):39    with open(toolkit, "rb") as file:40        loaded_toolkit = pickle.load(file)41    return loaded_toolkit42 43toolkit = load_toolkit()44scaler = toolkit["scaler"]45model = toolkit["model"]46 47 48@app.get("/")49async def hello():50    return "Welcome to our model API"51 52 53 54@app.post("/Sepssis")55async def prediction(input:model_input):56   data = {57           'PRG': input.PRG, 58           'PL': input.PL,59           'PR': input.PR,60           'SK': input.SK,61           'TS': input.TS,62           'M11': input.M11,63           'BD2': input.BD2,64           'Age': input.Age,65           'Insurance': input.Insurance,66                }67   68# prepare the data as a dataframe69   df = pd.DataFrame(data, index=[0])70 71 72   #numerical features73   numeric_columns =  [ 'PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age','Insurance']74   75   #scaling76   Scaler = scaler.transform(df[numeric_columns])77   Scaled = pd.DataFrame(Scaler)                  78   prediction = model.predict(Scaled).tolist()79   probability = model.predict_proba(Scaled)80 81 82    # Labelling Model output83   if (prediction[0] < 0.5):84         prediction = "Negative. This person has no Sepssis"85   else: 86      prediction = "Positive. This person has Sepssis"87   data['prediction'] = prediction  88   return data89   90   91        92 93 94# Launch the app95if __name__ == "__main__":96 uvicorn.run("API_app:app",host = '0.0.0.0', port = 7860)