import gradio as gr import numpy as np import joblib # Load the model and scaler model = joblib.load('logistic_regression_model.pkl') scaler = joblib.load('scaler.pkl') def initial_risk_check(features): # Scale and predict risk based on the features features_scaled = scaler.transform([features]) prediction = model.predict_proba(features_scaled)[:, 1] # Define risk thresholds high_risk_threshold = 0.75 moderate_risk_threshold = 0.5 # Determine risk level and recommendation if prediction >= high_risk_threshold: return "High risk. Please consult a doctor immediately.", prediction elif prediction >= moderate_risk_threshold: return "Moderate risk. Consider lifestyle changes and monitor your health closely.", prediction else: return "Low risk. Maintain a healthy lifestyle and schedule routine check-ups.", prediction def launch_interface(): with gr.Blocks(css=""" .gradio-container { font-family: Arial, sans-serif; background-color: #f9f9f9; color: #333; } .gr-button { background-color: #007bff; color: white; } .gr-button:hover { background-color: #0056b3; } .gr-textbox { margin-bottom: 1em; } """) as app: gr.Markdown(""" # Gestational Diabetes Risk Calculator This calculator helps you assess your risk for gestational diabetes. Enter your details below and get an initial risk assessment. If you are above medium risk, please provide more detailed information for a more accurate assessment. """) with gr.Row(): pregnancies = gr.Number(label="Number of Pregnancies") age = gr.Number(label="Age") bmi = gr.Number(label="BMI") detailed = gr.Checkbox(label="Click if above medium risk for more detailed input", value=False) with gr.Row(): ogtt = gr.Number(label="OGTT", visible=False) sys_bp = gr.Number(label="Systolic Blood Pressure", visible=False) hdl = gr.Number(label="HDL", visible=False) # Assuming HDL is used for DPF submit_button = gr.Button("Submit") output = gr.Textbox(label="Result") def update_output(pregnancies, age, bmi, detailed, ogtt, sys_bp, hdl): if detailed: features = [pregnancies, ogtt, sys_bp, bmi, hdl, age] else: features = [pregnancies, 120, 120, bmi, 0.5, age] # Default values for detailed inputs result, prediction = initial_risk_check(features) return result def toggle_visibility(detailed): return { ogtt: gr.update(visible=detailed), sys_bp: gr.update(visible=detailed), hdl: gr.update(visible=detailed) } detailed.change( toggle_visibility, inputs=[detailed], outputs=[ogtt, sys_bp, hdl] ) submit_button.click( update_output, inputs=[pregnancies, age, bmi, detailed, ogtt, sys_bp, hdl], outputs=output ) app.launch() if __name__ == "__main__": launch_interface()