soham901 commited on
Commit
298dffa
1 Parent(s): dea1898

Upload 4 files

Browse files
Files changed (4) hide show
  1. api.py +130 -0
  2. app.py +60 -0
  3. requirements.txt +5 -0
  4. utils.py +7 -0
api.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import os
3
+ import numpy as np
4
+ import tensorflow as tf
5
+
6
+
7
+ data = "DATA"
8
+
9
+ exit()
10
+
11
+
12
+ def predict(values, dic):
13
+ # diabetes
14
+ if len(values) == 8:
15
+ dic2 = {'NewBMI_Obesity 1': 0, 'NewBMI_Obesity 2': 0, 'NewBMI_Obesity 3': 0, 'NewBMI_Overweight': 0,
16
+ 'NewBMI_Underweight': 0, 'NewInsulinScore_Normal': 0, 'NewGlucose_Low': 0,
17
+ 'NewGlucose_Normal': 0, 'NewGlucose_Overweight': 0, 'NewGlucose_Secret': 0}
18
+
19
+ if dic['BMI'] <= 18.5:
20
+ dic2['NewBMI_Underweight'] = 1
21
+ elif 18.5 < dic['BMI'] <= 24.9:
22
+ pass
23
+ elif 24.9 < dic['BMI'] <= 29.9:
24
+ dic2['NewBMI_Overweight'] = 1
25
+ elif 29.9 < dic['BMI'] <= 34.9:
26
+ dic2['NewBMI_Obesity 1'] = 1
27
+ elif 34.9 < dic['BMI'] <= 39.9:
28
+ dic2['NewBMI_Obesity 2'] = 1
29
+ elif dic['BMI'] > 39.9:
30
+ dic2['NewBMI_Obesity 3'] = 1
31
+
32
+ if 16 <= dic['Insulin'] <= 166:
33
+ dic2['NewInsulinScore_Normal'] = 1
34
+
35
+ if dic['Glucose'] <= 70:
36
+ dic2['NewGlucose_Low'] = 1
37
+ elif 70 < dic['Glucose'] <= 99:
38
+ dic2['NewGlucose_Normal'] = 1
39
+ elif 99 < dic['Glucose'] <= 126:
40
+ dic2['NewGlucose_Overweight'] = 1
41
+ elif dic['Glucose'] > 126:
42
+ dic2['NewGlucose_Secret'] = 1
43
+
44
+ dic.update(dic2)
45
+ values2 = list(map(float, list(dic.values())))
46
+
47
+ model = pickle.load(open('models/diabetes.pkl','rb'))
48
+ values = np.asarray(values2)
49
+ return model.predict(values.reshape(1, -1))[0]
50
+
51
+ # breast_cancer
52
+ elif len(values) == 22:
53
+ model = pickle.load(open('models/breast_cancer.pkl','rb'))
54
+ values = np.asarray(values)
55
+ return model.predict(values.reshape(1, -1))[0]
56
+
57
+ # heart disease
58
+ elif len(values) == 13:
59
+ model = pickle.load(open('models/heart.pkl','rb'))
60
+ values = np.asarray(values)
61
+ return model.predict(values.reshape(1, -1))[0]
62
+
63
+ # kidney disease
64
+ elif len(values) == 24:
65
+ model = pickle.load(open('models/kidney.pkl','rb'))
66
+ values = np.asarray(values)
67
+ return model.predict(values.reshape(1, -1))[0]
68
+
69
+ # liver disease
70
+ elif len(values) == 10:
71
+ model = pickle.load(open('models/liver.pkl','rb'))
72
+ values = np.asarray(values)
73
+ return model.predict(values.reshape(1, -1))[0]
74
+
75
+
76
+ def predict_api():
77
+ try:
78
+ to_predict_dict = request.form.to_dict()
79
+
80
+ for key, value in to_predict_dict.items():
81
+ try:
82
+ to_predict_dict[key] = int(value)
83
+ except ValueError:
84
+ to_predict_dict[key] = float(value)
85
+
86
+ to_predict_list = list(map(float, list(to_predict_dict.values())))
87
+ pred = predict(to_predict_list, to_predict_dict)
88
+
89
+ return {'prediction': pred}
90
+
91
+ except Exception as e:
92
+ return {'error': str(e)}
93
+
94
+
95
+ def malariapredict_api():
96
+ try:
97
+ img = Image.open(request.files['image'])
98
+ img.save("uploads/image.jpg")
99
+ img_path = os.path.join(os.path.dirname(__file__), 'uploads/image.jpg')
100
+ os.path.isfile(img_path)
101
+ img = tf.keras.utils.load_img(img_path, target_size=(128, 128))
102
+ img = tf.keras.utils.img_to_array(img)
103
+ img = np.expand_dims(img, axis=0)
104
+
105
+ model = tf.keras.models.load_model("models/malaria.h5")
106
+ pred = np.argmax(model.predict(img))
107
+
108
+ return {'prediction': int(pred)}
109
+
110
+ except Exception as e:
111
+ return {'error': str(e)}
112
+
113
+
114
+ def pneumoniapredict_api():
115
+ try:
116
+ img = Image.open(request.files['image']).convert('L')
117
+ img.save("uploads/image.jpg")
118
+ img_path = os.path.join(os.path.dirname(__file__), 'uploads/image.jpg')
119
+ os.path.isfile(img_path)
120
+ img = tf.keras.utils.load_img(img_path, target_size=(128, 128))
121
+ img = tf.keras.utils.img_to_array(img)
122
+ img = np.expand_dims(img, axis=0)
123
+
124
+ model = tf.keras.models.load_model("models/pneumonia.h5")
125
+ pred = np.argmax(model.predict(img))
126
+
127
+ return {'prediction': int(pred)}
128
+
129
+ except Exception as e:
130
+ return {'error': str(e)}
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import api
3
+
4
+ print(api.data)
5
+
6
+ st.title("Health Care Prediction App")
7
+ st.markdown("### Predict whether a patient has a certain disease or not")
8
+
9
+ st.sidebar.title("Predictive Models")
10
+ st.sidebar.markdown("Select the model you want to use")
11
+
12
+ model_type = st.sidebar.selectbox("Model", ("Diabetes", "Breast Cancer", "Heart Disease", "Kidney Disease", "Liver Disease"))
13
+
14
+
15
+ st.sidebar.title("How to use")
16
+ st.sidebar.markdown(
17
+ "Select the model you want to use. Then, enter the values in the fields and click on the 'Predict' button to check the prediction."
18
+ )
19
+
20
+ st.sidebar.title("About")
21
+ st.sidebar.info(
22
+ "This web app is to predict whether a patient has a certain disease or not. Built with Streamlit by [Pix2Pred Team]()"
23
+ )
24
+
25
+
26
+ with st.expander("Prection"):
27
+ if model_type == "Diabetes":
28
+ st.markdown("### Diabetes")
29
+ st.markdown("#### Please enter the details of the patient")
30
+ st.markdown("#### Features:")
31
+ st.markdown("* Pregnancies: Number of times pregnant")
32
+ st.markdown("* Glucose: Plasma glucose concentration a 2 hours in an oral glucose tolerance test")
33
+ st.markdown("* BloodPressure: Diastolic blood pressure (mm Hg)")
34
+ st.markdown("* SkinThickness: Triceps skin fold thickness (mm)")
35
+ st.markdown("* Insulin: 2-Hour serum insulin (mu U/ml)")
36
+ st.markdown("* BMI: Body mass index (weight in kg/(height in m)^2)")
37
+ st.markdown("* DiabetesPedigreeFunction: Diabetes pedigree function")
38
+ st.markdown("* Age: Age (years)")
39
+ st.markdown("* Outcome: Class variable (0 or 1) 268 of 768 are 1, the others are 0")
40
+ st.markdown("#### Enter the values in the fields below")
41
+ pregnancies = st.number_input("Pregnancies", min_value=0, max_value=20, value=0)
42
+ glucose = st.number_input("Glucose", min_value=0, max_value=200, value=0)
43
+ blood_pressure = st.number_input("Blood Pressure", min_value=0, max_value=200, value=0)
44
+ skin_thickness = st.number_input("Skin Thickness", min_value=0, max_value=100, value=0)
45
+ insulin = st.number_input("Insulin", min_value=0, max_value=800, value=0)
46
+ bmi = st.number_input("BMI", min_value=0, max_value=100, value=0)
47
+ diabetes_pedigree_function = st.number_input("Diabetes Pedigree Function", min_value=0.0, max_value=2.0, value=0.0)
48
+ age = st.number_input("Age", min_value=0, max_value=120, value=0)
49
+
50
+ if st.button("Predict"):
51
+ # print the prediction
52
+ st.write("The patient has diabetes:", predict(model_type, [pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, diabetes_pedigree_function, age]))
53
+
54
+
55
+
56
+ st.subheader("About")
57
+
58
+ st.write("This is a simple web app to predict the price of the house based on the square feet. I used only 10 data points to train the model. So, the predictions may not be accurate. The model used is Linear Regression. Purpose of this project is to learn how to deploy a machine learning model.")
59
+ st.write("Built by [Pix2Pred Team]() using Streamlit.")
60
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ scikit-learn
2
+ numpy
3
+ streamlit
4
+ Pillow==9.1.1
5
+ tensorflow==2.9.1
utils.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ forms = {
2
+ 'diabetes': "Hello",
3
+ }
4
+
5
+
6
+ def get_form(model_type: str):
7
+ return