Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing Personalized Recommendations on the basis of results #20 #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions client/src/components/Prediction.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -34,14 +34,14 @@ const Prediction = () => {

useEffect(() => {
window.scrollTo(0, 0);
});
}, []);

const handleChange = (e) => {
setUserInput({ ...userInput, [e.target.name]: e.target.value });
};

return (
<div className="min-h-screen bg-gradient-to-r from-purple-500 to-pink-500 flex flex-col items-center justify-center sm:flex-row sm:justify-center pb-3 md:pb-0">
<div className="min-h-screen bg-gradient-to-r from-purple-600 to-pink-600 flex flex-col items-center justify-center sm:flex-row sm:justify-center pb-3 md:pb-0">
<motion.div
initial={{ opacity: 0, x: -150 }}
whileInView={{ opacity: 1, x: 0 }}
Expand Down Expand Up @@ -209,7 +209,7 @@ const Prediction = () => {
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 && (
<div className="hidden sm:block">
Expand All @@ -221,15 +221,28 @@ const Prediction = () => {
</div>
)}
{prediction && (
<div className="bg-green-100 w-2/3 md:w-fit border mx-auto flex flex-col gap-5 border-green-400 text-green-700 py-2 rounded md:mr-4 text-center">

<div className="bg-green-100 w-full w-2/3 md:w-fit border mx-auto flex flex-col gap-5 border-green-400 py-2 rounded md:mr-4 text-center">
<img
src={prediction.gif_url}
src={prediction.gif_url || ""}
alt="Prediction GIF"
className="mx-auto pt-4 rounded-md"
/>
<p className="font-bold text-2xl px-3 mx-2 ">
{prediction.prediction}
</p>
<h3 className="font-bold text-2xl text-black-800">
{prediction.message || "No message"}
</h3>
<div className="px-6 mx-4 text-left text-green-800">
{prediction.recommendations && Array.isArray(prediction.recommendations) && prediction.recommendations.map((rec, index) => (
<p key={index} className="text-lg font-medium mb-2">
• {rec.recommendation}
</p>
))}
</div>
<a href={prediction.more_info_url} target="_blank" rel="noopener noreferrer">
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full mt-4">
Click here for more information
</button>
</a>
</div>
)}
</motion.div>
Expand Down
29 changes: 29 additions & 0 deletions server/app.log
Original file line number Diff line number Diff line change
@@ -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 -
94 changes: 85 additions & 9 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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'])
Expand All @@ -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)
logging.basicConfig(level=logging.INFO)
app.run(host='0.0.0.0', port=8000, debug=True)