diff --git a/client/src/components/Prediction.jsx b/client/src/components/Prediction.jsx index 6172a2e..05b9df5 100644 --- a/client/src/components/Prediction.jsx +++ b/client/src/components/Prediction.jsx @@ -24,8 +24,8 @@ const Prediction = () => { "https://diabetes-prediction-1-6a5i.onrender.com/predict", userInput ); + console.log("API Response:", response.data); setPrediction(response.data); - console.log(response.data); } catch (error) { console.error("Error:", error); } @@ -34,14 +34,14 @@ const Prediction = () => { useEffect(() => { window.scrollTo(0, 0); - }); + }, []); const handleChange = (e) => { setUserInput({ ...userInput, [e.target.name]: e.target.value }); }; return ( -
+
{ stiffness: 100, delay: 0.5, }} - className="w-full sm:w-1/2 sm:ml-8 mt-8 sm:mt-0 sm:order-2" + className="w-full sm:w-1/2 sm:ml-8 mt-8 sm:mt-0 sm:order-2 flex justify-center" > {!prediction && (
@@ -221,15 +221,28 @@ const Prediction = () => {
)} {prediction && ( -
+ +
Prediction GIF -

- {prediction.prediction} -

+

+ {prediction.message || "No message"} +

+
+ {prediction.recommendations && Array.isArray(prediction.recommendations) && prediction.recommendations.map((rec, index) => ( +

+ • {rec.recommendation} +

+ ))} +
+ + +
)} diff --git a/server/app.log b/server/app.log new file mode 100644 index 0000000..931949d --- /dev/null +++ b/server/app.log @@ -0,0 +1,29 @@ +2024-06-01 02:02:00,809 WARNING * Debugger is active! +2024-06-01 02:02:00,816 INFO * Debugger PIN: 664-860-149 +2024-06-01 02:02:09,538 INFO 127.0.0.1 - - [01/Jun/2024 02:02:09] "OPTIONS /predict HTTP/1.1" 200 - +2024-06-01 02:02:09,551 INFO 127.0.0.1 - - [01/Jun/2024 02:02:09] "POST /predict HTTP/1.1" 500 - +2024-06-01 02:02:10,566 INFO 127.0.0.1 - - [01/Jun/2024 02:02:10] "POST /predict HTTP/1.1" 500 - +2024-06-01 02:03:38,753 INFO * Detected change in '/home/agrima/Desktop/open-source/Diabetes-Prediction/server/app.py', reloading +2024-06-01 02:03:39,339 WARNING * Debugger is active! +2024-06-01 02:03:39,345 INFO * Debugger PIN: 132-725-711 +2024-06-01 02:03:43,468 INFO 127.0.0.1 - - [01/Jun/2024 02:03:43] "OPTIONS /predict HTTP/1.1" 200 - +2024-06-01 02:03:43,475 INFO Prediction Result: {'prediction': 'high', 'message': 'You have high chances of Diabetes! Please consult a Doctor', 'gif_url': 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif', 'more_info_url': 'https://www.diabetes.org/diabetes', 'recommendations': [{'metric': 'Glucose', 'value': 95.0, 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.'}, {'metric': 'BMI', 'value': 33.0, 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.'}, {'metric': 'BloodPressure', 'value': 95.0, 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.'}, {'metric': 'Insulin', 'value': 95.0, 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.'}]} +2024-06-01 02:03:43,476 INFO 127.0.0.1 - - [01/Jun/2024 02:03:43] "POST /predict HTTP/1.1" 200 - +2024-06-01 02:06:18,592 INFO 127.0.0.1 - - [01/Jun/2024 02:06:18] "OPTIONS /predict HTTP/1.1" 200 - +2024-06-01 02:06:18,597 INFO Prediction Result: {'prediction': 'high', 'message': 'You have high chances of Diabetes! Please consult a Doctor', 'gif_url': 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif', 'more_info_url': 'https://www.diabetes.org/diabetes', 'recommendations': [{'metric': 'Glucose', 'value': 95.0, 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.'}, {'metric': 'BMI', 'value': 33.0, 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.'}, {'metric': 'BloodPressure', 'value': 95.0, 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.'}, {'metric': 'Insulin', 'value': 95.0, 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.'}]} +2024-06-01 02:06:18,598 INFO 127.0.0.1 - - [01/Jun/2024 02:06:18] "POST /predict HTTP/1.1" 200 - +2024-06-01 02:06:19,720 INFO Prediction Result: {'prediction': 'high', 'message': 'You have high chances of Diabetes! Please consult a Doctor', 'gif_url': 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif', 'more_info_url': 'https://www.diabetes.org/diabetes', 'recommendations': [{'metric': 'Glucose', 'value': 95.0, 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.'}, {'metric': 'BMI', 'value': 33.0, 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.'}, {'metric': 'BloodPressure', 'value': 95.0, 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.'}, {'metric': 'Insulin', 'value': 95.0, 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.'}]} +2024-06-01 02:06:19,720 INFO 127.0.0.1 - - [01/Jun/2024 02:06:19] "POST /predict HTTP/1.1" 200 - +2024-06-01 02:06:35,235 INFO 127.0.0.1 - - [01/Jun/2024 02:06:35] "OPTIONS /predict HTTP/1.1" 200 - +2024-06-01 02:06:35,241 INFO Prediction Result: {'prediction': 'high', 'message': 'You have high chances of Diabetes! Please consult a Doctor', 'gif_url': 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif', 'more_info_url': 'https://www.diabetes.org/diabetes', 'recommendations': [{'metric': 'Glucose', 'value': 95.0, 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.'}, {'metric': 'BMI', 'value': 33.0, 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.'}, {'metric': 'BloodPressure', 'value': 95.0, 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.'}, {'metric': 'Insulin', 'value': 95.0, 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.'}]} +2024-06-01 02:06:35,242 INFO 127.0.0.1 - - [01/Jun/2024 02:06:35] "POST /predict HTTP/1.1" 200 - +2024-06-01 02:06:35,950 INFO Prediction Result: {'prediction': 'high', 'message': 'You have high chances of Diabetes! Please consult a Doctor', 'gif_url': 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif', 'more_info_url': 'https://www.diabetes.org/diabetes', 'recommendations': [{'metric': 'Glucose', 'value': 95.0, 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.'}, {'metric': 'BMI', 'value': 33.0, 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.'}, {'metric': 'BloodPressure', 'value': 95.0, 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.'}, {'metric': 'Insulin', 'value': 95.0, 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.'}]} +2024-06-01 02:06:35,951 INFO 127.0.0.1 - - [01/Jun/2024 02:06:35] "POST /predict HTTP/1.1" 200 - +2024-06-01 02:07:16,319 INFO 127.0.0.1 - - [01/Jun/2024 02:07:16] "OPTIONS /predict HTTP/1.1" 200 - +2024-06-01 02:07:16,325 INFO Prediction Result: {'prediction': 'high', 'message': 'You have high chances of Diabetes! Please consult a Doctor', 'gif_url': 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif', 'more_info_url': 'https://www.diabetes.org/diabetes', 'recommendations': [{'metric': 'Glucose', 'value': 95.0, 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.'}, {'metric': 'BMI', 'value': 98.0, 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.'}, {'metric': 'BloodPressure', 'value': 96.0, 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.'}, {'metric': 'Insulin', 'value': 56.0, 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.'}]} +2024-06-01 02:07:16,326 INFO 127.0.0.1 - - [01/Jun/2024 02:07:16] "POST /predict HTTP/1.1" 200 - +2024-06-01 02:27:13,059 INFO 127.0.0.1 - - [01/Jun/2024 02:27:13] "OPTIONS /predict HTTP/1.1" 200 - +2024-06-01 02:27:13,067 INFO Prediction Result: {'prediction': 'high', 'message': 'You have high chances of Diabetes! Please consult a Doctor', 'gif_url': 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif', 'more_info_url': 'https://www.diabetes.org/diabetes', 'recommendations': [{'metric': 'Glucose', 'value': 95.0, 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.'}, {'metric': 'BMI', 'value': 98.0, 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.'}, {'metric': 'BloodPressure', 'value': 96.0, 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.'}, {'metric': 'Insulin', 'value': 56.0, 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.'}]} +2024-06-01 02:27:13,068 INFO 127.0.0.1 - - [01/Jun/2024 02:27:13] "POST /predict HTTP/1.1" 200 - +2024-06-01 02:27:14,198 INFO Prediction Result: {'prediction': 'high', 'message': 'You have high chances of Diabetes! Please consult a Doctor', 'gif_url': 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif', 'more_info_url': 'https://www.diabetes.org/diabetes', 'recommendations': [{'metric': 'Glucose', 'value': 95.0, 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.'}, {'metric': 'BMI', 'value': 98.0, 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.'}, {'metric': 'BloodPressure', 'value': 96.0, 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.'}, {'metric': 'Insulin', 'value': 56.0, 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.'}]} +2024-06-01 02:27:14,200 INFO 127.0.0.1 - - [01/Jun/2024 02:27:14] "POST /predict HTTP/1.1" 200 - diff --git a/server/app.py b/server/app.py index d22808f..b2d948b 100644 --- a/server/app.py +++ b/server/app.py @@ -3,9 +3,10 @@ import pandas as pd from flask import Flask, request, jsonify from flask_cors import CORS +import logging app = Flask(__name__) -CORS(app, resources={r"/*": {"origins": "*"}}) +CORS(app, resources={r"/*": {"origins": "http://localhost:5173"}}) script_dir = os.path.dirname(os.path.abspath(__file__)) scaler_path = os.path.join(script_dir, 'scaler.pkl') @@ -15,22 +16,96 @@ with open(file_path, 'rb') as f: model = pkl.load(f) +# Configure logging +logging.basicConfig(level=logging.INFO, + format='%(asctime)s %(levelname)s %(message)s', + handlers=[ + logging.FileHandler("app.log"), + logging.StreamHandler() + ]) + +def generate_recommendations(Age, Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, Bmi, Dpf): + recommendations = [] + + if float(Glucose) > 140: + recommendations.append({ + 'metric': 'Glucose', + 'value': Glucose, + 'recommendation': 'Your glucose level is high. Reduce intake of sugary foods and beverages, and consider regular exercise to maintain a healthy glucose level.' + }) + else: + recommendations.append({ + 'metric': 'Glucose', + 'value': Glucose, + 'recommendation': 'Your glucose level is normal. Maintain a balanced diet to keep it steady.' + }) + + if float(Bmi) > 25: + recommendations.append({ + 'metric': 'BMI', + 'value': Bmi, + 'recommendation': 'Your BMI indicates that you are overweight. Incorporate a mix of cardio and strength training exercises and follow a balanced diet to reduce weight.' + }) + else: + recommendations.append({ + 'metric': 'BMI', + 'value': Bmi, + 'recommendation': 'Your BMI is in a healthy range. Continue maintaining a balanced diet and regular physical activity.' + }) + + if float(BloodPressure) > 130: + recommendations.append({ + 'metric': 'BloodPressure', + 'value': BloodPressure, + 'recommendation': 'Your blood pressure is high. Reduce salt intake, avoid alcohol, and engage in regular physical activity to manage blood pressure.' + }) + else: + recommendations.append({ + 'metric': 'BloodPressure', + 'value': BloodPressure, + 'recommendation': 'Your blood pressure is normal. Continue to monitor it regularly and maintain a healthy lifestyle.' + }) + + if float(Insulin) > 100: + recommendations.append({ + 'metric': 'Insulin', + 'value': Insulin, + 'recommendation': 'Your insulin level is high. Follow a diet low in refined carbs and sugars and engage in regular physical activity.' + }) + else: + recommendations.append({ + 'metric': 'Insulin', + 'value': Insulin, + 'recommendation': 'Your insulin level is normal. Maintain a healthy lifestyle to keep it stable.' + }) + + return recommendations + def predict(Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, Bmi, Dpf, Age): input_data = pd.DataFrame([[Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, Bmi, Dpf, Age]]) input_data = scaler.transform(input_data) - prediction = model.predict(input_data) + prediction = model.predict(input_data)[0] + + recommendations = generate_recommendations(Age, Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, Bmi, Dpf) if prediction == 1: result = { - 'prediction': "You have high chances of Diabetes! Please consult a Doctor", - 'gif_url': "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif" + 'prediction': "high", + 'message': "You have high chances of Diabetes! Please consult a Doctor", + 'gif_url': "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOTZlY2pwcDNtcnNhc2JwdDk4YnVqenRpcXl0OXFxdWRya3U0dmZ4aCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o6wrebnKWmvx4ZBio/giphy.gif", + 'more_info_url': "https://www.diabetes.org/diabetes", + 'recommendations': recommendations } else: result = { - 'prediction': "You have low chances of Diabetes. Please maintain a healthy life style", - 'gif_url': "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExd2txb242N3pkMmp0ODRiangydm9raDY5OHBhYmw1Y2NobjM0cGZtNSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/W1GG6RYUcWxoHl3jV9/giphy.gif" + 'prediction': "low", + 'message': "You have low chances of Diabetes. Please maintain a healthy lifestyle", + 'gif_url': "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExd2txb242N3pkMmp0ODRiangydm9raDY5OHBhYmw1Y2NobjM0cGZtNSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/W1GG6RYUcWxoHl3jV9/giphy.gif", + 'more_info_url': "https://www.cdc.gov/chronicdisease/resources/infographic/prevent-diabetes.htm", + 'recommendations': recommendations } - + logging.info("Prediction Result: %s", result) + print("Prediction Result:", result) # Adding print statement for debugging return result @app.route('/predict', methods=['POST']) @@ -46,10 +121,11 @@ def predictions(): SkinThickness = data.get('SkinThickness') Dpf = data.get('DPF') - result = predict(Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, Bmi, Dpf, Age) + result = predict(float(Pregnancies), float(Glucose), float(BloodPressure), float(SkinThickness), float(Insulin), float(Bmi), float(Dpf), float(Age)) return jsonify(result) return "Invalid request method" if __name__ == '__main__': - app.run(host='0.0.0.0', port=8000, debug=True) \ No newline at end of file + logging.basicConfig(level=logging.INFO) + app.run(host='0.0.0.0', port=8000, debug=True)