Spaces:
Sleeping
Sleeping
Clement Vachet
commited on
Commit
·
711e3f5
1
Parent(s):
8ed7e05
Add inference python file
Browse files- inference_api.py +45 -0
inference_api.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import io
|
3 |
+
import json
|
4 |
+
import argparse
|
5 |
+
import sys
|
6 |
+
|
7 |
+
|
8 |
+
# Default examples
|
9 |
+
# api_url = "http://localhost:8080/2015-03-31/functions/function/invocations"
|
10 |
+
|
11 |
+
|
12 |
+
def arg_parser():
|
13 |
+
"""Parse arguments"""
|
14 |
+
|
15 |
+
# Create an ArgumentParser object
|
16 |
+
parser = argparse.ArgumentParser(description='Object detection inference via API call')
|
17 |
+
# Add arguments
|
18 |
+
parser.add_argument('-u', '--url', type=str, help='URL to the server (with endpoint location)', required=True)
|
19 |
+
parser.add_argument('-d', '--data', type=str, help='Input data', required=True)
|
20 |
+
parser.add_argument('-v', '--verbose', action='store_true', help='Increase output verbosity')
|
21 |
+
return parser
|
22 |
+
|
23 |
+
|
24 |
+
def main(args=None):
|
25 |
+
"""Main function"""
|
26 |
+
|
27 |
+
args = arg_parser().parse_args(args)
|
28 |
+
# Use the arguments
|
29 |
+
if args.verbose:
|
30 |
+
print(f'Input data: {args.data}')
|
31 |
+
print(f'Input data type: {type(args.data)}')
|
32 |
+
|
33 |
+
# Send request to API
|
34 |
+
response = requests.post(args.url, json=json.loads(args.data))
|
35 |
+
|
36 |
+
if response.status_code == 200:
|
37 |
+
# Process the response
|
38 |
+
processed_data = json.loads(response.content)
|
39 |
+
print('processed_data', processed_data)
|
40 |
+
else:
|
41 |
+
print(f"Error: {response.status_code}")
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
sys.exit(main(sys.argv[1:]))
|