-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (42 loc) · 1.73 KB
/
app.py
File metadata and controls
58 lines (42 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from flask import Flask, render_template, request
import joblib
import pandas as pd
from pathlib import Path
app = Flask(__name__, template_folder='web_temp', static_folder='static')
BASE_DIR = Path(__file__).resolve().parent
MODEL_PATH = BASE_DIR / "pkl_files" / "model1.pkl"
ENCODER_PATH = BASE_DIR / "pkl_files" / "Disease_encoder1.pkl"
FEATURES_PATH = BASE_DIR / "pkl_files" / "trained_features1.pkl"
model = joblib.load(MODEL_PATH)
disease_encoder = joblib.load(ENCODER_PATH)
feature_list = joblib.load(FEATURES_PATH)
PRECAUTIONS_CSV = BASE_DIR / "dataset" / "symptom_precaution.csv"
precautions_df = pd.read_csv(PRECAUTIONS_CSV)
@app.route('/')
def index():
"""Render the home page."""
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
"""Handle symptom input and return prediction + precautions."""
symptoms_list = request.form.getlist('symptoms')
input_data = {symptom: 0 for symptom in feature_list}
for s in symptoms_list:
if s in input_data:
input_data[s] = 1
input_df = pd.DataFrame([input_data])[feature_list]
predicted_index = model.predict(input_df)[0]
predicted_disease = disease_encoder.inverse_transform([predicted_index])[0]
disease_data = precautions_df[precautions_df['Disease'].str.strip() == predicted_disease.strip()]
if not disease_data.empty:
row = disease_data.iloc[0]
precautions = [str(row[f'Precaution_{i}']) for i in range(1, 5)]
else:
precautions = ["No precautions found"]
return render_template(
'result.html',
prediction=predicted_disease,
precautions=precautions
)
if __name__ == '__main__':
app.run(debug=False, host="0.0.0.0", port=5000)