Spaces:
Sleeping
Sleeping
Otabek Najimov commited on
Commit ·
b6ace6a
1
Parent(s): 4474009
Add health check endpoint and improve error handling in prediction API
Browse files- Dockerfile +1 -1
- app.py +16 -2
Dockerfile
CHANGED
|
@@ -9,4 +9,4 @@ COPY ./label_encoder.pkl /code/label_encoder.pkl
|
|
| 9 |
|
| 10 |
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 11 |
|
| 12 |
-
CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
|
|
|
|
| 9 |
|
| 10 |
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 11 |
|
| 12 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--timeout", "180", "app:app"]
|
app.py
CHANGED
|
@@ -30,9 +30,21 @@ def predict(input_data):
|
|
| 30 |
return decoded_predictions[0]
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
@app.route('/predict', methods=['POST'])
|
| 34 |
def get_prediction():
|
| 35 |
"""Receive input data, preprocess, and return crop prediction."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
print("here")
|
| 38 |
# Get the input JSON data from the request
|
|
@@ -50,8 +62,10 @@ def get_prediction():
|
|
| 50 |
# Return the prediction as a JSON response
|
| 51 |
return jsonify({'crop': predicted_label})
|
| 52 |
except Exception as e:
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
if __name__ == '__main__':
|
| 57 |
-
|
|
|
|
|
|
| 30 |
return decoded_predictions[0]
|
| 31 |
|
| 32 |
|
| 33 |
+
@app.route('/', methods=['GET'])
|
| 34 |
+
def health_check():
|
| 35 |
+
"""Health check endpoint."""
|
| 36 |
+
return jsonify({'status': 'healthy', 'message': 'CropSmartAI API is running'})
|
| 37 |
+
|
| 38 |
@app.route('/predict', methods=['POST'])
|
| 39 |
def get_prediction():
|
| 40 |
"""Receive input data, preprocess, and return crop prediction."""
|
| 41 |
+
if not request.json:
|
| 42 |
+
return jsonify({'error': 'No input data provided'}), 400
|
| 43 |
+
|
| 44 |
+
required_fields = ['N', 'P', 'K', 'temperature', 'humidity', 'ph', 'rainfall']
|
| 45 |
+
if not all(field in request.json for field in required_fields):
|
| 46 |
+
return jsonify({'error': 'Missing required fields'}), 400
|
| 47 |
+
|
| 48 |
try:
|
| 49 |
print("here")
|
| 50 |
# Get the input JSON data from the request
|
|
|
|
| 62 |
# Return the prediction as a JSON response
|
| 63 |
return jsonify({'crop': predicted_label})
|
| 64 |
except Exception as e:
|
| 65 |
+
response = jsonify({'error': f'Prediction error: {str(e)}'})
|
| 66 |
+
return response, 500
|
| 67 |
|
| 68 |
|
| 69 |
if __name__ == '__main__':
|
| 70 |
+
# Update to use port 7860 to match Hugging Face Spaces requirements
|
| 71 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|