|
| 1 | +from flask import Flask, request, render_template, jsonify |
| 2 | +import joblib |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +app = Flask(__name__) |
| 6 | + |
| 7 | +# Load model on startup |
| 8 | +try: |
| 9 | + model = joblib.load("artifacts/model_trainer/model.joblib") |
| 10 | + print("Model loaded successfully") |
| 11 | +except: |
| 12 | + model = None |
| 13 | + print("Model not found - using fallback") |
| 14 | + |
| 15 | +def prepare_features(sex, age, height, weight, duration, heart_rate, body_temp): |
| 16 | + """Prepare 11 features for model prediction""" |
| 17 | + sex_numeric = 1 if sex.lower() == 'male' else 0 |
| 18 | + bmi = weight / ((height / 100) ** 2) |
| 19 | + met_estimate = (heart_rate - 60) / 20 + 1 |
| 20 | + estimated_calories_per_min = met_estimate * weight * 3.5 / 200 |
| 21 | + age_weight = age * weight |
| 22 | + heart_temp = heart_rate * body_temp |
| 23 | + |
| 24 | + return np.array([[sex_numeric, age, height, weight, duration, heart_rate, |
| 25 | + body_temp, bmi, estimated_calories_per_min, age_weight, heart_temp]]) |
| 26 | + |
| 27 | +def fallback_calories(sex, age, height, weight, duration, heart_rate): |
| 28 | + """Simple fallback calculation""" |
| 29 | + if sex == 'male': |
| 30 | + bmr = 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age) |
| 31 | + else: |
| 32 | + bmr = 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age) |
| 33 | + |
| 34 | + met = 6.0 if heart_rate < 140 else 8.0 |
| 35 | + calories_per_minute = (met * weight * 3.5) / 200 |
| 36 | + return round(calories_per_minute * duration, 2) |
| 37 | + |
| 38 | +@app.route('/') |
| 39 | +def home(): |
| 40 | + return render_template('index.html') |
| 41 | + |
| 42 | +@app.route('/predict', methods=['POST']) |
| 43 | +def predict(): |
| 44 | + try: |
| 45 | + # Get form data |
| 46 | + sex = request.form.get('sex', '').lower() |
| 47 | + age = float(request.form.get('age', 0)) |
| 48 | + height = float(request.form.get('height', 0)) |
| 49 | + weight = float(request.form.get('weight', 0)) |
| 50 | + duration = float(request.form.get('duration', 0)) |
| 51 | + heart_rate = float(request.form.get('heart_rate', 0)) |
| 52 | + body_temp = float(request.form.get('body_temp', 0)) |
| 53 | + |
| 54 | + # Prepare features and predict |
| 55 | + features = prepare_features(sex, age, height, weight, duration, heart_rate, body_temp) |
| 56 | + |
| 57 | + if model is not None: |
| 58 | + prediction = model.predict(features)[0] |
| 59 | + prediction = max(0, round(prediction, 2)) |
| 60 | + else: |
| 61 | + prediction = fallback_calories(sex, age, height, weight, duration, heart_rate) |
| 62 | + |
| 63 | + return jsonify({'success': True, 'prediction': prediction}) |
| 64 | + |
| 65 | + except Exception as e: |
| 66 | + return jsonify({'success': False, 'error': str(e)}), 400 |
| 67 | + |
| 68 | +@app.route('/health') |
| 69 | +def health(): |
| 70 | + return jsonify({'status': 'healthy', 'model_loaded': model is not None}) |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + app.run(host='0.0.0.0', port=5000, debug=False) |
0 commit comments