Spaces:
Sleeping
Sleeping
Sync App files
Browse files- README.md +9 -6
- drug_app.py +256 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
---
|
| 2 |
title: Drug Classification
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Drug Classification
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.16.0
|
| 8 |
+
app_file: drug_app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Drug Classification App
|
| 14 |
+
|
| 15 |
+
A machine learning web app that predicts drug type based on patient features such as age, sex, blood pressure, cholesterol, and sodium-to-potassium ratio.
|
drug_app.py
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import skops.io as sio
|
| 3 |
+
|
| 4 |
+
untrusted = sio.get_untrusted_types(file="./Model/drug_pipeline.skops")
|
| 5 |
+
pipe = sio.load("./Model/drug_pipeline.skops", trusted=untrusted)
|
| 6 |
+
|
| 7 |
+
DRUG_INFO = {
|
| 8 |
+
"DrugY": ("DrugY", "#00C9A7", "#005C4B", "Typically prescribed for patients with high Na_to_K ratio (>14)."),
|
| 9 |
+
"drugA": ("Drug A", "#845EC2", "#3B1F6E", "Often used for older patients with HIGH blood pressure and HIGH cholesterol."),
|
| 10 |
+
"drugB": ("Drug B", "#FF6F91", "#7A1A35", "Prescribed for older patients with HIGH blood pressure and NORMAL cholesterol."),
|
| 11 |
+
"drugC": ("Drug C", "#FFC75F", "#7A5500", "Used for younger patients with LOW blood pressure."),
|
| 12 |
+
"drugX": ("Drug X", "#0089BA", "#003F5A", "Prescribed for patients with NORMAL blood pressure and low Na_to_K ratio."),
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def predict_drug(age, sex, blood_pressure, cholesterol, na_to_k_ratio):
|
| 17 |
+
if sex is None or blood_pressure is None or cholesterol is None:
|
| 18 |
+
return (
|
| 19 |
+
gr.update(value="""
|
| 20 |
+
<div style="text-align:center; padding:30px; color:#FF6F91; font-size:14px; font-weight:600;
|
| 21 |
+
background:rgba(255,111,145,0.08); border-radius:12px; border:1px dashed #FF6F91;">
|
| 22 |
+
β οΈ Please fill in all fields before predicting.
|
| 23 |
+
</div>""", visible=True),
|
| 24 |
+
gr.update(visible=False),
|
| 25 |
+
gr.update(visible=False),
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
features = [age, sex, blood_pressure, cholesterol, na_to_k_ratio]
|
| 29 |
+
predicted = pipe.predict([features])[0]
|
| 30 |
+
name, color, dark, info = DRUG_INFO.get(predicted, (predicted, "#845EC2", "#3B1F6E", ""))
|
| 31 |
+
|
| 32 |
+
result_html = f"""
|
| 33 |
+
<div style="
|
| 34 |
+
background: linear-gradient(135deg, {color}33 0%, {color}11 100%);
|
| 35 |
+
border: 1.5px solid {color}66;
|
| 36 |
+
border-radius: 16px;
|
| 37 |
+
padding: 24px 28px;
|
| 38 |
+
margin-top: 8px;
|
| 39 |
+
position: relative;
|
| 40 |
+
overflow: hidden;
|
| 41 |
+
">
|
| 42 |
+
<div style="
|
| 43 |
+
position:absolute; top:-20px; right:-20px;
|
| 44 |
+
width:100px; height:100px;
|
| 45 |
+
background: radial-gradient(circle, {color}44, transparent 70%);
|
| 46 |
+
border-radius: 50%;
|
| 47 |
+
"></div>
|
| 48 |
+
<div style="font-size:11px; color:{color}; text-transform:uppercase; letter-spacing:2px; font-weight:700; margin-bottom:6px;">
|
| 49 |
+
Recommended Drug
|
| 50 |
+
</div>
|
| 51 |
+
<div style="font-size:38px; font-weight:800; color:{color}; line-height:1.1; margin-bottom:10px;">
|
| 52 |
+
{name}
|
| 53 |
+
</div>
|
| 54 |
+
<div style="
|
| 55 |
+
display:inline-block; background:{color}22; color:{dark};
|
| 56 |
+
border-radius:20px; padding:4px 12px; font-size:12px; font-weight:600;
|
| 57 |
+
border: 1px solid {color}55;
|
| 58 |
+
">Clinical Note</div>
|
| 59 |
+
<div style="font-size:13px; color:#555; margin-top:10px; line-height:1.6;">{info}</div>
|
| 60 |
+
</div>
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
summary_html = f"""
|
| 64 |
+
<div style="
|
| 65 |
+
background: linear-gradient(90deg, #1a1a2e 0%, #16213e 100%);
|
| 66 |
+
border-radius: 12px; padding: 16px 20px; margin-top: 12px;
|
| 67 |
+
font-size: 13px;
|
| 68 |
+
">
|
| 69 |
+
<div style="color:#aaa; font-size:11px; text-transform:uppercase; letter-spacing:1.5px; margin-bottom:10px; font-weight:600;">
|
| 70 |
+
Patient Summary
|
| 71 |
+
</div>
|
| 72 |
+
<div style="display:flex; flex-wrap:wrap; gap:10px;">
|
| 73 |
+
<span style="background:#ffffff15; color:#e0e0e0; padding:4px 12px; border-radius:20px;">Age: <b style="color:white">{age}</b></span>
|
| 74 |
+
<span style="background:#ffffff15; color:#e0e0e0; padding:4px 12px; border-radius:20px;">Sex: <b style="color:white">{sex}</b></span>
|
| 75 |
+
<span style="background:#ffffff15; color:#e0e0e0; padding:4px 12px; border-radius:20px;">BP: <b style="color:white">{blood_pressure}</b></span>
|
| 76 |
+
<span style="background:#ffffff15; color:#e0e0e0; padding:4px 12px; border-radius:20px;">Cholesterol: <b style="color:white">{cholesterol}</b></span>
|
| 77 |
+
<span style="background:#ffffff15; color:#e0e0e0; padding:4px 12px; border-radius:20px;">Na/K: <b style="color:white">{na_to_k_ratio}</b></span>
|
| 78 |
+
</div>
|
| 79 |
+
</div>
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
return (
|
| 83 |
+
gr.update(value=result_html, visible=True),
|
| 84 |
+
gr.update(value=summary_html, visible=True),
|
| 85 |
+
gr.update(visible=True),
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
CSS = """
|
| 90 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap');
|
| 91 |
+
|
| 92 |
+
* { font-family: 'Inter', sans-serif !important; }
|
| 93 |
+
|
| 94 |
+
body, .gradio-container {
|
| 95 |
+
background: linear-gradient(135deg, #0f0c29, #302b63, #24243e) !important;
|
| 96 |
+
min-height: 100vh;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
.card {
|
| 100 |
+
background: rgba(255,255,255,0.04) !important;
|
| 101 |
+
border: 1px solid rgba(255,255,255,0.1) !important;
|
| 102 |
+
border-radius: 20px !important;
|
| 103 |
+
padding: 28px !important;
|
| 104 |
+
backdrop-filter: blur(12px) !important;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
/* Slider track */
|
| 108 |
+
input[type=range] { accent-color: #845EC2; }
|
| 109 |
+
|
| 110 |
+
/* Radio buttons */
|
| 111 |
+
.gr-radio label { color: #ccc !important; }
|
| 112 |
+
|
| 113 |
+
/* Labels */
|
| 114 |
+
label span { color: #ccc !important; }
|
| 115 |
+
|
| 116 |
+
/* Buttons */
|
| 117 |
+
.lg.primary {
|
| 118 |
+
background: linear-gradient(135deg, #845EC2, #FF6F91) !important;
|
| 119 |
+
border: none !important;
|
| 120 |
+
border-radius: 12px !important;
|
| 121 |
+
font-weight: 700 !important;
|
| 122 |
+
letter-spacing: 0.5px !important;
|
| 123 |
+
box-shadow: 0 4px 20px rgba(132,94,194,0.4) !important;
|
| 124 |
+
transition: transform 0.15s, box-shadow 0.15s !important;
|
| 125 |
+
}
|
| 126 |
+
.lg.primary:hover {
|
| 127 |
+
transform: translateY(-2px) !important;
|
| 128 |
+
box-shadow: 0 8px 28px rgba(132,94,194,0.55) !important;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
.secondary {
|
| 132 |
+
background: rgba(255,255,255,0.07) !important;
|
| 133 |
+
border: 1px solid rgba(255,255,255,0.15) !important;
|
| 134 |
+
color: #ccc !important;
|
| 135 |
+
border-radius: 20px !important;
|
| 136 |
+
font-size: 12px !important;
|
| 137 |
+
}
|
| 138 |
+
.secondary:hover {
|
| 139 |
+
background: rgba(255,255,255,0.13) !important;
|
| 140 |
+
color: white !important;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
footer { display: none !important; }
|
| 144 |
+
|
| 145 |
+
.divider { border: none; border-top: 1px solid rgba(255,255,255,0.08); margin: 12px 0 24px; }
|
| 146 |
+
"""
|
| 147 |
+
|
| 148 |
+
with gr.Blocks(title="Drug Classification") as demo:
|
| 149 |
+
|
| 150 |
+
# ββ Header ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 151 |
+
gr.HTML("""
|
| 152 |
+
<div style="text-align:center; padding: 32px 0 16px;">
|
| 153 |
+
<div style="
|
| 154 |
+
display:inline-flex; align-items:center; justify-content:center;
|
| 155 |
+
width:64px; height:64px; border-radius:18px; font-size:30px;
|
| 156 |
+
background: linear-gradient(135deg, #845EC2, #FF6F91);
|
| 157 |
+
box-shadow: 0 8px 24px rgba(132,94,194,0.5);
|
| 158 |
+
margin-bottom:16px;
|
| 159 |
+
">π</div>
|
| 160 |
+
<h1 style="margin:0; font-size:28px; font-weight:800;
|
| 161 |
+
background: linear-gradient(90deg, #845EC2, #FF6F91, #FFC75F);
|
| 162 |
+
-webkit-background-clip:text; -webkit-text-fill-color:transparent;">
|
| 163 |
+
Drug Classification
|
| 164 |
+
</h1>
|
| 165 |
+
<p style="margin:10px 0 0; color:rgba(255,255,255,0.45); font-size:14px;">
|
| 166 |
+
AI-powered drug recommendation based on patient vitals
|
| 167 |
+
</p>
|
| 168 |
+
</div>
|
| 169 |
+
""")
|
| 170 |
+
|
| 171 |
+
gr.HTML("<hr class='divider'>")
|
| 172 |
+
|
| 173 |
+
with gr.Row(equal_height=True):
|
| 174 |
+
|
| 175 |
+
# ββ Left panel βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 176 |
+
with gr.Column(scale=1, elem_classes="card"):
|
| 177 |
+
gr.HTML("<div style='font-size:16px; font-weight:700; color:white; margin-bottom:18px;'>Patient Information</div>")
|
| 178 |
+
|
| 179 |
+
age = gr.Slider(
|
| 180 |
+
minimum=15, maximum=74, step=1, value=35,
|
| 181 |
+
label="Age", info="Range: 15 β 74 years"
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
with gr.Row():
|
| 185 |
+
sex = gr.Radio(["M", "F"], label="Sex", value=None)
|
| 186 |
+
cholesterol = gr.Radio(["HIGH", "NORMAL"], label="Cholesterol", value=None)
|
| 187 |
+
|
| 188 |
+
blood_pressure = gr.Radio(["HIGH", "LOW", "NORMAL"], label="Blood Pressure", value=None)
|
| 189 |
+
|
| 190 |
+
na_to_k = gr.Slider(
|
| 191 |
+
minimum=6.2, maximum=38.2, step=0.1, value=15.0,
|
| 192 |
+
label="Na / K Ratio", info="Sodium-to-potassium ratio in blood"
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
predict_btn = gr.Button("Predict Drug", variant="primary", size="lg")
|
| 196 |
+
|
| 197 |
+
gr.HTML("<div style='font-size:12px; color:rgba(255,255,255,0.35); margin:16px 0 8px; text-transform:uppercase; letter-spacing:1px;'>Quick Examples</div>")
|
| 198 |
+
with gr.Row():
|
| 199 |
+
ex1 = gr.Button("High BP", size="sm", variant="secondary")
|
| 200 |
+
ex2 = gr.Button("Low BP", size="sm", variant="secondary")
|
| 201 |
+
ex3 = gr.Button("High Na/K", size="sm", variant="secondary")
|
| 202 |
+
|
| 203 |
+
# ββ Right panel ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 204 |
+
with gr.Column(scale=1, elem_classes="card"):
|
| 205 |
+
gr.HTML("<div style='font-size:16px; font-weight:700; color:white; margin-bottom:18px;'>Prediction Result</div>")
|
| 206 |
+
|
| 207 |
+
result_box = gr.HTML(
|
| 208 |
+
value="""
|
| 209 |
+
<div style="
|
| 210 |
+
text-align:center; padding:50px 20px;
|
| 211 |
+
color:rgba(255,255,255,0.2); font-size:13px;
|
| 212 |
+
border: 1px dashed rgba(255,255,255,0.1);
|
| 213 |
+
border-radius:14px;
|
| 214 |
+
background: rgba(255,255,255,0.02);
|
| 215 |
+
">
|
| 216 |
+
Fill in patient details and click<br>
|
| 217 |
+
<b style="color:rgba(255,255,255,0.35);">Predict Drug</b>
|
| 218 |
+
</div>""",
|
| 219 |
+
)
|
| 220 |
+
summary_box = gr.HTML(visible=False)
|
| 221 |
+
disclaimer = gr.HTML(
|
| 222 |
+
value="""
|
| 223 |
+
<div style="font-size:11px; color:rgba(255,255,255,0.3); margin-top:14px;
|
| 224 |
+
padding:10px 14px; background:rgba(255,255,255,0.03);
|
| 225 |
+
border-radius:8px; border:1px solid rgba(255,255,255,0.07);">
|
| 226 |
+
β οΈ For educational purposes only. Always consult a licensed physician.
|
| 227 |
+
</div>""",
|
| 228 |
+
visible=False,
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
# ββ Footer βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 232 |
+
gr.HTML("""
|
| 233 |
+
<div style="text-align:center; margin-top:28px; font-size:11px; color:rgba(255,255,255,0.2);">
|
| 234 |
+
Built with Gradio Β· CI/CD for Machine Learning Β· Powered by scikit-learn
|
| 235 |
+
</div>
|
| 236 |
+
""")
|
| 237 |
+
|
| 238 |
+
# ββ Handlers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 239 |
+
predict_btn.click(
|
| 240 |
+
fn=predict_drug,
|
| 241 |
+
inputs=[age, sex, blood_pressure, cholesterol, na_to_k],
|
| 242 |
+
outputs=[result_box, summary_box, disclaimer],
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
ex1.click(fn=lambda: (50, "M", "HIGH", "HIGH", 10.2), outputs=[age, sex, blood_pressure, cholesterol, na_to_k])
|
| 246 |
+
ex2.click(fn=lambda: (35, "F", "LOW", "NORMAL", 8.0), outputs=[age, sex, blood_pressure, cholesterol, na_to_k])
|
| 247 |
+
ex3.click(fn=lambda: (28, "M", "NORMAL", "HIGH", 30.5), outputs=[age, sex, blood_pressure, cholesterol, na_to_k])
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
demo.launch(
|
| 251 |
+
theme=gr.themes.Base(
|
| 252 |
+
primary_hue="purple",
|
| 253 |
+
font=[gr.themes.GoogleFont("Inter"), "sans-serif"],
|
| 254 |
+
),
|
| 255 |
+
css=CSS,
|
| 256 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|
| 2 |
+
skops
|
| 3 |
+
gradio
|