A python flask rest api that use a trained tensorflow keras model from AWS S3 for predicting disease , deployed on AWS EC2 Ubuntu instance with NGINX . Application Performance Monitoring done by Elastic APM through elastic apm agent configured on the flask app.
Take a look at the right side of the block diagram
- Endpoint:
/predict
- Method:
POST
- Description: Predicts the disease in a plant image.
- Request Body:
- Form data with key
file
containing the plant image file.
- Form data with key
- Response:
- If successful, returns a JSON object with the predicted disease class.
- If unsuccessful, returns an error message.
- Endpoint:
/
- Method:
GET
- Description: A simple health check endpoint to verify if the API is running.
- Response: Returns a welcome message indicating that the API is operational.
- If any errors occur during the prediction process, appropriate error messages are returned along with HTTP status codes.
import requests
url = 'https://ec2_instance_ip_or_domain/predict'
files = {'file': open('plant_image.jpg', 'rb')}
response = requests.post(url, files=files)
print(response.json())
- Clone the repository or download the source code.
- Install required Python packages using
pip install -r requirements.txt
. - Set up AWS S3 bucket with the trained model file.
- Configure NGINX to forward requests to the Flask app.
- Set up Elastic APM and configure the Flask application with the Elastic APM agent.
- Obtain an SSL/TLS certificate using Python Certbot for securing communications.
- Assign a domain name to your AWS EC2 instance.
- Implement CORS to allow requests only from the Floravision frontend.
- Ensure that the AWS credentials with access to the S3 bucket are properly configured.
- Update the
download_model_from_s3
function with the appropriate S3 bucket name and model file name.
- Configure NGINX to forward requests to the Flask app running on a port.
- Set up SSL/TLS certificate obtained from Python Certbot for secure communicatio
- Set up an Elastic APM instance and obtain the service name, secret token, and server URL.
- Update the
app.config['ELASTIC_APM']
dictionary with the obtained credentials.
- Implement CORS to only allow requests from the Floravision frontend. Update the CORS configuration in the Flask app accordingly.
- Flask
- Flask-CORS
- ElasticAPM
- TensorFlow 2.11
- Boto3