Spaces:
Running
Running
| """ | |
| edge_client.py — Lightweight Edge Client for PV Defect Classifier API | |
| ====================================================================== | |
| Simulates an edge device (e.g. Raspberry Pi, industrial controller) sending | |
| a PV cell image to the cloud inference API and printing the result. | |
| Usage: | |
| python edge_client.py --image path/to/cell.png | |
| python edge_client.py --image path/to/cell.png --host https://hakimi233-pv-classifier.hf.space | |
| """ | |
| import argparse | |
| import sys | |
| import time | |
| import requests | |
| DEFAULT_HOST = "https://hakimi233-pv-classifier.hf.space" | |
| def classify(image_path: str, host: str) -> None: | |
| url = f"{host.rstrip('/')}/predict" | |
| with open(image_path, "rb") as f: | |
| files = {"file": (image_path, f, "image/jpeg")} | |
| print(f"[edge_client] Sending: {image_path}") | |
| print(f"[edge_client] Endpoint: {url}") | |
| t0 = time.time() | |
| response = requests.post(url, files=files, timeout=30) | |
| round_trip_ms = (time.time() - t0) * 1000 | |
| if response.status_code != 200: | |
| print(f"[edge_client] ERROR: HTTP {response.status_code}") | |
| print(response.text) | |
| sys.exit(1) | |
| data = response.json() | |
| if "error" in data: | |
| print(f"[edge_client] API error: {data['error']}") | |
| sys.exit(1) | |
| print("\n========== Prediction Result ==========") | |
| print(f" Prediction : {data['prediction']}") | |
| print(f" Confidence : {data['confidence']}%") | |
| print(f" Inference : {data['latency_ms']} ms (server-side)") | |
| print(f" Round-trip : {round_trip_ms:.1f} ms (including network)") | |
| print(f" Network OH : {round_trip_ms - data['latency_ms']:.1f} ms") | |
| print("=======================================\n") | |
| for cls, pct in data.get("probabilities", {}).items(): | |
| bar = "#" * int(pct / 5) | |
| print(f" {cls:<12} {bar:<20} {pct}%") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Edge client for PV Defect Classifier") | |
| parser.add_argument("--image", required=True, help="Path to PV cell image") | |
| parser.add_argument("--host", default=DEFAULT_HOST, help="API host URL") | |
| args = parser.parse_args() | |
| classify(args.image, args.host) | |