Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException, Query | |
| from pydantic import BaseModel | |
| import pickle | |
| import pandas as pd | |
| app = FastAPI( | |
| title="Sepsis Prediction API", | |
| description="This FastAPI application provides sepsis predictions using a machine learning model.", | |
| version="1.0" | |
| ) | |
| # Load the model and key components | |
| with open('model_and_key_components.pkl', 'rb') as file: | |
| loaded_components = pickle.load(file) | |
| loaded_model = loaded_components['model'] | |
| loaded_encoder = loaded_components['encoder'] | |
| loaded_scaler = loaded_components['scaler'] | |
| # Define the input data structure using Pydantic BaseModel | |
| class InputData(BaseModel): | |
| PRG: int = Query(..., title="Patient's Pregnancy Count", description="Enter the number of pregnancies.", example=2) | |
| PL: float = Query(..., title="Platelet Count", description="Enter the platelet count.", example=150.0) | |
| PR: float = Query(..., title="Pulse Rate", description="Enter the pulse rate.", example=75.0) | |
| SK: float = Query(..., title="Skin Thickness", description="Enter the skin thickness.", example=25.0) | |
| TS: int = Query(..., title="Triceps Skin Fold Thickness", description="Enter the triceps skin fold thickness.", example=30) | |
| M11: float = Query(..., title="Insulin Level", description="Enter the insulin level.", example=120.0) | |
| BD2: float = Query(..., title="BMI", description="Enter the Body Mass Index (BMI).", example=32.0) | |
| Age: int = Query(..., title="Age", description="Enter the patient's age.", example=35) | |
| # Define the output data structure using Pydantic BaseModel | |
| class OutputData(BaseModel): | |
| Sepsis: str | |
| # Define a function to preprocess input data | |
| def preprocess_input_data(input_data: InputData): | |
| # Encode Categorical Variables (if needed) | |
| # All columns are numerical. No need for encoding | |
| # Apply scaling to numerical data | |
| numerical_cols = ['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age'] | |
| input_data_scaled = loaded_scaler.transform([list(input_data.dict().values())]) | |
| return pd.DataFrame(input_data_scaled, columns=numerical_cols) | |
| # Define a function to make predictions | |
| def make_predictions(input_data_scaled_df: pd.DataFrame): | |
| y_pred = loaded_model.predict(input_data_scaled_df) | |
| sepsis_mapping = {0: 'Negative', 1: 'Positive'} | |
| return sepsis_mapping[y_pred[0]] | |
| async def root(): | |
| # Endpoint at the root URL ("/") returns a welcome message with a clickable link | |
| message = "Welcome to your Sepsis Classification API! Click [here](/docs) to access the API documentation." | |
| return {"message": message} | |
| async def predict_sepsis(input_data: InputData): | |
| try: | |
| input_data_scaled_df = preprocess_input_data(input_data) | |
| sepsis_status = make_predictions(input_data_scaled_df) | |
| return {"Sepsis": sepsis_status} | |
| except Exception as e: | |
| # Handle exceptions and return an error response | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| # Run the FastAPI application on the local host and port 8000 | |
| uvicorn.run(app, host="127.0.0.1", port=8000) | |