File size: 6,617 Bytes
27dbc11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, validator
import pickle
import joblib
import numpy as np
import tensorflow as tf
import pandas as pd
app = FastAPI()
# Input validation using Pydantic
class HealthPredictionRequest(BaseModel):
Gender: str
Age: int
SBP: int
HBP: int
heart_rate: int
Glucose: int
SpO2: int
Temprature: float
@validator("Gender")
def validate_gender(cls, value):
if value not in ["M", "F"]:
raise ValueError("Gender must be 'M' or 'F'.")
return value
@validator("Age", "SBP", "HBP", "heart_rate", "Glucose", "SpO2")
def validate_positive_integers(cls, value):
if value <= 0:
raise ValueError("Values must be positive integers.")
return value
@validator("Temprature")
def validate_temperature(cls, value):
if value < 95.0 or value > 105.0: # Example temperature range
raise ValueError("Temperature must be between 95.0 and 105.0.")
return value
# Function to make predictions
def get_prediction(Gender, Age, SBP, HBP, heart_rate, Glucose, SpO2, Temprature):
# Load the scaler
with open('minmax_scaler.pkl', 'rb') as file:
scaler = pickle.load(file)
# Load the model
model_path = 'random_forest_model.pkl'
with open(model_path, 'rb') as file:
model = joblib.load(file)
# Load the label encoder for Gender
with open('label_encoder.pkl', 'rb') as file:
label_encoder = pickle.load(file)
# Convert Gender to numeric
Gender_encoded = label_encoder.transform([Gender])[0]
# Create input DataFrame
input_data = pd.DataFrame(
[[Gender_encoded, Age, SBP, HBP, heart_rate, Glucose, SpO2, Temprature]],
columns=['Gender', 'Age', 'SBP ', 'HBP ', 'heart_rate ', 'Glucose ', 'SpO2', 'Temprature ']
)
# Scale the input data
input_data_scaled = scaler.transform(input_data)
# Make prediction
prediction = model.predict(input_data_scaled)
# Map prediction to label
label_map = {
0: 'healthy',
1: 'high BP',
2: 'low BP',
3: 'high sugar',
4: 'low sugar',
5: 'low oxygen',
6: 'high temperature',
7: 'heartbeat is high',
8: 'risk'
}
return label_map[prediction[0]]
# Define the input data structure using Pydantic
class FraudInput(BaseModel):
V1: float
V2: float
V3: float
V4: float
V5: float
V6: float
V7: float
V8: float
V9: float
V10: float
V11: float
V12: float
V13: float
V14: float
V15: float
V16: float
V17: float
V18: float
V19: float
V20: float
V21: float
V22: float
V23: float
V24: float
V25: float
V26: float
V27: float
V28: float
Amount: float
# Inference method for fraud detection
def fraud_inference(features, scaler_path="fraud_scaler.pkl", model_path="ann_model.h5"):
# Load scaler and model
with open(scaler_path, "rb") as f:
scaler = pickle.load(f)
ann_model_loaded = tf.keras.models.load_model(model_path)
# Scale features
scaled_features = scaler.transform(features)
# Perform inference
predictions = ann_model_loaded.predict(scaled_features)
predicted_label = np.argmax(predictions, axis=-1)
if predicted_label[0] == 0:
return 'Not Fraud'
else:
return 'Fraud'
class CrimeData(BaseModel):
Case: str
Block: str
IUCR: int
Primary_Type: str
Description: str
Location_Description: str
FBI_Code: int
Updated_On: str
Location: str
def crime_inference(Case, Block, IUCR, Primary_Type, Description, Location_Description, FBI_Code, Updated_On, Location):
# Load the scaler
with open('crime_scaler.pkl', 'rb') as file:
scaler = joblib.load(file)
# Load the model
model_path = 'xgboost_model.pkl'
with open(model_path, 'rb') as file:
model = joblib.load(file)
# Load the PCA
with open('crime_pca.pkl', 'rb') as file:
pca = joblib.load(file)
with open('crime_label_encoder.pkl', 'rb') as file:
label_encoder = joblib.load(file)
# Create input DataFrame
input_data = pd.DataFrame(
[[Case, Block, IUCR, Primary_Type, Description, Location_Description, FBI_Code, Updated_On, Location]],
columns=['Case Number', 'Block', 'IUCR', 'Primary Type', 'Description', 'Location Description', 'FBI Code',
'Updated On', 'Location']
)
categorical_cols = ['Case Number', 'Block', 'IUCR', 'Primary Type', 'Description',
'Location Description', 'FBI Code', 'Updated On', 'Location']
# Label encoding for categorical columns
for col in categorical_cols:
input_data[col] = label_encoder.fit_transform(input_data[col])
# Scale the input data
input_data_scaled = scaler.transform(input_data)
# Apply PCA transformation
pca_features = pca.transform(input_data_scaled)
print(pca_features.shape)
# Make prediction
prediction = model.predict(pca_features)
# Map prediction to label
label_map = {0: 'not arrest', 1: 'arrest'}
return label_map[prediction[0]]
# API endpoint
@app.post("/health_predict")
def predict(request: HealthPredictionRequest):
try:
# Call the prediction function
result = get_prediction(
Gender=request.Gender,
Age=request.Age,
SBP=request.SBP,
HBP=request.HBP,
heart_rate=request.heart_rate,
Glucose=request.Glucose,
SpO2=request.SpO2,
Temprature=request.Temprature
)
return {"prediction": result}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# Define an endpoint for prediction
@app.post("/fraud_predict")
async def predict(input_data: FraudInput):
# Convert input data to DataFrame
data_dict = input_data.dict()
data = pd.DataFrame([data_dict])
# Call the fraud detection inference method
label = fraud_inference(data)
return {"prediction": label}
@app.post("/predict_crime")
async def predict_crime(data: CrimeData):
result = crime_inference(
Case=data.Case,
Block=data.Block,
IUCR=data.IUCR,
Primary_Type=data.Primary_Type,
Description=data.Description,
Location_Description=data.Location_Description,
FBI_Code=data.FBI_Code,
Updated_On=data.Updated_On,
Location=data.Location
)
return {"prediction": result}
|