import tensorflow as tf import gradio as gr import numpy as np # Load the trained model model = tf.keras.models.load_model('sleep_disorder_model.h5') # Define the prediction function def predict_sleep_disorder(gender, age, sleep_duration, quality_of_sleep, physical_activity_level, stress_level, heart_rate, daily_steps, systolic, diastolic, occupation, bmi_category): # Gender encoding gender_male = 1 if gender == "Male" else 0 # Occupation encoding occupation_dict = { "Doctor": 0, "Engineer": 1, "Lawyer": 2, "Manager": 3, "Nurse": 4, "SalesRepresentative": 5, "Salesperson": 6, "Scientist": 7, "SoftwareEngineer": 8, "Teacher": 9 } occupation_encoded = [0] * 10 # Initialize a list of 10 zeros if occupation in occupation_dict: occupation_encoded[occupation_dict[occupation]] = 1 # Set the correct occupation to 1 # BMI Category encoding bmi_dict = { "NormalWeight": 0, "Obese": 1, "Overweight": 2 } bmi_encoded = [0, 0, 0] # Initialize a list of 3 zeros if bmi_category in bmi_dict: bmi_encoded[bmi_dict[bmi_category]] = 1 # Set the correct BMI category to 1 # Combine all inputs into a single array input_data = np.array([[ age, sleep_duration, quality_of_sleep, physical_activity_level, stress_level, heart_rate, daily_steps, systolic, diastolic, gender_male ] + occupation_encoded + bmi_encoded]) # Use the model to make a prediction prediction = model.predict(input_data) # Convert the prediction to a readable format prediction_index = np.argmax(prediction, axis=1)[0] # Get the index of the highest probability prediction_confidence = prediction[0][prediction_index] * 100 prediction_mapping = { 0: "No Sleep Disorder", 1: "Insomnia", 2: "Sleep Apnea" } result = prediction_mapping.get(prediction_index, "Unknown") if result in ["Insomnia", "Sleep Apnea"]: message = gr.Markdown("# Did you know?\n ## Here are some facts related to sleep disorders:") image = "sleep_problems_table.png" source_message = ( "Source: [AIHW - Sleep problems as a risk factor]" "(https://www.aihw.gov.au/reports/risk-factors/sleep-problems-as-a-risk-factor/summary)" ) else: image = "starrysky.jpg" message = gr.Markdown("# Have a good night's sleep!") source_message = ("" ) confidence_message = f"**Model Confidence: {prediction_confidence:.2f}%**" return result, confidence_message, message, image, source_message def welcome(name): if not name.strip(): return"" return f"Nice to meet you, {name}! Let's begin." js = """ function createGradioAnimation() { var container = document.createElement('div'); container.id = 'gradio-animation'; container.style.fontSize = '2em'; container.style.fontWeight = 'bold'; container.style.textAlign = 'left'; container.style.marginBottom = '20px'; var text = 'Welcome to the Sleep Disorder Prediction App'; for (var i = 0; i < text.length; i++) { (function(i){ setTimeout(function(){ var letter = document.createElement('span'); letter.style.opacity = '0'; letter.style.transition = 'opacity 0.2s'; letter.innerText = text[i]; container.appendChild(letter); setTimeout(function() { letter.style.opacity = '1'; }, 30); }, i * 30 ); })(i); } var gradioContainer = document.querySelector('.gradio-container'); gradioContainer.insertBefore(container, gradioContainer.firstChild); return 'Animation created'; } """ # Build the Gradio interface with gr.Blocks(js=js) as demo: gr.Markdown(""" This app predicts the risk of developing sleep disorders based on your responses to a series of questions. Please fill in the details below to get started.
*This project was built as an educational exercise during UWA's Data Analytics Bootcamp program. It was developed under a cooperative effort of Group 3 members: Daniel Usuga, Bradley Curthoys, Lisa Shimano, and Bailey Strauch.* """) # Welcome section with gr.Row(): inp = gr.Textbox(placeholder="What is your name?", show_label=False, scale=0, min_width=300) out = gr.Label(show_label=False, scale=1, min_width=500) with gr.Row(): submit_btn = gr.Button("Submit", scale=0) submit_btn.click(fn=welcome, inputs=inp, outputs=out) with gr.Row(): gr.Markdown("""
""" ) with gr.Row(): gr.Markdown("## User Questionnaire") with gr.Row(): gr.Markdown(""" To make a prediction about your risk of developing a sleep disorder, please answer the questions below. All questions must be answered for the app to process your data correctly. You can hover over the question heading to display the categorical values associated with each question.

