Spaces:
Runtime error
Runtime error
Upload app/services/ml.py with huggingface_hub
Browse files- app/services/ml.py +201 -201
app/services/ml.py
CHANGED
|
@@ -1,201 +1,201 @@
|
|
| 1 |
-
from flask import Blueprint, request, jsonify
|
| 2 |
-
from app import limiter
|
| 3 |
-
from app.services.ml_service import MLService
|
| 4 |
-
|
| 5 |
-
ml_bp = Blueprint('ml', __name__)
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
@ml_bp.route('/recommend-crop', methods=['POST'])
|
| 9 |
-
@limiter.limit("30 per hour")
|
| 10 |
-
def recommend_crop():
|
| 11 |
-
"""Recommend crop based on soil and climate data."""
|
| 12 |
-
try:
|
| 13 |
-
data = request.get_json()
|
| 14 |
-
|
| 15 |
-
required_fields = ['n_value', 'p_value', 'k_value', 'temperature', 'humidity', 'ph', 'rainfall']
|
| 16 |
-
if not all(field in data for field in required_fields):
|
| 17 |
-
return jsonify({
|
| 18 |
-
'success': False,
|
| 19 |
-
'error': 'Missing required fields',
|
| 20 |
-
'required': required_fields
|
| 21 |
-
}), 400
|
| 22 |
-
|
| 23 |
-
prediction = MLService.recommend_crop(data)
|
| 24 |
-
|
| 25 |
-
return jsonify({
|
| 26 |
-
'success': True,
|
| 27 |
-
'recommended_crop': prediction
|
| 28 |
-
}), 200
|
| 29 |
-
|
| 30 |
-
except Exception as e:
|
| 31 |
-
return jsonify({
|
| 32 |
-
'success': False,
|
| 33 |
-
'error': 'Crop recommendation failed',
|
| 34 |
-
'message': str(e)
|
| 35 |
-
}), 500
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
@ml_bp.route('/predict-yield', methods=['POST'])
|
| 39 |
-
@limiter.limit("30 per hour")
|
| 40 |
-
def predict_yield():
|
| 41 |
-
"""Predict crop yield based on input parameters."""
|
| 42 |
-
try:
|
| 43 |
-
data = request.get_json()
|
| 44 |
-
|
| 45 |
-
required_fields = ['nitrogen', 'phosphorus', 'potassium', 'temperature', 'rainfall', 'ph']
|
| 46 |
-
if not all(field in data for field in required_fields):
|
| 47 |
-
return jsonify({
|
| 48 |
-
'success': False,
|
| 49 |
-
'error': 'Missing required fields',
|
| 50 |
-
'required': required_fields
|
| 51 |
-
}), 400
|
| 52 |
-
|
| 53 |
-
prediction = MLService.predict_yield(data)
|
| 54 |
-
|
| 55 |
-
return jsonify({
|
| 56 |
-
'success': True,
|
| 57 |
-
'predicted_yield_ton_ha': prediction
|
| 58 |
-
}), 200
|
| 59 |
-
|
| 60 |
-
except Exception as e:
|
| 61 |
-
return jsonify({
|
| 62 |
-
'success': False,
|
| 63 |
-
'error': 'Yield prediction failed',
|
| 64 |
-
'message': str(e)
|
| 65 |
-
}), 500
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
@ml_bp.route('/predict-yield-advanced', methods=['POST'])
|
| 69 |
-
@limiter.limit("30 per hour")
|
| 70 |
-
def predict_yield_advanced():
|
| 71 |
-
"""Predict crop yield with SHAP explanation."""
|
| 72 |
-
try:
|
| 73 |
-
data = request.get_json()
|
| 74 |
-
|
| 75 |
-
required_fields = ['nitrogen', 'phosphorus', 'potassium', 'temperature', 'rainfall', 'ph']
|
| 76 |
-
if not all(field in data for field in required_fields):
|
| 77 |
-
return jsonify({
|
| 78 |
-
'success': False,
|
| 79 |
-
'error': 'Missing required fields',
|
| 80 |
-
'required': required_fields
|
| 81 |
-
}), 400
|
| 82 |
-
|
| 83 |
-
result = MLService.predict_yield_advanced(data)
|
| 84 |
-
|
| 85 |
-
return jsonify({
|
| 86 |
-
'success': True,
|
| 87 |
-
'predicted_yield_ton_ha': result['predicted_yield_ton_ha'],
|
| 88 |
-
'feature_importances': result['feature_importances'],
|
| 89 |
-
'shap_values': result['shap_values'],
|
| 90 |
-
'base_value': result['base_value']
|
| 91 |
-
}), 200
|
| 92 |
-
|
| 93 |
-
except Exception as e:
|
| 94 |
-
return jsonify({
|
| 95 |
-
'success': False,
|
| 96 |
-
'error': 'Advanced yield prediction failed',
|
| 97 |
-
'message': str(e)
|
| 98 |
-
}), 500
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
@ml_bp.route('/generate-yield-plan', methods=['POST'])
|
| 102 |
-
@limiter.limit("30 per hour")
|
| 103 |
-
def generate_yield_plan():
|
| 104 |
-
"""Generate land condition plan based on target yield."""
|
| 105 |
-
try:
|
| 106 |
-
data = request.get_json()
|
| 107 |
-
|
| 108 |
-
if 'target_yield' not in data:
|
| 109 |
-
return jsonify({'success': False, 'error': 'Missing target_yield field'}), 400
|
| 110 |
-
|
| 111 |
-
target_yield = float(data.get('target_yield', 0))
|
| 112 |
-
if target_yield <= 0:
|
| 113 |
-
return jsonify({'success': False, 'error': 'Target yield must be greater than 0'}), 400
|
| 114 |
-
|
| 115 |
-
plan = MLService.generate_yield_plan(target_yield)
|
| 116 |
-
|
| 117 |
-
if not plan:
|
| 118 |
-
return jsonify({'success': False, 'error': 'No matching data found for target yield.'}), 404
|
| 119 |
-
|
| 120 |
-
return jsonify({'success': True, 'plan': plan}), 200
|
| 121 |
-
|
| 122 |
-
except Exception as e:
|
| 123 |
-
return jsonify({
|
| 124 |
-
'success': False,
|
| 125 |
-
'error': 'Yield plan generation failed',
|
| 126 |
-
'message': str(e)
|
| 127 |
-
}), 500
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
@ml_bp.route('/calculate-fertilizer-bags', methods=['POST'])
|
| 131 |
-
@limiter.limit("30 per hour")
|
| 132 |
-
def calculate_fertilizer_bags():
|
| 133 |
-
"""Calculate fertilizer bags needed for nutrient requirement."""
|
| 134 |
-
try:
|
| 135 |
-
data = request.get_json()
|
| 136 |
-
|
| 137 |
-
required_fields = ['nutrient_needed', 'nutrient_amount_kg', 'fertilizer_type']
|
| 138 |
-
if not all(field in data for field in required_fields):
|
| 139 |
-
return jsonify({
|
| 140 |
-
'success': False,
|
| 141 |
-
'error': 'Missing required fields',
|
| 142 |
-
'required': required_fields
|
| 143 |
-
}), 400
|
| 144 |
-
|
| 145 |
-
result = MLService.calculate_fertilizer_bags(
|
| 146 |
-
data['nutrient_needed'],
|
| 147 |
-
float(data['nutrient_amount_kg']),
|
| 148 |
-
data['fertilizer_type']
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
if not result:
|
| 152 |
-
return jsonify({
|
| 153 |
-
'success': False,
|
| 154 |
-
'error': 'Calculation failed. Check fertilizer type and nutrient.'
|
| 155 |
-
}), 400
|
| 156 |
-
|
| 157 |
-
return jsonify({
|
| 158 |
-
'success': True,
|
| 159 |
-
'required_fertilizer_kg': result['required_kg'],
|
| 160 |
-
'fertilizer_name': result['fertilizer_name'],
|
| 161 |
-
'nutrient_needed': result['nutrient_needed'],
|
| 162 |
-
'nutrient_amount_kg': result['nutrient_amount_kg']
|
| 163 |
-
}), 200
|
| 164 |
-
|
| 165 |
-
except Exception as e:
|
| 166 |
-
return jsonify({
|
| 167 |
-
'success': False,
|
| 168 |
-
'error': 'Calculation failed',
|
| 169 |
-
'message': str(e)
|
| 170 |
-
}), 500
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
@ml_bp.route('/predict-success', methods=['POST'])
|
| 174 |
-
@limiter.limit("30 per hour")
|
| 175 |
-
def predict_success():
|
| 176 |
-
"""Predict harvest success probability based on parameters."""
|
| 177 |
-
try:
|
| 178 |
-
data = request.get_json()
|
| 179 |
-
|
| 180 |
-
required_fields = ['nitrogen', 'phosphorus', 'potassium', 'temperature', 'rainfall', 'ph']
|
| 181 |
-
if not all(field in data for field in required_fields):
|
| 182 |
-
return jsonify({
|
| 183 |
-
'success': False,
|
| 184 |
-
'error': 'Missing required fields',
|
| 185 |
-
'required': required_fields
|
| 186 |
-
}), 400
|
| 187 |
-
|
| 188 |
-
result = MLService.predict_success(data)
|
| 189 |
-
|
| 190 |
-
return jsonify({
|
| 191 |
-
'success': True,
|
| 192 |
-
'status': result['status'],
|
| 193 |
-
'probability_of_success': result['probability_of_success']
|
| 194 |
-
}), 200
|
| 195 |
-
|
| 196 |
-
except Exception as e:
|
| 197 |
-
return jsonify({
|
| 198 |
-
'success': False,
|
| 199 |
-
'error': 'Success prediction failed',
|
| 200 |
-
'message': str(e)
|
| 201 |
-
}), 500
|
|
|
|
| 1 |
+
from flask import Blueprint, request, jsonify
|
| 2 |
+
from app import limiter
|
| 3 |
+
from app.services.ml_service import MLService
|
| 4 |
+
|
| 5 |
+
ml_bp = Blueprint('ml', __name__)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@ml_bp.route('/recommend-crop', methods=['POST'])
|
| 9 |
+
@limiter.limit("30 per hour")
|
| 10 |
+
def recommend_crop():
|
| 11 |
+
"""Recommend crop based on soil and climate data."""
|
| 12 |
+
try:
|
| 13 |
+
data = request.get_json()
|
| 14 |
+
|
| 15 |
+
required_fields = ['n_value', 'p_value', 'k_value', 'temperature', 'humidity', 'ph', 'rainfall']
|
| 16 |
+
if not all(field in data for field in required_fields):
|
| 17 |
+
return jsonify({
|
| 18 |
+
'success': False,
|
| 19 |
+
'error': 'Missing required fields',
|
| 20 |
+
'required': required_fields
|
| 21 |
+
}), 400
|
| 22 |
+
|
| 23 |
+
prediction = MLService.recommend_crop(data)
|
| 24 |
+
|
| 25 |
+
return jsonify({
|
| 26 |
+
'success': True,
|
| 27 |
+
'recommended_crop': prediction
|
| 28 |
+
}), 200
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return jsonify({
|
| 32 |
+
'success': False,
|
| 33 |
+
'error': 'Crop recommendation failed',
|
| 34 |
+
'message': str(e)
|
| 35 |
+
}), 500
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@ml_bp.route('/predict-yield', methods=['POST'])
|
| 39 |
+
@limiter.limit("30 per hour")
|
| 40 |
+
def predict_yield():
|
| 41 |
+
"""Predict crop yield based on input parameters."""
|
| 42 |
+
try:
|
| 43 |
+
data = request.get_json()
|
| 44 |
+
|
| 45 |
+
required_fields = ['nitrogen', 'phosphorus', 'potassium', 'temperature', 'rainfall', 'ph']
|
| 46 |
+
if not all(field in data for field in required_fields):
|
| 47 |
+
return jsonify({
|
| 48 |
+
'success': False,
|
| 49 |
+
'error': 'Missing required fields',
|
| 50 |
+
'required': required_fields
|
| 51 |
+
}), 400
|
| 52 |
+
|
| 53 |
+
prediction = MLService.predict_yield(data)
|
| 54 |
+
|
| 55 |
+
return jsonify({
|
| 56 |
+
'success': True,
|
| 57 |
+
'predicted_yield_ton_ha': prediction
|
| 58 |
+
}), 200
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return jsonify({
|
| 62 |
+
'success': False,
|
| 63 |
+
'error': 'Yield prediction failed',
|
| 64 |
+
'message': str(e)
|
| 65 |
+
}), 500
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@ml_bp.route('/predict-yield-advanced', methods=['POST'])
|
| 69 |
+
@limiter.limit("30 per hour")
|
| 70 |
+
def predict_yield_advanced():
|
| 71 |
+
"""Predict crop yield with SHAP explanation."""
|
| 72 |
+
try:
|
| 73 |
+
data = request.get_json()
|
| 74 |
+
|
| 75 |
+
required_fields = ['nitrogen', 'phosphorus', 'potassium', 'temperature', 'rainfall', 'ph']
|
| 76 |
+
if not all(field in data for field in required_fields):
|
| 77 |
+
return jsonify({
|
| 78 |
+
'success': False,
|
| 79 |
+
'error': 'Missing required fields',
|
| 80 |
+
'required': required_fields
|
| 81 |
+
}), 400
|
| 82 |
+
|
| 83 |
+
result = MLService.predict_yield_advanced(data)
|
| 84 |
+
|
| 85 |
+
return jsonify({
|
| 86 |
+
'success': True,
|
| 87 |
+
'predicted_yield_ton_ha': result['predicted_yield_ton_ha'],
|
| 88 |
+
'feature_importances': result['feature_importances'],
|
| 89 |
+
'shap_values': result['shap_values'],
|
| 90 |
+
'base_value': result['base_value']
|
| 91 |
+
}), 200
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return jsonify({
|
| 95 |
+
'success': False,
|
| 96 |
+
'error': 'Advanced yield prediction failed',
|
| 97 |
+
'message': str(e)
|
| 98 |
+
}), 500
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
@ml_bp.route('/generate-yield-plan', methods=['POST'])
|
| 102 |
+
@limiter.limit("30 per hour")
|
| 103 |
+
def generate_yield_plan():
|
| 104 |
+
"""Generate land condition plan based on target yield."""
|
| 105 |
+
try:
|
| 106 |
+
data = request.get_json()
|
| 107 |
+
|
| 108 |
+
if 'target_yield' not in data:
|
| 109 |
+
return jsonify({'success': False, 'error': 'Missing target_yield field'}), 400
|
| 110 |
+
|
| 111 |
+
target_yield = float(data.get('target_yield', 0))
|
| 112 |
+
if target_yield <= 0:
|
| 113 |
+
return jsonify({'success': False, 'error': 'Target yield must be greater than 0'}), 400
|
| 114 |
+
|
| 115 |
+
plan = MLService.generate_yield_plan(target_yield)
|
| 116 |
+
|
| 117 |
+
if not plan:
|
| 118 |
+
return jsonify({'success': False, 'error': 'No matching data found for target yield.'}), 404
|
| 119 |
+
|
| 120 |
+
return jsonify({'success': True, 'plan': plan}), 200
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
return jsonify({
|
| 124 |
+
'success': False,
|
| 125 |
+
'error': 'Yield plan generation failed',
|
| 126 |
+
'message': str(e)
|
| 127 |
+
}), 500
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
@ml_bp.route('/calculate-fertilizer-bags', methods=['POST'])
|
| 131 |
+
@limiter.limit("30 per hour")
|
| 132 |
+
def calculate_fertilizer_bags():
|
| 133 |
+
"""Calculate fertilizer bags needed for nutrient requirement."""
|
| 134 |
+
try:
|
| 135 |
+
data = request.get_json()
|
| 136 |
+
|
| 137 |
+
required_fields = ['nutrient_needed', 'nutrient_amount_kg', 'fertilizer_type']
|
| 138 |
+
if not all(field in data for field in required_fields):
|
| 139 |
+
return jsonify({
|
| 140 |
+
'success': False,
|
| 141 |
+
'error': 'Missing required fields',
|
| 142 |
+
'required': required_fields
|
| 143 |
+
}), 400
|
| 144 |
+
|
| 145 |
+
result = MLService.calculate_fertilizer_bags(
|
| 146 |
+
data['nutrient_needed'],
|
| 147 |
+
float(data['nutrient_amount_kg']),
|
| 148 |
+
data['fertilizer_type']
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
if not result:
|
| 152 |
+
return jsonify({
|
| 153 |
+
'success': False,
|
| 154 |
+
'error': 'Calculation failed. Check fertilizer type and nutrient.'
|
| 155 |
+
}), 400
|
| 156 |
+
|
| 157 |
+
return jsonify({
|
| 158 |
+
'success': True,
|
| 159 |
+
'required_fertilizer_kg': result['required_kg'],
|
| 160 |
+
'fertilizer_name': result['fertilizer_name'],
|
| 161 |
+
'nutrient_needed': result['nutrient_needed'],
|
| 162 |
+
'nutrient_amount_kg': result['nutrient_amount_kg']
|
| 163 |
+
}), 200
|
| 164 |
+
|
| 165 |
+
except Exception as e:
|
| 166 |
+
return jsonify({
|
| 167 |
+
'success': False,
|
| 168 |
+
'error': 'Calculation failed',
|
| 169 |
+
'message': str(e)
|
| 170 |
+
}), 500
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
@ml_bp.route('/predict-success', methods=['POST'])
|
| 174 |
+
@limiter.limit("30 per hour")
|
| 175 |
+
def predict_success():
|
| 176 |
+
"""Predict harvest success probability based on parameters."""
|
| 177 |
+
try:
|
| 178 |
+
data = request.get_json()
|
| 179 |
+
|
| 180 |
+
required_fields = ['nitrogen', 'phosphorus', 'potassium', 'temperature', 'rainfall', 'ph']
|
| 181 |
+
if not all(field in data for field in required_fields):
|
| 182 |
+
return jsonify({
|
| 183 |
+
'success': False,
|
| 184 |
+
'error': 'Missing required fields',
|
| 185 |
+
'required': required_fields
|
| 186 |
+
}), 400
|
| 187 |
+
|
| 188 |
+
result = MLService.predict_success(data)
|
| 189 |
+
|
| 190 |
+
return jsonify({
|
| 191 |
+
'success': True,
|
| 192 |
+
'status': result['status'],
|
| 193 |
+
'probability_of_success': result['probability_of_success']
|
| 194 |
+
}), 200
|
| 195 |
+
|
| 196 |
+
except Exception as e:
|
| 197 |
+
return jsonify({
|
| 198 |
+
'success': False,
|
| 199 |
+
'error': 'Success prediction failed',
|
| 200 |
+
'message': str(e)
|
| 201 |
+
}), 500
|