Testing and Running the API
Once you have containerized your FastAPI application and started the Docker container, you need to verify that the API is running correctly and returning predictions as expected. To run your Docker container, use a command like:
Replacing your_image_name with the name of your built image. This command maps port 8000 on your local machine to port 8000 inside the container, making the FastAPI app accessible at:
Testing the /predict endpoint can be done using command line tools like curl or by sending an HTTP request from Python. Always ensure your input data matches the expected format defined by your FastAPI model. For example, if your model expects a JSON payload with certain fields, your test requests should include those fields with appropriate sample values.
import requests
# Replace with the actual URL if running on a different host or port
url = "http://localhost:8000/predict"
# Example input data matching the expected schema of your FastAPI model
input_data = {
"feature1": 3.5,
"feature2": 1.2,
"feature3": 0.8
}
response = requests.post(url, json=input_data)
if response.status_code == 200:
print("Prediction:", response.json())
else:
print("Error:", response.status_code, response.text)
Warning: always validate input data and handle errors gracefully in production APIs. Never assume that clients will always send well-formed or expected data. Use FastAPI's validation features and implement clear error messages to help users and protect your service from unexpected input.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 6.25
Testing and Running the API
Scorri per mostrare il menu
Once you have containerized your FastAPI application and started the Docker container, you need to verify that the API is running correctly and returning predictions as expected. To run your Docker container, use a command like:
Replacing your_image_name with the name of your built image. This command maps port 8000 on your local machine to port 8000 inside the container, making the FastAPI app accessible at:
Testing the /predict endpoint can be done using command line tools like curl or by sending an HTTP request from Python. Always ensure your input data matches the expected format defined by your FastAPI model. For example, if your model expects a JSON payload with certain fields, your test requests should include those fields with appropriate sample values.
import requests
# Replace with the actual URL if running on a different host or port
url = "http://localhost:8000/predict"
# Example input data matching the expected schema of your FastAPI model
input_data = {
"feature1": 3.5,
"feature2": 1.2,
"feature3": 0.8
}
response = requests.post(url, json=input_data)
if response.status_code == 200:
print("Prediction:", response.json())
else:
print("Error:", response.status_code, response.text)
Warning: always validate input data and handle errors gracefully in production APIs. Never assume that clients will always send well-formed or expected data. Use FastAPI's validation features and implement clear error messages to help users and protect your service from unexpected input.
Grazie per i tuoi commenti!