***Disclaimer:***  This app is intended as interactive display of a machine learning model at work ONLY. It is not to be taken as medical advice. If you think you may have or a be at risk of developing a sleep disorder, please consult your PCP or another licensed medical professional. More information on Sleep Disorders """) with gr.Row(): gender = gr.Radio(choices=["Male", "Female"], label="Biological Gender") age = gr.Number(label="Age", value=30) with gr.Row(): gr.HTML("""
Recommended Sleep Duration (Mayo Clinic):
Adults:  7 or more hours per night
Teens (13-18 years):  8 to 10 hours per night
School-age children (6-12 years):  9 to 12 hours per night
Toddlers (1-2 years):  11 to 14 hours per 24 hours (including naps)
Learn more
""") sleep_duration = gr.Slider(0, 12, step=0.1, label="Enter the duration of your sleep (hours)", value=7) gr.HTML("""
Quality of Sleep Explanation:
1-2:  Very poor quality, frequent disturbances
3-4:  Poor quality, several disturbances
5-6:  Average quality, some disturbances
7-8:  Good quality, few disturbances
9-10:  Excellent quality, undisturbed sleep
""") quality_of_sleep = gr.Slider(1, 10, label="Enter the quality of your sleep (1-10)", value=5) gr.HTML("""
Physical Activity Level Explanation:
0-20:  Sedentary (little or no physical activity)
21-40:  Lightly active (light exercise or sports 1-3 days/week)
41-60:  Moderately active (moderate exercise or sports 3-5 days/week)
61-80:  Very active (hard exercise or sports 6-7 days a week)
81-100:  Super active (very hard exercise, physical job, or training twice a day)
""") physical_activity_level = gr.Slider(0, 100, label="Enter your physical activity level (1-100)", value=50) gr.HTML("""
Stress Level Explanation:
1-2:  Very low stress, calm and relaxed
3-4:  Low stress, minor concerns
5-6:  Moderate stress, manageable but noticeable
7-8:  High stress, significant tension
9-10:  Very high stress, overwhelming and constant pressure
""") stress_level = gr.Slider(1, 10, label="Enter your stress level (1-10)", value=5) with gr.Row(): heart_rate = gr.Number(label="Heart Rate", value=70) daily_steps = gr.Number(label="Daily Steps", value=5000) systolic = gr.Number(label="Systolic Blood Pressure", value=120) diastolic = gr.Number(label="Diastolic Blood Pressure", value=80) with gr.Row(): occupation = gr.Dropdown(choices=[ "Doctor", "Engineer", "Lawyer", "Manager", "Nurse", "Sales Representative", "Salesperson", "Scientist", "Software Engineer", "Teacher" ], label="Occupation") bmi_category = gr.Dropdown(choices=["Normal Weight", "Overweight", "Obese"], label="BMI Category") output = gr.Label(label="Predicted Sleep Disorder") confidence_output = gr.Markdown() message_output = gr.Markdown() image_output = gr.Image(show_label=False, container=False) source_output = gr.Markdown() submit_btn = gr.Button("Submit") submit_btn.click(fn=predict_sleep_disorder, inputs=[gender, age, sleep_duration, quality_of_sleep, physical_activity_level, stress_level, heart_rate, daily_steps, systolic, diastolic, occupation, bmi_category], outputs=[output, confidence_output, message_output, image_output, source_output]) demo.launch()