vehicle-damage-classifier / src /step10_demo_request.py
efnanaladagg's picture
Clean push
6f6eb85
import sys
import requests
import mimetypes
import os
def main():
if len(sys.argv) < 2:
print("Usage: python src/step10_demo_request.py path/to/image.jpg")
sys.exit(1)
img_path = sys.argv[1]
url = "http://127.0.0.1:8000/predict"
mime_type, _ = mimetypes.guess_type(img_path)
if mime_type is None:
mime_type = "application/octet-stream"
print(f"Sending: {img_path} -> {url} (mime: {mime_type})")
try:
with open(img_path, "rb") as f:
files = {"file": (os.path.basename(img_path), f, mime_type)}
r = requests.post(url, files=files, timeout=60)
print("Request sent.")
print("Status code:", r.status_code)
print("Response headers:", dict(r.headers))
try:
print("JSON response:", r.json())
except ValueError:
print("Non-JSON response text:", r.text)
# Optional: raise for non-2xx to get exception trace if desired
# r.raise_for_status()
except requests.exceptions.RequestException as e:
print("Request failed:", repr(e))
except FileNotFoundError:
print("File not found:", img_path)
except Exception as e:
print("Unexpected error:", repr(e))
if __name__ == "__main__":
main()
# --- IGNORE ---
# This script demonstrates how to send an image file to the FastAPI
# prediction endpoint and print the response containing predicted
# vehicle damage classes and confidence scores.