Commit 0f94abec authored by Yomal-Dulanjana's avatar Yomal-Dulanjana

ML Update

parent 94b8b3ac
from inference import *
from flask_cors import CORS
from flask import Flask, Response, request
from flask import Flask, Response, jsonify, request
from pymongo import MongoClient
import threading
import requests
import schedule
# import schedule
import time
import random
from datetime import datetime
from mongodb_api import *
app = Flask(__name__)
CORS(app)
def risk():
risk_level, HeartRateArr, BloodOxygenLevelArr, BodyTemperatureArr = risk_level_prediction()
if risk_level == "Risky":
requests.get('http://localhost/api/risk-alert')
print(risk_level)
def diwo():
exercise_plan, diet_plan, HeartRateArr, BloodOxygenLevelArr, BodyTemperatureArr = diet_workout_prediction()
print(diet_plan)
def update_risk_level():
while True:
risk()
time.sleep(120)
def update_diet_plan():
# schedule.every().day.at("19:05:00").do(diwo)
while True:
diwo()
# schedule.run_pending()
time.sleep(60)
thread1 = threading.Thread(target=update_risk_level)
thread1.start()
print("test")
thread2 = threading.Thread(target=update_diet_plan)
thread2.start()
print("test")
@app.route('/inference', methods=['POST'])
def perform_inference():
data = request.json.get('data')
data = json.loads(data)
print(data)
api_data = data
# Perform inference and get the result
result = get_Android_data(api_data)
print(result)
if result == "Patient has dementia":
# Return a specific response for dementia case
return jsonify({'message': 'This patient has dementia.'})
else:
# Return a specific response for non-dementia case
return jsonify({'message': 'This patient does not have dementia.'})
@app.route('/record', methods=['POST'])
def record():
......@@ -35,8 +80,8 @@ def record():
spo2 = data['spo2']
# temperature = random.uniform(29, 38)
# temperature = round(temperature, 2)
# bpm = random.randint(60, 100)
# spo2 = random.randint(80, 110)
bpm = random.randint(60, 100)
spo2 = random.randint(80, 110)
print(temperature, bpm, spo2)
health_collection = db['health']
health_document = {
......@@ -65,6 +110,3 @@ if __name__ == "__main__":
threaded=False,
use_reloader=True
)
import json
import pickle
import numpy as np
import pandas as pd
import tensorflow as tf
from math import sin, cos, sqrt, atan2, radians, asin
from pymongo import MongoClient
from datetime import datetime
try:
client = MongoClient(
"mongodb+srv://eshan:78NCxoxRgTBQAVyg@health.7yovxrt.mongodb.net/?retryWrites=true&w=majority")
# client = MongoClient(
# "mongodb+srv://admin:admin@early-detection.jevtz.mongodb.net/")
db = client["Heal"]
client.server_info()
print("DB accessed !")
except Exception as e:
print("*************************************************************")
print(e)
print("*************************************************************")
##################################### PARAMS #########################################
risk_level_mapping = {
0: "situation doesn't exist",
1: "Manageable",
2: "Normal Healthy",
3: "Risky"
}
excercise_plan_mapping = {
1: '''{"plan": "No exercises", "exercise": "Reading,Solving puzzles,Art and Craft,Mindful Relaxation"}''',
0: '''{"plan": "Low Risk Exercises", "exercises": " Walking,Seated Leg Lifts,Wall Push-Ups,Neck Stretches,Deep Breathing Exercises"}''',
2: '''{"plan": "Low to Moderate Risk Exercises", "exercises": "Tai Chi,Water Aerobics,Yoga,Stationary Biking,Resistance Band Exercises"}''',
3: '''{"plan": "Moderate to High Risk Exercises", "exercises": "Extreme Heat Yoga,High-Impact Sports,Advanced Martial Arts,High-Intensity Dance Workouts,Rapid or Excessive Stretching,Acrobatics"}'''
}
diet_plan_mapping = {
1: '''{"plan": "No food plan"}''',
0: '''{"plan": "Low Cholesterol Food", "breakfast": "Vegetable roti (flatbread filled with a mix of grated vegetables) with a side of fresh fruit. Egg hoppers (appa) with a side of tomato and cucumber salad. Rava upma (semolina cooked with vegetables) with a side of fresh fruit. Oatmeal topped with sliced almonds and fresh berries.", "lunch": "Grilled fish curry with tempered green beans and steamed red rice. Lentil curry (parippu) with tempered spinach and brown rice. Sambar (lentil and vegetable stew) with brown rice and a cucumber salad. Grilled vegetable wrap with whole wheat tortilla, hummus, and avocado.", "dinner": "Stir-fried chicken and vegetable curry with quinoa. Grilled shrimp skewers with mixed vegetable stir-fry and red rice. Millet dosa with tomato chutney and a mixed vegetable curry. Baked chicken breast with quinoa and steamed asparagus"}''',
2: '''{"plan": "Balanced Diet with All Nutrients", "breakfast": "Coconut milk rice (kiribath) with lunu miris (spicy onion sambol) and boiled eggs. String hoppers (idiyappam) with dhal curry and coconut chutney. Greek yogurt with granola, banana slices, and a drizzle of honey. Idli (steamed rice cakes) with coconut chutney and sambar.", "lunch": "Chicken curry with a side of mixed vegetable curry and quinoa. Fish ambul thiyal (sour fish curry) with red rice and gotu kola salad. Quinoa salad with mixed vegetables (cucumbers, tomatoes, bell peppers) and a lemon-tahini dressing. Vegetable biryani with cucumber raita and a side of mixed vegetable kurma", "dinner": "Eggplant curry with coconut roti and a cucumber and tomato salad Mixed vegetable kottu roti with a side of chicken curry. Channa masala (spiced chickpea curry) with roti and a spinach salad. Grilled lean steak with roasted potatoes, sautéed kale, and a side of mixed beans."}''',
3: '''{"plan": "Hot Beverages & Meals Rich in Potassium", "breakfast": "Spiced tea (Ceylon tea with spices like cinnamon and cardamom) with string hoppers and coconut sambol. Coconut water with hopper pancakes (appa) and lunu miris (spicy onion sambol). Banana and spinach smoothie with almond milk, chia seeds, and a pinch of cinnamon. Masala chai with medu vada (lentil fritters) and coconut chutney.", "lunch": "Jackfruit curry with basmati rice and a side of tempered spinach. Chicken curry with potato and green bean curry, served with red rice. Lentil soup with carrots, celery, and a squeeze of lemon juice Avial (mixed vegetable curry in coconut gravy) with adai (mixed lentil dosa) and a beetroot poriyal.", "dinner": " Lentil soup with pumpkin and a side of papadums Beetroot curry with coconut milk rice(kiribath) and a cucumber salad. Baked cod with steamed brown rice, roasted Brussels sprouts, and a sprinkle of sesame seeds. Tomato rasam(spicy tomato soup) with lemon rice and a spinach poriyal"}'''
}
##################################### LOAD MODELS #########################################
risk_model = tf.keras.models.load_model('weights/risk_prediction.h5')
risk_model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=[
tf.keras.metrics.CategoricalAccuracy(),
tf.keras.metrics.Precision(),
tf.keras.metrics.Recall(),
tf.keras.metrics.AUC()
])
with open('weights/scalar - risk_prediction.pkl', 'rb') as f:
risk_scalar = pickle.load(f)
diet_workout_model = tf.keras.models.load_model(
'weights/diet_workout_prediction.h5')
diet_workout_model.compile(
loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
with open('weights/scalar - diet_workout_prediction.pkl', 'rb') as f:
diet_workout_scalar = pickle.load(f)
##################################### PREDICTION #########################################
def risk_level_prediction():
health_collection = db["health"]
health_data = pd.DataFrame(list(health_collection.find()))
sample_data = health_data[-50:][["HeartRate",
"BloodOxygenLevel", "BodyTemperature"]]
# sample_data = health_data.sample(
# frac=50/len(health_data))[["HeartRate", "BloodOxygenLevel", "BodyTemperature"]]
HeartRateArr = sample_data["HeartRate"].values.tolist()
BloodOxygenLevelArr = sample_data["BloodOxygenLevel"].values.tolist()
BodyTemperatureArr = sample_data["BodyTemperature"].values.tolist()
Xinf = sample_data.values
Xinf = risk_scalar.transform(Xinf)
Xinf = Xinf.reshape(1, 50, 3)
risk_level = risk_model.predict(Xinf).squeeze()
risk_level = np.argmax(risk_level)
risk_level = risk_level_mapping[risk_level]
risk_level_collection = db['risk_level']
risk_level_document = {
"risk_level": risk_level,
"created_at": datetime.now(),
"updated_at": datetime.now()
}
risk_level_collection.insert_one(risk_level_document)
return risk_level, HeartRateArr, BloodOxygenLevelArr, BodyTemperatureArr
def diet_workout_prediction():
health_collection = db["health"]
health_data = pd.DataFrame(list(health_collection.find()))
sample_data = health_data[-50:][["HeartRate",
"BloodOxygenLevel", "BodyTemperature"]]
HeartRateArr = sample_data["HeartRate"].values.tolist()
BloodOxygenLevelArr = sample_data["BloodOxygenLevel"].values.tolist()
BodyTemperatureArr = sample_data["BodyTemperature"].values.tolist()
Xinf = sample_data.values
Xinf = diet_workout_scalar.transform(Xinf)
exercise_plan_idxs, diet_plan_idxs = diet_workout_model.predict(Xinf)
exercise_plan_idxs = exercise_plan_idxs.squeeze()
diet_plan_idxs = diet_plan_idxs.squeeze()
exercise_plan_idxs = np.argmax(exercise_plan_idxs, axis=1)
diet_plan_idxs = np.argmax(diet_plan_idxs, axis=1)
exercise_plans = [excercise_plan_mapping[idx]
for idx in exercise_plan_idxs]
diet_plans = [diet_plan_mapping[idx] for idx in diet_plan_idxs]
mode_exercise_plan = max(set(exercise_plans), key=exercise_plans.count)
mode_diet_plan = max(set(diet_plans), key=diet_plans.count)
diet_plan_collection = db['diet_plan']
diet_plan_document = {
"diet_plan": mode_diet_plan,
"created_at": datetime.now(),
"updated_at": datetime.now()
}
diet_plan_collection.insert_one(diet_plan_document)
exercise_plan_collection = db['exercise_plan']
exercise_plan_document = {
"exercise_plan": mode_exercise_plan,
"created_at": datetime.now(),
"updated_at": datetime.now()
}
exercise_plan_collection.insert_one(exercise_plan_document)
return mode_exercise_plan, mode_diet_plan, HeartRateArr, BloodOxygenLevelArr, BodyTemperatureArr
def haversine(p1, p2):
lat1, lon1 = p1
lat2, lon2 = p2
lat1 = float(lat1)
lon1 = float(lon1)
lat2 = float(lat2)
lon2 = float(lon2)
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
# Radius of earth in kilometers. Use 3956 for miles. Determines return value units.
r = 6371
distance_km = c * r
return distance_km
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
import numpy as np
import pickle
import tensorflow as tf
import pandas as pd
import pymongo
from bson import ObjectId
# Load the trained model and scaler
model = tf.keras.models.load_model('weights/health_data_dementia.h5')
with open('weights/scalar_health_data.pkl', 'rb') as f:
scalar = pickle.load(f)
# Function to process new data and make predictions
def predict_dementia(new_data):
# Preprocess the new data
new_data = scalar.transform(new_data.reshape(1, -1))
# Reshape the data to match the model's input shape
n_steps, n_features = model.input_shape[1:]
X = np.zeros((1, n_steps, n_features))
X[0] = new_data
# Make predictions
predictions = model.predict(X)
# Check the prediction
if predictions[0][0] >= 0.5:
result = "Patient has dementia"
else:
result = "Patient doesn't have dementia"
return result
# Set up MongoDB connection
try:
client = pymongo.MongoClient("mongodb+srv://ITP:89DbKo5LYRogaw96@cluster0.3agof.mongodb.net/?retryWrites=true&w=majority")
db = client["Heal"]
print("DB accessed!")
except Exception as e:
print("Error while connecting to MongoDB:", e)
exit() # Exit the script if there's a connection error
# Create a collection for user data
user_data_collection = db["health"]
# Function to retrieve user data from MongoDB
def get_latest_user_data():
try:
# Sort the documents in descending order by _id (assuming _id is a timestamp)
latest_user_data = user_data_collection.find().sort([("_id", pymongo.DESCENDING)]).limit(1)
if latest_user_data:
return latest_user_data[0]
else:
return None
except Exception as e:
print("Error while retrieving user data:", e)
return None
latest_user_data = get_latest_user_data()
if latest_user_data:
for key, value in latest_user_data.items():
print(f"{key}: {value}")
else:
print("User data not found.")
# Function to create a new array
def get_Android_data(api_data):
if latest_user_data is not None and api_data is not None:
# Create an array with the required features
new_data = np.array([api_data["Diabetic"], api_data["Alcohol_Level"], api_data["Weight"], api_data["MRI_Delay"],
latest_user_data["HeartRate"], latest_user_data["BloodOxygenLevel"], latest_user_data["BodyTemperature"]])
result = predict_dementia(new_data)
return result
else:
print("User data not found or API data is missing.")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment