Sync App files
Browse files- .DS_Store +0 -0
- drug_app.py +60 -0
- init.py +0 -0
- test/test_model.py +31 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
drug_app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import skops.io as sio
|
| 3 |
+
|
| 4 |
+
pipe = sio.load("./Model/drug_pipeline.skops", trusted=True)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def predict_drug(age, sex, blood_pressure, cholesterol, na_to_k_ratio):
|
| 8 |
+
"""Predict drugs based on patient features.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
age (int): Age of patient
|
| 12 |
+
sex (str): Sex of patient
|
| 13 |
+
blood_pressure (str): Blood pressure level
|
| 14 |
+
cholesterol (str): Cholesterol level
|
| 15 |
+
na_to_k_ratio (float): Ratio of sodium to potassium in blood
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
str: Predicted drug label"""
|
| 19 |
+
|
| 20 |
+
features = [age, sex, blood_pressure, cholesterol, na_to_k_ratio]
|
| 21 |
+
predicted_drug = pipe.predict([features])[0]
|
| 22 |
+
|
| 23 |
+
label = f"Predicted Drug: {predicted_drug}"
|
| 24 |
+
return label
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
inputs = [
|
| 28 |
+
gr.Slider(15, 74, step=1, label="Age"),
|
| 29 |
+
gr.Radio(["M", "F"], label="Sex"),
|
| 30 |
+
gr.Radio(["HIGH", "LOW", "NORMAL"], label="Blood Pressure"),
|
| 31 |
+
gr.Radio(["HIGH", "NORMAL"], label="Cholesterol"),
|
| 32 |
+
gr.Slider(6.2, 38.2, step=0.1, label="Na_to_K"),
|
| 33 |
+
]
|
| 34 |
+
outputs = [gr.Label(num_top_classes=5)]
|
| 35 |
+
|
| 36 |
+
examples = [
|
| 37 |
+
[30, "M", "HIGH", "NORMAL", 15.4],
|
| 38 |
+
[35, "F", "LOW", "NORMAL", 8],
|
| 39 |
+
[50, "M", "HIGH", "HIGH", 34],
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
title = "Drug Classification"
|
| 44 |
+
description = "Enter the details to correctly identify Drug type?"
|
| 45 |
+
article = "This app is a part of the Beginner's Guide to CI/CD for Machine Learning. It teaches how to automate training, evaluation, and deployment of models to Hugging Face using GitHub Actions."
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
gr.Interface(
|
| 49 |
+
fn=predict_drug,
|
| 50 |
+
inputs=inputs,
|
| 51 |
+
outputs=outputs,
|
| 52 |
+
examples=examples,
|
| 53 |
+
title=title,
|
| 54 |
+
description=description,
|
| 55 |
+
article=article,
|
| 56 |
+
theme=gr.themes.Soft(),
|
| 57 |
+
).launch()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# I also want to publish show mlflow results, maybe some logs, maybe auc scores ans stuff
|
init.py
ADDED
|
File without changes
|
test/test_model.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sklearn.model_selection import train_test_split
|
| 3 |
+
import skops.io as sio
|
| 4 |
+
from sklearn.metrics import roc_auc_score
|
| 5 |
+
|
| 6 |
+
class TestModel():
|
| 7 |
+
|
| 8 |
+
def __init__(self, df):
|
| 9 |
+
|
| 10 |
+
drug_df = pd.read_csv('/Users/muratdemiralay/Downloads/drug200.csv')
|
| 11 |
+
X = drug_df.drop("Drug", axis=1).values
|
| 12 |
+
y = drug_df.Drug.values
|
| 13 |
+
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
|
| 14 |
+
X, y, test_size=0.3, random_state=125)
|
| 15 |
+
|
| 16 |
+
self.model = sio.load("Model/drug_pipeline.skops")
|
| 17 |
+
|
| 18 |
+
def test_auc(self):
|
| 19 |
+
|
| 20 |
+
preds = self.model.predict_proba(self.X_test)[:,1]
|
| 21 |
+
auc = roc_auc_score(self.y_test, preds)
|
| 22 |
+
assert auc > 0.8, "Auc is below acceptable threshold"
|
| 23 |
+
|
| 24 |
+
def test_missing_values(self):
|
| 25 |
+
|
| 26 |
+
assert self.drug_df.isna().sum().sum() == 0, "Dataset contains missing values"
|
| 27 |
+
|
| 28 |
+
def test_pipeline_execution(self):
|
| 29 |
+
|
| 30 |
+
assert len(self.X_train) > 0, "Training data is empty!"
|
| 31 |
+
assert len(self.y_train) > 0, "Labels are empty!"
|