Sync App files
Browse files- routes/__init__.py +0 -0
- routes/__pycache__/__init__.cpython-39.pyc +0 -0
- routes/__pycache__/model.cpython-39.pyc +0 -0
- routes/__pycache__/task.cpython-39.pyc +0 -0
- routes/model.py +50 -0
- routes/task.py +22 -0
routes/__init__.py
ADDED
File without changes
|
routes/__pycache__/__init__.cpython-39.pyc
ADDED
Binary file (118 Bytes). View file
|
|
routes/__pycache__/model.cpython-39.pyc
ADDED
Binary file (1.99 kB). View file
|
|
routes/__pycache__/task.cpython-39.pyc
ADDED
Binary file (829 Bytes). View file
|
|
routes/model.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Depends, HTTPException, Request, Response, UploadFile
|
2 |
+
from pydantic import BaseModel, Field
|
3 |
+
from typing import Literal
|
4 |
+
from routes.task import predict_drug
|
5 |
+
import skops.io as sio
|
6 |
+
|
7 |
+
# Create an instance of the FastAPI class
|
8 |
+
router = APIRouter(
|
9 |
+
prefix="/api",
|
10 |
+
tags=["api"],
|
11 |
+
responses={404: {"description": "Not found"}}
|
12 |
+
)
|
13 |
+
|
14 |
+
class PredictDrugInput(BaseModel):
|
15 |
+
age: int = Field(..., ge=15, le=74, description="Age of the patient (15 to 74)")
|
16 |
+
sex: Literal["M", "F"] = Field(..., description="Sex of the patient (M or F)")
|
17 |
+
blood_pressure: Literal["HIGH", "LOW", "NORMAL"] = Field(..., description="Blood pressure level")
|
18 |
+
cholesterol: Literal["HIGH", "NORMAL"] = Field(..., description="Cholesterol level")
|
19 |
+
na_to_k_ratio: float = Field(..., ge=6.2, le=38.2, description="Sodium-to-potassium ratio in blood (6.2 to 38.2)")
|
20 |
+
|
21 |
+
model_config = {
|
22 |
+
"json_schema_extra": {
|
23 |
+
"examples": [
|
24 |
+
{
|
25 |
+
"age": 30,
|
26 |
+
"sex": "M",
|
27 |
+
"blood_pressure": "HIGH",
|
28 |
+
"cholesterol": "HIGH",
|
29 |
+
"na_to_k_ratio": 10
|
30 |
+
}
|
31 |
+
]
|
32 |
+
}
|
33 |
+
}
|
34 |
+
|
35 |
+
# Define a GET endpoint
|
36 |
+
@router.get("/")
|
37 |
+
def read_root():
|
38 |
+
return {"message": "Hello, welcome to the demo"}
|
39 |
+
|
40 |
+
@router.get("/get_perams")
|
41 |
+
def get_perams():
|
42 |
+
pipe = sio.load("./Model/drug_pipeline.skops", trusted=True)
|
43 |
+
model = pipe.named_steps["model"]
|
44 |
+
model_params = model.get_params()
|
45 |
+
return model_params
|
46 |
+
|
47 |
+
@router.post("/predict")
|
48 |
+
def predict_ml(input_data: PredictDrugInput):
|
49 |
+
label = predict_drug(input_data.age, input_data.sex, input_data.blood_pressure, input_data.cholesterol, input_data.na_to_k_ratio)
|
50 |
+
return label
|
routes/task.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import skops.io as sio
|
2 |
+
|
3 |
+
pipe = sio.load("./Model/drug_pipeline.skops", trusted=True)
|
4 |
+
|
5 |
+
def predict_drug(age, sex, blood_pressure, cholesterol, na_to_k_ratio):
|
6 |
+
"""Predict drugs based on patient features.
|
7 |
+
|
8 |
+
Args:
|
9 |
+
age (int): Age of patient
|
10 |
+
sex (str): Sex of patient
|
11 |
+
blood_pressure (str): Blood pressure level
|
12 |
+
cholesterol (str): Cholesterol level
|
13 |
+
na_to_k_ratio (float): Ratio of sodium to potassium in blood
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
str: Predicted drug label
|
17 |
+
"""
|
18 |
+
features = [age, sex, blood_pressure, cholesterol, na_to_k_ratio]
|
19 |
+
predicted_drug = pipe.predict([features])[0]
|
20 |
+
|
21 |
+
label = f"Predicted Drug: {predicted_drug}"
|
22 |
+
return label
|