import gradio as gr import pickle import pandas as pd # Load the saved model (from the Colab notebook) with open("/content/logistic_model_pipeline.pkl2", "rb") as f: model = pickle.load(f) # Define the prediction function (from the Colab notebook) def predict_heart_disease(age, sex_male, cigs_per_day, tot_chol, sys_bp, glucose): # Create a DataFrame for the input input_data = pd.DataFrame({ 'age': [age], 'Sex_male': [sex_male], 'cigsPerDay': [cigs_per_day], 'totChol': [tot_chol], 'sysBP': [sys_bp], 'glucose': [glucose] }) # Convert DataFrame to NumPy array input_data_array = input_data.values # Make prediction using the loaded model (replace with your model's prediction logic) prob = model.predict_proba(input_data_array)[0, 1] # Assuming a probability prediction return f"The probability of having heart disease is: {prob * 100:.2f}%" # Create the Gradio interface with gr.Blocks() as interface: gr.Markdown("## Heart Disease Risk Prediction") gr.Markdown("Enter your details below to check the probability of having heart disease.") # Age input with helper text age = gr.Slider(label="Age", minimum=20, maximum=100, value=50, step=1, info="Enter your age (20-100).") # Sex input with helper text sex_male = gr.Radio(label="Sex", choices=[1, 0], value=1, info="Select your sex: 1 for Male, 0 for Female.") # Cigarettes per day input with helper text cigs_per_day = gr.Slider(label="Cigarettes per Day", minimum=0, maximum=60, value=10, step=1, info="Enter the number of cigarettes you smoke per day (0-60).") # Total cholesterol input with helper text tot_chol = gr.Slider(label="Total Cholesterol (mg/dL)", minimum=100, maximum=400, value=200, step=1, info="Enter your total cholesterol level (100-400 mg/dL).") # Systolic blood pressure input with helper text sys_bp = gr.Slider(label="Systolic Blood Pressure (mmHg)", minimum=90, maximum=200, value=120, step=1, info="Enter your systolic blood pressure (90-200 mmHg).") # Glucose input with helper text glucose = gr.Slider(label="Glucose Level (mg/dL)", minimum=50, maximum=300, value=100, step=1, info="Enter your glucose level (50-300 mg/dL).") # Predict button and output predict_btn = gr.Button("Predict") output = gr.Textbox(label="Prediction") # Set up the prediction process predict_btn.click(predict_heart_disease, inputs=[age, sex_male, cigs_per_day, tot_chol, sys_bp, glucose], outputs=output) # Launch the interface interface.launch(share=True)