|
a |
|
b/gui.py |
|
|
1 |
import pickle |
|
|
2 |
import numpy as np |
|
|
3 |
from flask import Flask, render_template, request, redirect, url_for |
|
|
4 |
from sklearn.preprocessing import StandardScaler |
|
|
5 |
from keras.models import load_model |
|
|
6 |
|
|
|
7 |
app = Flask(__name__) |
|
|
8 |
|
|
|
9 |
# Load the trained machine learning model |
|
|
10 |
model = load_model('my_model.h5') |
|
|
11 |
|
|
|
12 |
# Load the scaler object used during training |
|
|
13 |
scaler = StandardScaler() |
|
|
14 |
scaler_file = 'scaler.pkl' |
|
|
15 |
with open(scaler_file, 'rb') as f: |
|
|
16 |
scaler = pickle.load(f) |
|
|
17 |
|
|
|
18 |
# Define the home page route |
|
|
19 |
@app.route('/') |
|
|
20 |
def home(): |
|
|
21 |
return render_template('home.html', favicon_url=url_for('static', filename='images/favicon.png')) |
|
|
22 |
|
|
|
23 |
# Define the form page route |
|
|
24 |
@app.route('/form') |
|
|
25 |
def form(): |
|
|
26 |
return render_template('form.html') |
|
|
27 |
|
|
|
28 |
@app.route('/results', methods=['GET', 'POST']) |
|
|
29 |
def results(): |
|
|
30 |
if request.method == 'POST': |
|
|
31 |
# Print out the form data for debugging |
|
|
32 |
print(request.form) |
|
|
33 |
|
|
|
34 |
# Extract the form data |
|
|
35 |
age = int(request.form.get('age')) |
|
|
36 |
gender = request.form.get('gender') |
|
|
37 |
if gender == 'Male': gender = 1 |
|
|
38 |
else: gender = 2 |
|
|
39 |
air_pollution = int(request.form.get('air_pollution')) |
|
|
40 |
alcohol_use = int(request.form.get('alcohol_use')) |
|
|
41 |
dust_allergy = int(request.form.get('dust_allergy')) |
|
|
42 |
occupational_hazards = int(request.form.get('occupational_hazards')) |
|
|
43 |
genetic_risk = int(request.form.get('genetic_risk')) |
|
|
44 |
chronic_lung_disease = int(request.form.get('chronic_lung_disease')) |
|
|
45 |
balanced_diet = int(request.form.get('balanced_diet')) |
|
|
46 |
obesity = int(request.form.get('obesity')) |
|
|
47 |
smoking = int(request.form.get('smoking')) |
|
|
48 |
passive_smoker = int(request.form.get('passive_smoker')) |
|
|
49 |
chest_pain = int(request.form.get('chest_pain')) |
|
|
50 |
coughing_blood = int(request.form.get('coughing_blood')) |
|
|
51 |
fatigue = int(request.form.get('fatigue')) |
|
|
52 |
weight_loss = int(request.form.get('weight_loss')) |
|
|
53 |
shortness_of_breath = int(request.form.get('shortness_of_breath')) |
|
|
54 |
wheezing = int(request.form.get('wheezing')) |
|
|
55 |
swallowing_difficulty = int(request.form.get('swallowing_difficulty')) |
|
|
56 |
clubbing = int(request.form.get('clubbing')) |
|
|
57 |
frequent_cold = int(request.form.get('frequent_cold')) |
|
|
58 |
dry_cough = int(request.form.get('dry_cough')) |
|
|
59 |
snoring = int(request.form.get('snoring')) |
|
|
60 |
|
|
|
61 |
data = np.zeros((23)) |
|
|
62 |
data[0] = age |
|
|
63 |
data[1] = gender |
|
|
64 |
data[2] = air_pollution |
|
|
65 |
data[3] = alcohol_use |
|
|
66 |
data[4] = dust_allergy |
|
|
67 |
data[5] = occupational_hazards |
|
|
68 |
data[6] = genetic_risk |
|
|
69 |
data[7] = chronic_lung_disease |
|
|
70 |
data[8] = balanced_diet |
|
|
71 |
data[9] = obesity |
|
|
72 |
data[10] = smoking |
|
|
73 |
data[11] = passive_smoker |
|
|
74 |
data[12] = chest_pain |
|
|
75 |
data[13] = coughing_blood |
|
|
76 |
data[14] = fatigue |
|
|
77 |
data[15] = weight_loss |
|
|
78 |
data[16] = shortness_of_breath |
|
|
79 |
data[17] = wheezing |
|
|
80 |
data[18] = swallowing_difficulty |
|
|
81 |
data[19] = clubbing |
|
|
82 |
data[20] = frequent_cold |
|
|
83 |
data[21] = dry_cough |
|
|
84 |
data[22] = snoring |
|
|
85 |
|
|
|
86 |
# Convert the list to a numpy array with the desired shape |
|
|
87 |
new_data = np.array([data]) |
|
|
88 |
|
|
|
89 |
# Standardize the new data using the loaded scaler |
|
|
90 |
new_data_scaled = scaler.transform(new_data) |
|
|
91 |
|
|
|
92 |
# Make predictions |
|
|
93 |
predictions = model.predict(new_data_scaled) |
|
|
94 |
|
|
|
95 |
# Convert the predictions to class labels |
|
|
96 |
predicted_classes = np.argmax(predictions, axis=1) |
|
|
97 |
|
|
|
98 |
|
|
|
99 |
# Determine the predicted outcome based on the prediction |
|
|
100 |
if predicted_classes[0] == 0: |
|
|
101 |
outcome = 'Low' |
|
|
102 |
elif predicted_classes[0] == 1: |
|
|
103 |
outcome = 'Medium' |
|
|
104 |
else: |
|
|
105 |
outcome = 'High' |
|
|
106 |
|
|
|
107 |
# Render the results template with the predicted outcome |
|
|
108 |
return render_template('results.html', outcome=outcome) |
|
|
109 |
else: |
|
|
110 |
# If the request method is not POST, redirect to the home page |
|
|
111 |
return redirect('/') |
|
|
112 |
|
|
|
113 |
if __name__ == '__main__': |
|
|
114 |
app.run(debug=True) |