<jupyter_start><jupyter_text>This notebook contains an excerpt from the [PyTorch Pocket Reference](http://pytorchbook.com) book by [<NAME>](http://joepapa.ai); content is available [on GitHub](https://github.com/joe-papa/pytorch-book). [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joe-papa/pytorch-book/blob/main/07_02_Deploying_Flask_App_1.ipynb)  Chapter 7 - Deploy to Production  Flask App<jupyter_code>import io
import json

from torchvision import models
import torchvision.transforms as transforms
from PIL import Image
from flask import Flask, jsonify, request<jupyter_output><empty_output><jupyter_text>Download from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json<jupyter_code>!wget "https://raw.githubusercontent.com/raghakot/keras-vis/master/resources/imagenet_class_index.json"
import json
imagenet_class_index = json.load(
    open("./imagenet_class_index.json"))

model = models.vgg16(pretrained=True)

image_transforms = transforms.Compose(
    [transforms.Resize(255),
     transforms.CenterCrop(224),
     transforms.ToTensor(),
     transforms.Normalize(
          [0.485, 0.456, 0.406],
          [0.229, 0.224, 0.225])])

def get_prediction(image_bytes):
 image = Image.open(io.BytesIO(image_bytes))
 tensor = image_transforms(image)
 outputs = model(tensor)
 _, y = outputs.max(1)
 predicted_idx = str(y.item())
 return imagenet_class_index[predicted_idx]
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
  if request.method == 'POST':
    file = request.files['file']
  img_bytes = file.read()
  class_id, class_name = \
    get_prediction(image_bytes=img_bytes)
  return jsonify({'class_id': class_id,
                 'class_name': class_name})
!pip install flask-ngrok
from flask_ngrok import run_with_ngrok # <1>
@app.route("/")
def home():
 return "<h1>Running Flask on Google Colab!</h1>" 

app.run()
app = Flask(__name__)
run_with_ngrok(app) #<2>

@app.route('/predict', methods=['POST'])
def predict():
  if request.method == 'POST':
    file = request.files['file']
  img_bytes = file.read()
  class_id, class_name = \
    get_prediction(image_bytes=img_bytes)
  return jsonify({'class_id': class_id,
            'class_name': class_name})
 
app.run() # <3><jupyter_output>* Serving Flask app "__main__" (lazy loading)
 * Environment: production
[31m   WARNING: This is a development server. Do not use it in a production deployment.[0m
[2m   Use a production WSGI server instead.[0m
 * Debug mode: off
{'fraction_non_alphanumeric': 0.13814512999611953, 'fraction_numerical': 0.019402405898331393, 'mean_word_length': 4.87243735763098, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '429814', 'license': 'MIT', 'n_tokens_mistral': 932, 'n_tokens_neox': 898, 'n_words': 180, 'path': '07_02_Deploying_Flask_App_1.ipynb', 'repo_name': 'kingslyt/pytorch-book', 'size': '2579'}