Download Cattle Breed Classification/test_client.py from MLCoder90/Cattle_Breed_Recognition: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/MLCoder90/Cattle_Breed_Recognition/resolve/main/Cattle%20Breed%20Classification/test_client.py
- Command line
-
hf download 'hf://MLCoder90/Cattle_Breed_Recognition/Cattle Breed Classification/test_client.py'
-
curl -L -o test_client.py https://huggingface.co/MLCoder90/Cattle_Breed_Recognition/resolve/main/Cattle%20Breed%20Classification/test_client.py
1.32 kB
| """ | |
| Simple test client for the Cattle Breed Classifier API. | |
| Usage: | |
| python test_client.py path/to/image.jpg | |
| python test_client.py path/to/image.jpg --url http://localhost:8000 | |
| """ | |
| import sys | |
| import argparse | |
| import requests | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Test the Cattle Breed Classifier API") | |
| parser.add_argument("image_path", help="Path to an image file") | |
| parser.add_argument("--url", default="http://localhost:8000", help="Base URL of the API") | |
| args = parser.parse_args() | |
| # Health check first | |
| health_resp = requests.get(f"{args.url}/health") | |
| print("Health:", health_resp.json()) | |
| with open(args.image_path, "rb") as f: | |
| files = {"file": (args.image_path, f, "image/jpeg")} | |
| resp = requests.post(f"{args.url}/predict", files=files) | |
| if resp.status_code != 200: | |
| print(f"Error {resp.status_code}: {resp.text}") | |
| sys.exit(1) | |
| result = resp.json() | |
| print(f"\nPredicted label: {result['predicted_label']}") | |
| print(f"Confidence: {result['confidence']:.4f}\n") | |
| print("Top 5 probabilities:") | |
| sorted_probs = sorted(result["probabilities"].items(), key=lambda x: x[1], reverse=True) | |
| for label, prob in sorted_probs[:5]: | |
| print(f" {label:25s} {prob:.4f}") | |
| if __name__ == "__main__": | |
| main() | |