|
|
|
import numpy as np |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
from sklearn.linear_model import LogisticRegression |
|
from sklearn.neighbors import KNeighborsClassifier |
|
from sklearn.metrics import accuracy_score |
|
|
|
|
|
df = pd.read_csv('Sleep_health_and_lifestyle_dataset.csv') |
|
|
|
|
|
|
|
df.isnull().sum() |
|
|
|
df['Sleep Disorder'].fillna('None', inplace=True) |
|
|
|
df.drop('Person ID', axis=1, inplace=True) |
|
|
|
df['systolic_bp'] = df['Blood Pressure'].apply(lambda x: x.split('/')[0]) |
|
df['diastolic_bp'] = df['Blood Pressure'].apply(lambda x: x.split('/')[1]) |
|
|
|
df.drop('Blood Pressure', axis=1, inplace=True) |
|
|
|
df['BMI Category'] = df['BMI Category'].replace('Normal Weight', 'Normal') |
|
|
|
df['Gender'].replace({'Male':0, 'Female':1}, inplace=True) |
|
df['Occupation'].replace({'Software Engineer':0, 'Doctor':1,'Sales Representative':2,'Teacher':3,'Nurse':4,'Engineer':5,'Accountant':6,'Scientist':7,'Lawyer':8,'Salesperson':9,'Manager':10}, inplace=True) |
|
df['BMI Category'].replace({'Normal':0, 'Overweight':1, 'Obese':2}, inplace=True) |
|
df['Sleep Disorder'].replace({'None':0,'Insomnia':1, 'Sleep Apnea':2}, inplace=True) |
|
|
|
from sklearn.model_selection import train_test_split |
|
X_train, X_test, y_train, y_test = train_test_split(df.drop('Sleep Disorder',axis=1), df['Sleep Disorder'], test_size=0.2, random_state=42) |
|
|
|
from sklearn.ensemble import RandomForestClassifier |
|
rfc = RandomForestClassifier() |
|
rfc.fit(X_train, y_train) |
|
|
|
def func(a, b, c, d, e, f, g, h, i, j, k, l): |
|
|
|
|
|
x_test = [[a, b, c, d, e, f, g, h, i, j, k, l]] |
|
result = rfc.predict(x_test)[0] |
|
dict = { |
|
0 : "None", |
|
1: "Insomnia", |
|
2 : "Sleep Apnea" |
|
} |
|
|
|
if result==0: |
|
str = "Yay! You are totally fit ππ" |
|
elif result==1: |
|
str = "You may be suffering from Sleep Insomnia ππ" |
|
else: |
|
str="You may be suffering from Sleep Apnea ππ" |
|
return f"{dict[result]}\n\n{str}" |
|
|
|
|
|
import gradio as gr |
|
|
|
demo = gr.Interface( |
|
fn= func, |
|
inputs=[ |
|
gr.Textbox(label="Gender", placeholder="Enter 0 for Male and 1 for Female", elem_id="gender",type='text'), |
|
gr.Textbox(label="Age", placeholder="Enter Your Age", elem_id="gender",type='text'), |
|
gr.Textbox(label="Occupation", placeholder="Enter from 0 - 10", elem_id="gender",type='text'), |
|
gr.Textbox(label="Sleep Duration(in Hrs)", placeholder="Enter Your Sleep Duration", elem_id="gender",type='text'), |
|
gr.Textbox(label="Quality of Sleep", placeholder="Enter Your Quality of Sleep", elem_id="gender",type='text'), |
|
gr.Textbox(label="Physical Activity Level", placeholder="Enter Your Physical Activity Level", elem_id="gender",type='text'), |
|
gr.Textbox(label="Stress Level", placeholder="Enter Your Stress Level", elem_id="gender",type='text'), |
|
gr.Textbox(label="BMI Category", placeholder="Enter 0 for Normal 1 for Overweight and 2 for Obeses", elem_id="gender",type='text'), |
|
gr.Textbox(label="Systolic Blood Pressure", placeholder="Enter Your Systolic Blood Pressure", elem_id="gender",type='text'), |
|
gr.Textbox(label="Diastolic Blood Pressure", placeholder="Enter Your Diastolic Blood Pressure", elem_id="gender",type='text'), |
|
gr.Textbox(label="Heart Rate", placeholder="Enter Your Heart Rate", elem_id="gender",type='text'), |
|
gr.Textbox(label="Daily Steps Count", placeholder="Enter Your Daily Steps Count", elem_id="gender",type='text') |
|
], |
|
outputs='text', |
|
theme=gr.themes.Soft(), |
|
title="<h1 id=title-first> Welcome to HEALTHSURE <br> <span id=title-second>Predict Your Sleep Disorder here using a ML Model</span> </h1>", |
|
description="<p id=desc>βΎ Please Enter the Data in following way(Important)<br>βΎ Gender: <span id=desc-info>Male=0 Female=1</span><br>βΎ Occupation: <span id=desc-info> Software Engineer=0 Doctor=1 Sales Representative=2 Teacher=3 Nurse=4 <br> Engineer=5 Accontant=6 Scientist=7 Lawyer=8 SalesPerson=9 Manager=10</span> <br>βΎ BMI Category: <span id=desc-info>Normal=0 OverWeight=1 Obese=2 </span></p> <br> <p id=desc>**Some Examples are given at bottom You can try them by clicking on it.<br>**Enter only Numeric Value</p>", |
|
css=""" |
|
.gradio-container {background-color: #daeefe}" |
|
#gender { background-color : teal !important; } |
|
#gender textarea {background-color: #ecf7fd; font-size : 15px; color : black; |
|
font-weight : bold; !important;} |
|
|
|
#desc {font-weight : bold; color : black !important;} |
|
#desc-info{font-weight:normal;} |
|
h1 {text-align : center; font-size: 40px !important;} |
|
#title-first {color:black; !important;} |
|
#title-second {color:green; font-size: 17px !important;} |
|
#a-tag { color : white !important;} |
|
|
|
#a-tag:hover {text-decoration : none !important;} |
|
|
|
""", |
|
|
|
examples=[[0, 44, 9, 6.3, 6, 45, 7, 1, 130,85, 72, 6000],[1, 31, 4, 7.9, 8, 75, 4, 0, 117, 76, 69, 6800],[1, 49, 4, 6.1, 6, 90, 8, 1, 140,95, 75, 10000]] |
|
|
|
|
|
) |
|
|
|
|
|
demo.launch(inline=False) |