Diabetees_coxPH / app.py
netrosec's picture
Update app.py
a262331
raw
history blame contribute delete
No virus
3.29 kB
import gradio as gr
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
def hazard_predictor(gender, age, bmi, diabetes_type, spb, dbp, hba1c, smoking, marital_status):
# Dummy hazard function with a limit of 0.95 (95%)
hazard_ratio = min((age / 50) * (bmi / 25) * (spb / 120) * (dbp / 80), 0.92)
return {'Hazard Ratio': hazard_ratio}
def plot_chart(hazard_ratio, age):
age_range = np.linspace(18, 100, 500)
hazard_ratio_line = []
for a in age_range:
if a <= age:
hazard_ratio_line.append(a / age * hazard_ratio)
else:
hazard_ratio_line.append(hazard_ratio)
# Create a dataframe for Seaborn plotting
data = {'Age': age_range, 'Hazard Ratio': hazard_ratio_line}
df = pd.DataFrame(data)
# Create the Seaborn line plot
sns_plot = sns.lineplot(x="Age", y="Hazard Ratio", data=df)
# Set plot labels and limits
sns_plot.set(xlabel='Age', ylabel='Hazard Ratio', ylim=(0, 1))
# Save the plot as an image
chart_filepath = "chart.png"
plt.savefig(chart_filepath, dpi=150)
plt.close() # Close the plot to free up the memory
return chart_filepath
def output_function(gender, age, bmi, diabetes_type, spb, dbp, hba1c, smoking, marital_status):
hazard_ratio = hazard_predictor(gender, age, bmi, diabetes_type, spb, dbp, hba1c, smoking, marital_status)['Hazard Ratio']
output_label = f"Hazard Ratio: {hazard_ratio}"
chart_filepath = plot_chart(hazard_ratio, age)
return output_label, chart_filepath
# Save the chart as an image
chart_filepath = "chart.png"
chart.savefig(chart_filepath, dpi=150)
return output_label, chart_filepath
# Gradio user interface components
gender_radio = gr.inputs.Radio(choices=['Female', 'Male'], label="Gender")
age_slider = gr.inputs.Slider(minimum=18, maximum=100, step=1, default=50, label="Age")
bmi_slider = gr.inputs.Slider(minimum=15, maximum=50, step=0.1, default=25, label="BMI")
diabetes_type_radio = gr.inputs.Radio(choices=['Type 1', 'Type 2', 'Gestational'], label="Diabetes Type")
spb_slider = gr.inputs.Slider(minimum=90, maximum=200, step=1, default=120, label="Systolic Blood Pressure (SPB)")
dbp_slider = gr.inputs.Slider(minimum=60, maximum=120, step=1, default=80, label="Diastolic Blood Pressure (DBP)")
hba1c_slider = gr.inputs.Slider(minimum=100, maximum=400, step=1, default=200, label="Hemoglobin A1c value (mg/dL)")
smoking_radio = gr.inputs.Radio(choices=['Non-smoker', 'Smoker'], label="Smoking History")
marital_status_dropdown = gr.inputs.Dropdown(choices=['Single', 'Married', 'Widowed', 'Divorced'], label="Marital Status")
output_text = gr.outputs.Textbox(label="Hazard Ratio")
output_chart = gr.outputs.Image(type='filepath')
# Creating Gradio interface
interface = gr.Interface(fn=output_function,
inputs=[gender_radio, age_slider, bmi_slider, diabetes_type_radio, spb_slider, dbp_slider, hba1c_slider, smoking_radio, marital_status_dropdown],
outputs=[output_text, output_chart],
title="Diabetes Cox Proportional Hazard Predictor",
submit_button="Predict")
interface.launch(debug=True)