Created
December 23, 2021 20:54
-
-
Save vinimonteiro/5acf8cd83b5092188352c70ca40f4a42 to your computer and use it in GitHub Desktop.
MNIST model as a a service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import numpy as np | |
from neural_network import NeuralNetwork | |
from flask import Flask, request | |
import json | |
model = None | |
app = Flask(__name__) | |
def load(): | |
global model | |
model = NeuralNetwork() | |
model.load_state_dict(torch.load("model.pth")) | |
model.eval() | |
@app.route('/guess', methods=['POST']) | |
def get_data(): | |
if request.method == 'POST': | |
data_string = request.get_json() | |
data_array = json.loads(data_string) | |
data_tensor = torch.Tensor(data_array) | |
data_tensor_flattened = data_tensor.view(-1, 784) | |
guess = torch.argmax(model(data_tensor_flattened)[0]) | |
return str(guess) | |
if __name__ == '__main__': | |
load() | |
app.run(host='127.0.0.1', port=8888) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment