Skip to content

Commit 6d90ccb

Browse files
committed
initial commit ➕➕➕
1 parent 04088ce commit 6d90ccb

File tree

17 files changed

+159
-0
lines changed

17 files changed

+159
-0
lines changed

0.png

3.02 KB
Loading

1.png

2 KB
Loading

2.png

5.03 KB
Loading

3.png

3.59 KB
Loading

4.png

4.67 KB
Loading

my-model/environment/conda.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
channels:
2+
- conda-forge
3+
dependencies:
4+
- python=3.9
5+
- pip=23.0.1
6+
- pip:
7+
- azureml-defaults==1.53.0
8+
- torch
9+
- transformers
10+
- pillow
11+
- pandas
12+
- scipy

my-model/model/dummy model.pkl

756 Bytes
Binary file not shown.

my-model/onlinescoring/score.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from transformers import pipeline
2+
from PIL import Image
3+
import torch
4+
import base64
5+
import json
6+
from io import BytesIO
7+
import logging
8+
9+
def img_to_base64(image):
10+
im_file = BytesIO()
11+
image.save(im_file, format="JPEG")
12+
im_bytes = im_file.getvalue()
13+
im_b64 = base64.b64encode(im_bytes)
14+
return im_b64
15+
16+
def init():
17+
global semantic_segmentation
18+
semantic_segmentation = pipeline("image-segmentation", model="facebook/maskformer-swin-large-coco",
19+
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
20+
logging.info(f"pipeline device : {semantic_segmentation.device}")
21+
logging.info("Init complete")
22+
23+
def run(raw_data):
24+
logging.info("model : request received")
25+
data = json.loads(raw_data)
26+
image = Image.open(BytesIO(base64.b64decode(data['img'])))
27+
results = semantic_segmentation(image)
28+
for i,j in enumerate(results):
29+
img = j['mask']
30+
wef = img_to_base64(img)
31+
results[i]['mask'] = wef.decode("utf-8")
32+
logging.info("Request processed")
33+
return results

my-model/request-structure.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"img": "<base64 encoded img>"
3+
}

my-model/test cpu/req.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import urllib.request
2+
import json
3+
import os
4+
import ssl
5+
import time
6+
7+
def allowSelfSignedHttps(allowed):
8+
# bypass the server certificate verification on client side
9+
if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
10+
ssl._create_default_https_context = ssl._create_unverified_context
11+
12+
allowSelfSignedHttps(True) # this line is needed if you use self-signed certificate in your scoring service.
13+
14+
# Request data goes here
15+
# The example below assumes JSON formatting which may be updated
16+
# depending on the format your endpoint expects.
17+
# More information can be found here:
18+
# https://docs.microsoft.com/azure/machine-learning/how-to-deploy-advanced-entry-script
19+
20+
with open('test cpu/request.json') as f:
21+
data = json.load(f)
22+
23+
body = str.encode(json.dumps(data))
24+
25+
url = 'https://custom-endpoint.centralindia.inference.ml.azure.com/score'
26+
# Replace this with the primary/secondary key, AMLToken, or Microsoft Entra ID token for the endpoint
27+
api_key = 'f8pjDedS03UCIOVtZdr9xPcO3ImPnXF4'
28+
if not api_key:
29+
raise Exception("A key should be provided to invoke the endpoint")
30+
31+
# The azureml-model-deployment header will force the request to go to a specific deployment.
32+
# Remove this header to have the request observe the endpoint traffic rules
33+
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key), 'azureml-model-deployment': 'deployment-1' }
34+
35+
req = urllib.request.Request(url, body, headers)
36+
37+
try:
38+
t1 = time.time()
39+
response = urllib.request.urlopen(req)
40+
print("Time taken to get response : ", time.time()-t1)
41+
result = response.read()
42+
# print(result)
43+
except urllib.error.HTTPError as error:
44+
print("The request failed with status code: " + str(error.code))
45+
46+
# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
47+
print(error.info())
48+
print(error.read().decode("utf8", 'ignore'))

0 commit comments

Comments
 (0)