Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load the trained model
|
| 5 |
+
with open("model.pkl", "rb") as f:
|
| 6 |
+
model = pickle.load(f)
|
| 7 |
+
|
| 8 |
+
# Mapping
|
| 9 |
+
diagnoses = {
|
| 10 |
+
0: "Negative",
|
| 11 |
+
1: "Hypothyroid",
|
| 12 |
+
2: "Hyperthyroid"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
# Preprocess inputs
|
| 16 |
+
def preprocess_inputs(age, sex, on_thyroxine, query_on_thyroxine, on_antithyroid_meds, sick, pregnant,
|
| 17 |
+
thyroid_surgery, I131_treatment, query_hypothyroid, query_hyperthyroid, lithium,
|
| 18 |
+
goitre, tumor, hypopituitary, psych, TSH, T3, TT4, T4U, FTI):
|
| 19 |
+
|
| 20 |
+
binary_map = {'Yes': 1, 'No': 0, '': None}
|
| 21 |
+
|
| 22 |
+
on_thyroxine = binary_map[on_thyroxine]
|
| 23 |
+
query_on_thyroxine = binary_map[query_on_thyroxine]
|
| 24 |
+
on_antithyroid_meds = binary_map[on_antithyroid_meds]
|
| 25 |
+
sick = binary_map[sick]
|
| 26 |
+
pregnant = binary_map[pregnant]
|
| 27 |
+
thyroid_surgery = binary_map[thyroid_surgery]
|
| 28 |
+
I131_treatment = binary_map[I131_treatment]
|
| 29 |
+
query_hypothyroid = binary_map[query_hypothyroid]
|
| 30 |
+
query_hyperthyroid = binary_map[query_hyperthyroid]
|
| 31 |
+
lithium = binary_map[lithium]
|
| 32 |
+
goitre = binary_map[goitre]
|
| 33 |
+
tumor = binary_map[tumor]
|
| 34 |
+
hypopituitary = binary_map[hypopituitary]
|
| 35 |
+
psych = binary_map[psych]
|
| 36 |
+
|
| 37 |
+
sex = 1 if sex == 'F' else 0 if sex == 'M' else None
|
| 38 |
+
|
| 39 |
+
return [age, sex, on_thyroxine, query_on_thyroxine, on_antithyroid_meds, sick, pregnant,
|
| 40 |
+
thyroid_surgery, I131_treatment, query_hypothyroid, query_hyperthyroid, lithium,
|
| 41 |
+
goitre, tumor, hypopituitary, psych, TSH, T3, TT4, T4U, FTI]
|
| 42 |
+
|
| 43 |
+
# Predict function
|
| 44 |
+
def predict_diagnosis(age, sex, on_thyroxine, query_on_thyroxine, on_antithyroid_meds, sick, pregnant,
|
| 45 |
+
thyroid_surgery, I131_treatment, query_hypothyroid, query_hyperthyroid, lithium,
|
| 46 |
+
goitre, tumor, hypopituitary, psych, TSH, T3, TT4, T4U, FTI):
|
| 47 |
+
|
| 48 |
+
inputs = preprocess_inputs(age, sex, on_thyroxine, query_on_thyroxine, on_antithyroid_meds, sick,
|
| 49 |
+
pregnant, thyroid_surgery, I131_treatment, query_hypothyroid,
|
| 50 |
+
query_hyperthyroid, lithium, goitre, tumor, hypopituitary, psych,
|
| 51 |
+
TSH, T3, TT4, T4U, FTI)
|
| 52 |
+
|
| 53 |
+
output = model.predict([inputs])[0]
|
| 54 |
+
return diagnoses.get(output, "Unknown")
|
| 55 |
+
|
| 56 |
+
# Define Gradio interface
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
+
fn=predict_diagnosis,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Number(label="Age"),
|
| 61 |
+
gr.Radio(["M", "F"], label="Sex"),
|
| 62 |
+
gr.Radio(["Yes", "No"], label="On Thyroxine"),
|
| 63 |
+
gr.Radio(["Yes", "No"], label="Query on Thyroxine"),
|
| 64 |
+
gr.Radio(["Yes", "No"], label="On Antithyroid Meds"),
|
| 65 |
+
gr.Radio(["Yes", "No"], label="Sick"),
|
| 66 |
+
gr.Radio(["Yes", "No"], label="Pregnant"),
|
| 67 |
+
gr.Radio(["Yes", "No"], label="Thyroid Surgery"),
|
| 68 |
+
gr.Radio(["Yes", "No"], label="I131 Treatment"),
|
| 69 |
+
gr.Radio(["Yes", "No"], label="Query Hypothyroid"),
|
| 70 |
+
gr.Radio(["Yes", "No"], label="Query Hyperthyroid"),
|
| 71 |
+
gr.Radio(["Yes", "No"], label="Lithium"),
|
| 72 |
+
gr.Radio(["Yes", "No"], label="Goitre"),
|
| 73 |
+
gr.Radio(["Yes", "No"], label="Tumor"),
|
| 74 |
+
gr.Radio(["Yes", "No"], label="Hypopituitary"),
|
| 75 |
+
gr.Radio(["Yes", "No"], label="Psych"),
|
| 76 |
+
gr.Number(label="TSH"),
|
| 77 |
+
gr.Number(label="T3"),
|
| 78 |
+
gr.Number(label="TT4"),
|
| 79 |
+
gr.Number(label="T4U"),
|
| 80 |
+
gr.Number(label="FTI"),
|
| 81 |
+
],
|
| 82 |
+
outputs="text",
|
| 83 |
+
title="Thyroid Diagnosis Predictor",
|
| 84 |
+
description="Predict whether a patient has Hypothyroid, Hyperthyroid or is Negative using medical features."
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# Launch app
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
demo.launch()
|