Spaces:
Build error
Build error
Sync App files
Browse files- app.py +74 -0
- requirements.txt +2 -1
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import skops.io as sio
|
| 3 |
+
import warnings
|
| 4 |
+
from sklearn.exceptions import InconsistentVersionWarning
|
| 5 |
+
|
| 6 |
+
# Suppress the version warnings
|
| 7 |
+
warnings.filterwarnings("ignore", category=InconsistentVersionWarning)
|
| 8 |
+
|
| 9 |
+
# Explicitly specify trusted types
|
| 10 |
+
trusted_types = [
|
| 11 |
+
"sklearn.pipeline.Pipeline",
|
| 12 |
+
"sklearn.preprocessing.OneHotEncoder",
|
| 13 |
+
"sklearn.preprocessing.StandardScaler",
|
| 14 |
+
"sklearn.compose.ColumnTransformer",
|
| 15 |
+
"sklearn.preprocessing.OrdinalEncoder",
|
| 16 |
+
"sklearn.impute.SimpleImputer",
|
| 17 |
+
"sklearn.tree.DecisionTreeClassifier",
|
| 18 |
+
"sklearn.ensemble.RandomForestClassifier",
|
| 19 |
+
"numpy.dtype",
|
| 20 |
+
]
|
| 21 |
+
pipe = sio.load("./model/drug_pipeline.skops", trusted=trusted_types)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def predict_drug(age, sex, blood_pressure, cholesterol, na_to_k_ratio):
|
| 25 |
+
"""Predict drugs based on patient features.
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
age (int): Age of patient
|
| 29 |
+
sex (str): Sex of patient
|
| 30 |
+
blood_pressure (str): Blood pressure level
|
| 31 |
+
cholesterol (str): Cholesterol level
|
| 32 |
+
na_to_k_ratio (float): Ratio of sodium to potassium in blood
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
str: Predicted drug label
|
| 36 |
+
"""
|
| 37 |
+
features = [age, sex, blood_pressure, cholesterol, na_to_k_ratio]
|
| 38 |
+
predicted_drug = pipe.predict([features])[0]
|
| 39 |
+
|
| 40 |
+
label = f"Predicted Drug: {predicted_drug}"
|
| 41 |
+
return label
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
inputs = [
|
| 45 |
+
gr.Slider(15, 74, step=1, label="Age"),
|
| 46 |
+
gr.Radio(["M", "F"], label="Sex"),
|
| 47 |
+
gr.Radio(["HIGH", "LOW", "NORMAL"], label="Blood Pressure"),
|
| 48 |
+
gr.Radio(["HIGH", "NORMAL"], label="Cholesterol"),
|
| 49 |
+
gr.Slider(6.2, 38.2, step=0.1, label="Na_to_K"),
|
| 50 |
+
]
|
| 51 |
+
outputs = [gr.Label(num_top_classes=5)]
|
| 52 |
+
|
| 53 |
+
examples = [
|
| 54 |
+
[30, "M", "HIGH", "NORMAL", 15.4],
|
| 55 |
+
[35, "F", "LOW", "NORMAL", 8],
|
| 56 |
+
[50, "M", "HIGH", "HIGH", 34],
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
title = "Drug Classification"
|
| 61 |
+
description = "Enter the details to correctly identify Drug type?"
|
| 62 |
+
article = "A Beginners Guide to CI/CD for Machine Learning. It teaches how to automate training, evaluation, and deployment of models to Hugging Face using GitHub Actions."
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
gr.Interface(
|
| 66 |
+
fn=predict_drug,
|
| 67 |
+
inputs=inputs,
|
| 68 |
+
outputs=outputs,
|
| 69 |
+
examples=examples,
|
| 70 |
+
title=title,
|
| 71 |
+
description=description,
|
| 72 |
+
article=article,
|
| 73 |
+
theme=gr.themes.Soft(),
|
| 74 |
+
).launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
scikit-learn
|
| 2 |
-
skops
|
|
|
|
|
|
| 1 |
scikit-learn
|
| 2 |
+
skops
|
| 3 |
+
gradio
|