Spaces:
Runtime error
Runtime error
first commit
Browse files- app.py +93 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
import gradio as gr
|
3 |
+
import hopsworks
|
4 |
+
import joblib
|
5 |
+
import pandas as pd
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
project = hopsworks.login()
|
9 |
+
fs = project.get_feature_store()
|
10 |
+
|
11 |
+
|
12 |
+
mr = project.get_model_registry()
|
13 |
+
model = mr.get_model("heart_model", version=1)
|
14 |
+
model_dir = model.download()
|
15 |
+
model = joblib.load(model_dir + "/heart_model.pkl")
|
16 |
+
preprocessing_pipeline = joblib.load(model_dir + "/preprocessing_pipeline.pkl")
|
17 |
+
print("Model downloaded")
|
18 |
+
|
19 |
+
|
20 |
+
def predict(df):
|
21 |
+
df = preprocessing_pipeline.transform(df)
|
22 |
+
prediction = model.predict(df)
|
23 |
+
return prediction[0]
|
24 |
+
|
25 |
+
def heart(heartdisease, smoking, alcoholdrinking, stroke, diffwalking, sex, agecategory, race, diabetic, physicalactivity, genhealth, asthma, kidneydisease, skincancer, mentalhealth, physicalhealth, sleeptime, bmi):
|
26 |
+
df = pd.DataFrame({
|
27 |
+
'smoking': [smoking],
|
28 |
+
'alcohol_drinking': [alcoholdrinking],
|
29 |
+
'stroke': [stroke],
|
30 |
+
'diff_walking': [diffwalking],
|
31 |
+
'sex': [sex],
|
32 |
+
'age_category': [agecategory],
|
33 |
+
'race': [race],
|
34 |
+
'diabetic': [diabetic],
|
35 |
+
'physical_activity': [physicalactivity],
|
36 |
+
'general_health': [genhealth],
|
37 |
+
'asthma': [asthma],
|
38 |
+
'kidney_disease': [kidneydisease],
|
39 |
+
'skin_cancer': [skincancer],
|
40 |
+
'b_m_i': [bmi],
|
41 |
+
'mental_health': [mentalhealth],
|
42 |
+
'physical_health': [physicalhealth],
|
43 |
+
'sleep_time': [sleeptime],
|
44 |
+
})
|
45 |
+
|
46 |
+
# Replace Unknowns with NaNs
|
47 |
+
# Feature pipeline has an imputer
|
48 |
+
df = df.replace('Unknown', np.nan)
|
49 |
+
|
50 |
+
pred = predict(df)
|
51 |
+
|
52 |
+
if heartdisease != "Unknown":
|
53 |
+
df['heart_disease'] = heartdisease
|
54 |
+
df['timestamp'] = pd.to_datetime(datetime.now())
|
55 |
+
|
56 |
+
heart_fg = fs.get_feature_group(name="heart", version=1)
|
57 |
+
heart_fg.insert(df)
|
58 |
+
|
59 |
+
# If insert fails, insert the imputed value instead of nan
|
60 |
+
|
61 |
+
if not pred:
|
62 |
+
return "We predict that you do NOT have heart disease. (But this is not medical advice!)"
|
63 |
+
else:
|
64 |
+
return "We predict that you MIGHT have heart disease. (But this is not medical advice!)"
|
65 |
+
|
66 |
+
demo = gr.Interface(
|
67 |
+
fn=heart,
|
68 |
+
title="Heart Disease Predictive Analytics",
|
69 |
+
description="Experiment with different heart configurations.",
|
70 |
+
allow_flagging="never",
|
71 |
+
inputs=[
|
72 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Heart Disease (TARGET)"),
|
73 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Smoking"),
|
74 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Alcohol Drinking"),
|
75 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Stroke"),
|
76 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Diff Walking"),
|
77 |
+
gr.Dropdown(['Unknown', 'Female', 'Male'], label="Sex"),
|
78 |
+
gr.Dropdown(['Unknown', '18-24', '25-29', '30-34', '35-39', '40-44', '45-49', '50-54', '55-59', '60-64', '65-69', '70-74', '75-79', '80 or older'], label="Age Category"),
|
79 |
+
gr.Dropdown(['Unknown', 'American Indian/Alaskan Native', 'Asian', 'Black', 'Hispanic', 'Other', 'White'], label="Race"),
|
80 |
+
gr.Dropdown(['Unknown', 'No', 'No, borderline diabetes', 'Yes', 'Yes (during pregnancy)'], label="Diabetic"),
|
81 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Physical Activity"),
|
82 |
+
gr.Dropdown(['Unknown', 'Poor', 'Fair', 'Good', 'Very good', 'Excellent'], label="General Health"),
|
83 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Asthma"),
|
84 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Kidney Disease"),
|
85 |
+
gr.Dropdown(['Unknown', 'No', 'Yes'], label="Skin Cancer"),
|
86 |
+
gr.Number(label="Mental Health", minimum=0, maximum=30),
|
87 |
+
gr.Number(label="Physical Health", minimum=0, maximum=30),
|
88 |
+
gr.Number(label="Sleep Time", minimum=1, maximum=24),
|
89 |
+
gr.Number(label="BMI"),
|
90 |
+
],
|
91 |
+
outputs="text")
|
92 |
+
|
93 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
hopsworks
|