File size: 4,704 Bytes
f0bfa55 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
#!/usr/bin/env python3
"""
VitaMind AI Inference Example
"""
import tensorflow as tf
import joblib
import numpy as np
from huggingface_hub import hf_hub_download
class VitaMindPredictor:
def __init__(self, repo_id="YOUR_USERNAME/vitamind-calorie-predictor"):
print("Loading model from Hugging Face...")
# Download artifacts
self.model = tf.keras.models.load_model(
hf_hub_download(repo_id=repo_id, filename="model.keras")
)
self.scaler = joblib.load(
hf_hub_download(repo_id=repo_id, filename="scaler.joblib")
)
self.encoders = joblib.load(
hf_hub_download(repo_id=repo_id, filename="encoders.joblib")
)
self.activity_scores = {
'Sedentary': 1, 'Lightly Active': 2, 'Active': 3,
'Very Active': 4, 'Athlete': 5
}
print("✓ Model loaded successfully!")
def predict(self, user_data):
"""
Predict daily calorie goal
Args:
user_data (dict): User information with keys:
age, weight, height, steps, heart_rate, sleep_hours,
stress_level, activity_level, gender, mood
Returns:
dict: Prediction results
"""
# Validate inputs
required_keys = ['age', 'weight', 'height', 'steps', 'heart_rate',
'sleep_hours', 'stress_level', 'activity_level',
'gender', 'mood']
for key in required_keys:
if key not in user_data:
raise ValueError(f"Missing required key: {key}")
# Feature engineering
bmi = user_data['weight'] / ((user_data['height'] / 100) ** 2)
good_sleep = 1 if user_data['sleep_hours'] >= 7 else 0
high_stress = 1 if user_data['stress_level'] >= 7 else 0
activity_score = self.activity_scores[user_data['activity_level']]
# Encoding
activity_encoded = self.encoders['activity_level'].transform(
[user_data['activity_level']]
)[0]
gender_encoded = self.encoders['gender'].transform(
[user_data['gender']]
)[0]
mood_encoded = self.encoders['mood'].transform(
[user_data['mood']]
)[0]
# Create feature vector
features = np.array([[
user_data['age'],
user_data['weight'],
user_data['height'],
user_data['steps'],
user_data['heart_rate'],
user_data['sleep_hours'],
user_data['stress_level'],
bmi,
activity_encoded,
gender_encoded,
mood_encoded,
good_sleep,
high_stress,
activity_score
]])
# Predict
features_scaled = self.scaler.transform(features)
calories = self.model.predict(features_scaled, verbose=0)[0][0]
return {
'calories': round(float(calories), 0),
'bmi': round(bmi, 1),
'bmi_category': self._get_bmi_category(bmi),
'activity_score': activity_score,
'sleep_quality': 'Good' if good_sleep else 'Poor',
'stress_level': 'High' if high_stress else 'Normal'
}
def _get_bmi_category(self, bmi):
if bmi < 18.5:
return 'Underweight'
elif bmi < 25:
return 'Normal'
elif bmi < 30:
return 'Overweight'
else:
return 'Obese'
# Example usage
if __name__ == "__main__":
# Initialize predictor
predictor = VitaMindPredictor(repo_id="YOUR_USERNAME/vitamind-calorie-predictor")
# Example user data
user = {
'age': 28,
'weight': 70, # kg
'height': 170, # cm
'steps': 10000,
'heart_rate': 75,
'sleep_hours': 8,
'stress_level': 3,
'activity_level': 'Active',
'gender': 'M',
'mood': 'happy'
}
# Make prediction
result = predictor.predict(user)
# Display results
print("\n" + "="*50)
print("VitaMind AI - Calorie Recommendation")
print("="*50)
print(f"Daily Calorie Goal: {result['calories']} kcal")
print(f"BMI: {result['bmi']} ({result['bmi_category']})")
print(f"Activity Score: {result['activity_score']}/5")
print(f"Sleep Quality: {result['sleep_quality']}")
print(f"Stress Level: {result['stress_level']}")
print("="*50)
|