Commit
Β·
a7f8c3c
1
Parent(s):
2e93dd6
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#importing the libraries
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
+
from sklearn.linear_model import LogisticRegression
|
7 |
+
from sklearn.neighbors import KNeighborsClassifier
|
8 |
+
from sklearn.metrics import accuracy_score
|
9 |
+
|
10 |
+
#loading the dataset
|
11 |
+
df = pd.read_csv('Sleep_health_and_lifestyle_dataset.csv')
|
12 |
+
# df.head()
|
13 |
+
|
14 |
+
#checking for missing values
|
15 |
+
df.isnull().sum()
|
16 |
+
#replacing the null values with 'None' in the column 'Sleep Disorder'
|
17 |
+
df['Sleep Disorder'].fillna('None', inplace=True)
|
18 |
+
#drop column Person ID
|
19 |
+
df.drop('Person ID', axis=1, inplace=True)
|
20 |
+
#spliting the blood pressure into two columns
|
21 |
+
df['systolic_bp'] = df['Blood Pressure'].apply(lambda x: x.split('/')[0])
|
22 |
+
df['diastolic_bp'] = df['Blood Pressure'].apply(lambda x: x.split('/')[1])
|
23 |
+
#droping the blood pressure column
|
24 |
+
df.drop('Blood Pressure', axis=1, inplace=True)
|
25 |
+
#replacing normal weight with normal in BMI column
|
26 |
+
df['BMI Category'] = df['BMI Category'].replace('Normal Weight', 'Normal')
|
27 |
+
|
28 |
+
df['Gender'].replace({'Male':0, 'Female':1}, inplace=True)
|
29 |
+
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)
|
30 |
+
df['BMI Category'].replace({'Normal':0, 'Overweight':1, 'Obese':2}, inplace=True)
|
31 |
+
df['Sleep Disorder'].replace({'None':0,'Insomnia':1, 'Sleep Apnea':2}, inplace=True)
|
32 |
+
|
33 |
+
from sklearn.model_selection import train_test_split
|
34 |
+
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)
|
35 |
+
|
36 |
+
from sklearn.ensemble import RandomForestClassifier
|
37 |
+
rfc = RandomForestClassifier()
|
38 |
+
rfc.fit(X_train, y_train)
|
39 |
+
|
40 |
+
def func(a, b, c, d, e, f, g, h, i, j, k, l):
|
41 |
+
|
42 |
+
|
43 |
+
x_test = [[a, b, c, d, e, f, g, h, i, j, k, l]]
|
44 |
+
result = rfc.predict(x_test)[0]
|
45 |
+
dict = {
|
46 |
+
0 : "None",
|
47 |
+
1: "Insomnia",
|
48 |
+
2 : "Sleep Apnea"
|
49 |
+
}
|
50 |
+
|
51 |
+
if result==0:
|
52 |
+
str = "Yay! You are totally fit ππ"
|
53 |
+
elif result==1:
|
54 |
+
str = "You may be suffering from Sleep Insomnia ππ"
|
55 |
+
else:
|
56 |
+
str="You may be suffering from Sleep Apnea ππ"
|
57 |
+
return f"{dict[result]}\n\n{str}"
|
58 |
+
|
59 |
+
|
60 |
+
import gradio as gr
|
61 |
+
|
62 |
+
demo = gr.Interface(
|
63 |
+
fn= func,
|
64 |
+
inputs=[
|
65 |
+
gr.Textbox(label="Gender", placeholder="Enter 0 for Male and 1 for Female", elem_id="gender",type='text'),
|
66 |
+
gr.Textbox(label="Age", placeholder="Enter Your Age", elem_id="gender",type='text'),
|
67 |
+
gr.Textbox(label="Occupation", placeholder="Enter from 0 - 10", elem_id="gender",type='text'),
|
68 |
+
gr.Textbox(label="Sleep Duration(in Hrs)", placeholder="Enter Your Sleep Duration", elem_id="gender",type='text'),
|
69 |
+
gr.Textbox(label="Quality of Sleep", placeholder="Enter Your Quality of Sleep", elem_id="gender",type='text'),
|
70 |
+
gr.Textbox(label="Physical Activity Level", placeholder="Enter Your Physical Activity Level", elem_id="gender",type='text'),
|
71 |
+
gr.Textbox(label="Stress Level", placeholder="Enter Your Stress Level", elem_id="gender",type='text'),
|
72 |
+
gr.Textbox(label="BMI Category", placeholder="Enter 0 for Normal 1 for Overweight and 2 for Obeses", elem_id="gender",type='text'),
|
73 |
+
gr.Textbox(label="Systolic Blood Pressure", placeholder="Enter Your Systolic Blood Pressure", elem_id="gender",type='text'),
|
74 |
+
gr.Textbox(label="Diastolic Blood Pressure", placeholder="Enter Your Diastolic Blood Pressure", elem_id="gender",type='text'),
|
75 |
+
gr.Textbox(label="Heart Rate", placeholder="Enter Your Heart Rate", elem_id="gender",type='text'),
|
76 |
+
gr.Textbox(label="Daily Steps Count", placeholder="Enter Your Daily Steps Count", elem_id="gender",type='text')
|
77 |
+
],
|
78 |
+
outputs='text',
|
79 |
+
theme=gr.themes.Soft(),
|
80 |
+
title="<h1 id=title-first> Welcome to HEALTHSURE <br> <span id=title-second>Predict Your Sleep Disorder here using a ML Model</span> </h1>",
|
81 |
+
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>",
|
82 |
+
css="""
|
83 |
+
.gradio-container {background-color: #daeefe}"
|
84 |
+
#gender { background-color : teal !important; }
|
85 |
+
#gender textarea {background-color: #ecf7fd; font-size : 15px; color : black;
|
86 |
+
font-weight : bold; !important;}
|
87 |
+
|
88 |
+
#desc {font-weight : bold; color : black !important;}
|
89 |
+
#desc-info{font-weight:normal;}
|
90 |
+
h1 {text-align : center; font-size: 40px !important;}
|
91 |
+
#title-first {color:black; !important;}
|
92 |
+
#title-second {color:green; font-size: 17px !important;}
|
93 |
+
#a-tag { color : white !important;}
|
94 |
+
|
95 |
+
#a-tag:hover {text-decoration : none !important;}
|
96 |
+
|
97 |
+
""",
|
98 |
+
|
99 |
+
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]]
|
100 |
+
|
101 |
+
|
102 |
+
)
|
103 |
+
|
104 |
+
|
105 |
+
demo.launch(inline=False)
|