Skip to content

Commit dd97c6d

Browse files
committed
updated with Dockerfile and inference code
1 parent a162424 commit dd97c6d

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

Dockerfile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM python:3.7-slim-buster
2+
3+
ENV GRADIO_SERVER_PORT 80
4+
5+
# Set working directory
6+
WORKDIR /workspace/project
7+
8+
# Install requirements
9+
COPY requirements.txt ./
10+
11+
RUN pip install --no-cache-dir -r requirements.txt \
12+
&& rm requirements.txt
13+
14+
COPY inference.py ./
15+
16+
COPY images ./images
17+
18+
# tell the port number the container should expose
19+
EXPOSE 80
20+
21+
# run the application
22+
CMD [ "python3", "inference.py"]

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# aws-deploy-cifar
2-
Demo deployment using ECS and Fargate Spot
1+
### Demo deployment using ECS and Fargate Spot
2+

inference.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import boto3
2+
import botocore
3+
4+
import torch
5+
import gradio as gr
6+
from torchvision import transforms
7+
8+
def download_model():
9+
bucket = 'mlops-tutorials'
10+
object_name = 'model.script.pt'
11+
filename = 'model.script.pt'
12+
13+
s3 = boto3.resource('s3')
14+
15+
try:
16+
s3.Bucket(bucket).download_file(object_name, filename)
17+
except botocore.exceptions.ClientError as e:
18+
if e.response['Error']['Code'] == "404":
19+
print("The object does not exist.")
20+
else:
21+
raise
22+
return
23+
24+
def demo():
25+
#assert cfg.ckpt_path
26+
27+
model = torch.jit.load('model.script.pt')
28+
29+
model.eval()
30+
31+
# Labels for CIFAR10 dataset
32+
labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
33+
34+
def classify_top10(inp):
35+
inp = transforms.ToTensor()(inp).unsqueeze(0)
36+
with torch.no_grad():
37+
prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
38+
confidences = {labels[i]: float(prediction[i]) for i in range(10)}
39+
return confidences
40+
41+
im = gr.Image(shape=(224, 224), type="pil")
42+
43+
gr.close_all()
44+
45+
demo = gr.Interface(
46+
fn=classify_top10,
47+
inputs=[im],
48+
outputs=gr.Label(num_top_classes=10),
49+
examples='images',
50+
)
51+
52+
demo.launch(server_name="0.0.0.0", share=True)
53+
54+
55+
def main():
56+
demo()
57+
58+
if __name__ == "__main__":
59+
download_model()
60+
main()
61+
62+
63+

requirements.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--extra-index-url https://download.pytorch.org/whl/cpu
2+
torch==1.11.0+cpu
3+
torchvision==0.12.0+cpu
4+
5+
gradio==3.4.1
6+
boto3==1.24.89

0 commit comments

Comments
 (0)