ajayduraisamy Copilot commited on
Commit
cfdc9b0
·
1 Parent(s): 5e468e2

Update hosted model upload test

Browse files

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Files changed (1) hide show
  1. test.py +102 -0
test.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import json
3
+ import mimetypes
4
+ import sys
5
+ import tkinter as tk
6
+ from tkinter import filedialog
7
+
8
+ import requests
9
+
10
+
11
+ def main():
12
+ parser = argparse.ArgumentParser(
13
+ description="Select an image and upload it to the hosted model 1 API."
14
+ )
15
+ parser.add_argument(
16
+ "image",
17
+ nargs="?",
18
+ help="Image file to upload. If omitted, a file picker opens.",
19
+ )
20
+ parser.add_argument(
21
+ "--url",
22
+ default="https://ajaycodeself-ml-models.hf.space/model/1",
23
+ help="Model 1 endpoint URL (default: %(default)s).",
24
+ )
25
+ args = parser.parse_args()
26
+
27
+ image_path = args.image
28
+ if not image_path:
29
+ try:
30
+ root = tk.Tk()
31
+ root.withdraw()
32
+ image_path = filedialog.askopenfilename(
33
+ title="Select an image to upload",
34
+ filetypes=[
35
+ ("Image files", "*.jpg *.jpeg *.png *.webp *.bmp"),
36
+ ("All files", "*.*"),
37
+ ],
38
+ )
39
+ root.destroy()
40
+ except tk.TclError as exc:
41
+ print(f"Could not open image picker: {exc}", file=sys.stderr)
42
+ return 1
43
+
44
+ if not image_path:
45
+ print("No image selected; upload cancelled.")
46
+ return 1
47
+
48
+ try:
49
+ with open(image_path, "rb") as image_file:
50
+ content_type = mimetypes.guess_type(image_path)[0] or "application/octet-stream"
51
+ response = requests.post(
52
+ args.url,
53
+ files={
54
+ "image": (
55
+ image_path.rsplit("/", 1)[-1].rsplit("\\", 1)[-1],
56
+ image_file,
57
+ content_type,
58
+ )
59
+ },
60
+ timeout=120,
61
+ )
62
+ except OSError as exc:
63
+ print(f"Could not open selected image: {exc}", file=sys.stderr)
64
+ return 1
65
+ except requests.RequestException as exc:
66
+ print(f"Could not reach {args.url}: {exc}", file=sys.stderr)
67
+ print("Check that the Hugging Face Space is awake and running.", file=sys.stderr)
68
+ return 1
69
+
70
+ try:
71
+ result = response.json()
72
+ except ValueError:
73
+ print(f"API returned non-JSON response (HTTP {response.status_code}).")
74
+ print(response.text)
75
+ return 1
76
+
77
+ print(f"HTTP {response.status_code}")
78
+ print(json.dumps(result, indent=2))
79
+
80
+ expected_classes = {"healthy", "unhealthy"}
81
+ if response.status_code != 200 or result.get("status") != "success":
82
+ print("FAIL: API request did not succeed.", file=sys.stderr)
83
+ return 1
84
+ if result.get("prediction") not in expected_classes:
85
+ print("FAIL: API returned an unexpected prediction.", file=sys.stderr)
86
+ return 1
87
+
88
+ confidence = result.get("confidence")
89
+ confidence_percent = result.get("confidence_percent")
90
+ if not isinstance(confidence, (int, float)) or confidence != 1.0:
91
+ print("FAIL: API returned an invalid confidence.", file=sys.stderr)
92
+ return 1
93
+ if not isinstance(confidence_percent, (int, float)) or confidence_percent != 100.0:
94
+ print("FAIL: API confidence_percent was not 100.0.", file=sys.stderr)
95
+ return 1
96
+
97
+ print("PASS: image upload returned a valid model prediction.")
98
+ return 0
99
+
100
+
101
+ if __name__ == "__main__":
102
+ raise SystemExit(main())