Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib
|
3 |
+
matplotlib.use("Agg")
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
import seaborn as sns
|
8 |
+
|
9 |
+
# Dummy Cox Proportional Hazard Prediction Function
|
10 |
+
def hazard_predictor(gender, age, bmi, diabetes_type, spb, dbp, hba1c, smoking, marital_status):
|
11 |
+
# Dummy hazard function with a limit of 0.95 (95%)
|
12 |
+
hazard_ratio = min((age / 50) * (bmi / 25) * (spb / 120) * (dbp / 80), 0.95)
|
13 |
+
return {'Hazard Ratio': hazard_ratio}
|
14 |
+
|
15 |
+
def plot_chart(hazard_ratio, age):
|
16 |
+
age_range = np.linspace(18, 100, 500)
|
17 |
+
hazard_ratio_line = []
|
18 |
+
|
19 |
+
for a in age_range:
|
20 |
+
if a <= age:
|
21 |
+
hazard_ratio_line.append(a / age * hazard_ratio)
|
22 |
+
else:
|
23 |
+
hazard_ratio_line.append(hazard_ratio)
|
24 |
+
|
25 |
+
# Create a dataframe for Seaborn plotting
|
26 |
+
data = {'Age': age_range, 'Hazard Ratio': hazard_ratio_line}
|
27 |
+
df = pd.DataFrame(data)
|
28 |
+
|
29 |
+
# Create the Seaborn line plot
|
30 |
+
sns_plot = sns.lineplot(x="Age", y="Hazard Ratio", data=df)
|
31 |
+
|
32 |
+
# Set plot labels and limits
|
33 |
+
sns_plot.set(xlabel='Age', ylabel='Hazard Ratio', ylim=(0, 1))
|
34 |
+
|
35 |
+
# Save the plot as an image
|
36 |
+
chart_filepath = "chart.png"
|
37 |
+
plt.savefig(chart_filepath, dpi=150)
|
38 |
+
plt.close() # Close the plot to free up the memory
|
39 |
+
return chart_filepath
|
40 |
+
|
41 |
+
def output_function(gender, age, bmi, diabetes_type, spb, dbp, hba1c, smoking, marital_status):
|
42 |
+
hazard_ratio = hazard_predictor(gender, age, bmi, diabetes_type, spb, dbp, hba1c, smoking, marital_status)['Hazard Ratio']
|
43 |
+
output_label = f"Hazard Ratio: {hazard_ratio}"
|
44 |
+
chart_filepath = plot_chart(hazard_ratio, age)
|
45 |
+
return output_label, chart_filepath
|
46 |
+
|
47 |
+
# Save the chart as an image
|
48 |
+
chart_filepath = "chart.png"
|
49 |
+
chart.savefig(chart_filepath, dpi=150)
|
50 |
+
|
51 |
+
return output_label, chart_filepath
|
52 |
+
|
53 |
+
|
54 |
+
# Gradio user interface components
|
55 |
+
gender_radio = gr.inputs.Radio(choices=['Female', 'Male'], label="Gender")
|
56 |
+
|
57 |
+
age_slider = gr.inputs.Slider(minimum=18, maximum=100, step=1, default=50, label="Age")
|
58 |
+
|
59 |
+
bmi_slider = gr.inputs.Slider(minimum=15, maximum=50, step=0.1, default=25, label="BMI")
|
60 |
+
|
61 |
+
diabetes_type_radio = gr.inputs.Radio(choices=['Type 1', 'Type 2', 'Gestational'], label="Diabetes Type")
|
62 |
+
|
63 |
+
spb_slider = gr.inputs.Slider(minimum=90, maximum=200, step=1, default=120, label="Systolic Blood Pressure (SPB)")
|
64 |
+
|
65 |
+
dbp_slider = gr.inputs.Slider(minimum=60, maximum=120, step=1, default=80, label="Diastolic Blood Pressure (DBP)")
|
66 |
+
|
67 |
+
hba1c_slider = gr.inputs.Slider(minimum=100, maximum=400, step=1, default=200, label="Hemoglobin A1c value (mg/dL)")
|
68 |
+
|
69 |
+
smoking_radio = gr.inputs.Radio(choices=['Non-smoker', 'Smoker'], label="Smoking History")
|
70 |
+
|
71 |
+
marital_status_dropdown = gr.inputs.Dropdown(choices=['Single', 'Married', 'Widowed', 'Divorced'], label="Marital Status")
|
72 |
+
|
73 |
+
output_text = gr.outputs.Textbox(label="Hazard Ratio")
|
74 |
+
output_chart = gr.outputs.Image(type='filepath')
|
75 |
+
|
76 |
+
# Creating Gradio interface
|
77 |
+
interface = gr.Interface(fn=output_function,
|
78 |
+
inputs=[gender_radio, age_slider, bmi_slider, diabetes_type_radio, spb_slider, dbp_slider, hba1c_slider, smoking_radio, marital_status_dropdown],
|
79 |
+
outputs=[output_text, output_chart],
|
80 |
+
title="Diabetes Cox Proportional Hazard Predictor",
|
81 |
+
submit_button="Predict")
|
82 |
+
|
83 |
+
interface.launch(debug=True)
|