Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

app.py 2.6 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...