1+ import numpy as np
2+ import requests
3+
4+ # --- (user_fun function from the previous section goes here) ---
5+
6+ # Configuration for the server endpoint
7+ SERVER_URL = "http://139.6.66.69:8000/compute/"
8+
9+ def user_fun (X : np .ndarray , ** kwargs ) -> np .ndarray :
10+ """
11+ A client-side function that sends a NumPy array to a remote server
12+ for computation and returns the result as a NumPy array.
13+ """
14+ payload = {"X" : X .tolist ()}
15+ response = requests .post (SERVER_URL , json = payload )
16+ response .raise_for_status ()
17+ result_data = response .json ()
18+ return np .array (result_data ['fx' ])
19+
20+ # --- Verification Logic ---
21+
22+ def user_fun_local (X : np .ndarray , ** kwargs ) -> np .ndarray :
23+ """The original local implementation for comparison."""
24+ return np .sum (X ** 2 , axis = 1 )
25+
26+ if __name__ == "__main__" :
27+ # Create a sample NumPy array for testing.
28+ test_X = np .array ([[1.0 , 2.0 , 3.0 ],
29+ [4.0 , 5.0 , 6.0 ],
30+ [0.1 , 0.2 , 0.3 ]])
31+
32+ print ("--- Verification Script ---" )
33+ print (f"Input Data (X):\n { test_X } \n " )
34+
35+ # 1. Compute the expected result using the local function.
36+ expected_result = user_fun_local (test_X )
37+ print (f"Expected Result (Local Computation): { expected_result } " )
38+
39+ # 2. Compute the result using the remote API call.
40+ try :
41+ remote_result = user_fun (test_X )
42+ print (f"Actual Result (Remote Computation): { remote_result } \n " )
43+
44+ # 3. Compare the results.
45+ # np.allclose is used for safe floating-point comparison.
46+ if np .allclose (expected_result , remote_result ):
47+ print ("✅ SUCCESS: Remote result matches the local result." )
48+ else :
49+ print ("❌ FAILURE: Remote result does NOT match the local result." )
50+
51+ except requests .exceptions .RequestException as e :
52+ print (f"❌ FAILURE: An error occurred while communicating with the server." )
53+ print (f" Error: { e } " )
54+ print (" Please ensure the FastAPI server is running at {SERVER_URL}" )
0 commit comments