Okay, you've built a Flask application that loads your trained machine learning model and defines an endpoint to serve predictions. That's a significant step! But how do you know if it actually works? Before you think about packaging this service or deploying it anywhere else, it's essential to test it right here on your local machine. This local testing cycle allows you to quickly find and fix problems with your API logic, data handling, or model integration.
Running your Flask application locally and sending test requests helps confirm several things:
/predict
route (or whatever you named it)?Catching errors at this stage is much faster and simpler than troubleshooting them after deployment.
First, ensure your Flask application is running. Open your terminal or command prompt, navigate to the directory containing your Flask application file (e.g., app.py
), and run it using Python:
python app.py
You should see output indicating the server has started, usually mentioning it's running on http://127.0.0.1:5000/
or http://localhost:5000/
. Keep this terminal window open; closing it will stop your server.
To interact with your running API, you need a way to send HTTP requests to it. Think of this like a specialized web browser that you control programmatically. Two common tools for this are curl
and the Python requests
library.
curl
(Command-Line)curl
is a versatile command-line tool for transferring data with URLs. It's available on most Linux and macOS systems, and can be installed on Windows.
To test your prediction endpoint (assuming it's at /predict
and expects a POST request with JSON data), you would use a command like this:
curl -X POST -H "Content-Type: application/json" \
-d '{"features": [1.5, 2.8, 0.5]}' \
http://127.0.0.1:5000/predict
Let's break down this command:
curl
: Invokes the tool.-X POST
: Specifies the HTTP method as POST. This is typically used when sending data to create or update a resource, or in our case, to submit data for a prediction.-H "Content-Type: application/json"
: Sets the Content-Type
header. This tells the Flask server that the data you're sending in the request body is formatted as JSON. Your Flask endpoint likely expects this.-d '{"features": [1.5, 2.8, 0.5]}'
: Provides the data payload (the body of the request). Replace the example {"features": [1.5, 2.8, 0.5]}
with valid JSON input that your specific model expects. Note the single quotes around the JSON string to prevent issues with shell interpretation of the double quotes inside the JSON.http://127.0.0.1:5000/predict
: The URL of your prediction endpoint. Make sure the address, port (5000
), and route (/predict
) match your running Flask application.If successful, curl
will print the response body sent back by your Flask application directly to your terminal.
requests
Alternatively, you can write a small Python script using the popular requests
library. This can be more convenient if your input data is complex or if you want to integrate API testing into a larger Python workflow.
First, make sure you have the library installed:
pip install requests
Then, you can create a script like test_api.py
:
import requests
import json
# The URL of your local Flask API endpoint
url = 'http://127.0.0.1:5000/predict'
# The input data for your model, in the format your API expects
# Make sure this matches the structure you handle in your Flask endpoint
input_data = {
'features': [1.5, 2.8, 0.5]
}
# Send the POST request with the JSON data
# The `json` parameter automatically sets the Content-Type header to application/json
try:
response = requests.post(url, json=input_data)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
# Print the status code and the response JSON
print(f"Status Code: {response.status_code}")
print("Response JSON:", response.json())
except requests.exceptions.RequestException as e:
print(f"Error sending request: {e}")
except json.JSONDecodeError:
print("Error: Response is not valid JSON")
print("Response text:", response.text)
Run this script from your terminal:
python test_api.py
This script constructs the input data as a Python dictionary, uses requests.post
to send it to the specified URL (automatically handling JSON serialization and setting the Content-Type
header), and then prints the status code and the JSON response received from the server.
Whether you use curl
or requests
, you need to check the output carefully:
200 OK
. If you see codes like 404 Not Found
, 400 Bad Request
, or 500 Internal Server Error
, it indicates a problem.{"prediction": 1}
or {"probabilities": [0.1, 0.9]}
). Verify that the structure and values match what you expect.If things don't work on the first try, don't worry. Here are common problems and how to approach them:
127.0.0.1:5000
)? Check the terminal where you launched app.py
./predict
correctly? Does it match the route defined in your Flask application (@app.route('/predict', ...)
). Is the HTTP method correct (e.g., sending a POST request to an endpoint expecting POST)?Content-Type: application/json
header (if using curl
)?'features'
)?app.py
) is running. Flask usually prints detailed error messages (a "traceback") there, which will point you to the line of code causing the problem. Common causes include:
.pkl
or .joblib
) couldn't be found or loaded.predict()
method failed (perhaps due to incorrect input shape or data types).By systematically testing your API locally, you gain confidence that your prediction service behaves as intended. Once you've verified it works reliably on your machine, you'll be better prepared for the next steps, such as packaging your application using Docker.
© 2025 ApX Machine Learning