def get_learner( model_url="https://storage.googleapis.com/sv-fastai/models/apthunt/20200503_cltab.pkl" ): print(f"downloading model from {model_url}") path = maybe_download_model(model_url) learn = load_learner(path) return learn
def predict(img): # Display the test image st.image(img, use_column_width=True) # Temporarily displays a message while executing with st.spinner('Wait for it...'): time.sleep(3) # Load model and make prediction learn_inf = load_learner('export.pkl') pred, pred_idx, probs = learn_inf.predict(img) st.success(f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}')
def load_learner_from_s3(pkl_name, update_ml=server_config.UPDATE_ML): """Import the latest trained model from S3 Keyword Arguments: pkl_name {str} -- Name of pickled model to use (default: {'0_18_512_0.722.pkl'}) Returns: fastai2_learner -- A learner with the model already loaded into memory """ pkl_path = Path(path_config.LOCAL_DIR) / "models" / pkl_name if server_config.VERBOSE: print("Loading Learner") if update_ml: clear(pkl_path) if not pkl_path.exists(): # pylint: disable=no-member src_path = "s3://skytruth-cerulean/model_artifacts/" + str(pkl_name) download_str = f"aws s3 cp {src_path} {pkl_path}" # print(download_str) run(download_str, shell=True) return load_learner(pkl_path)
from flask import Flask, jsonify, request from flask import render_template from flask_sockets import Sockets from flask_cors import CORS from fastai2.learner import load_learner from dotenv import load_dotenv load_dotenv() app = Flask(__name__) CORS(app) sockets = Sockets(app) trump = load_learner('model.pkl') @app.route('/') def index(): response = jsonify({'status': 'ok'}) response.status_code = 200 return response @app.route('/api/1.0/classify-image', methods=['POST']) def classify(): image = request.files['image'] res = trump.predict(image.read()) response = jsonify({'result': res[0]}) response.status_code = 200
#https://www.roytuts.com/upload-and-display-image-using-python-flask/ import os import uuid import urllib.request from fastai2.learner import load_learner from flask import Flask, flash, request, redirect, url_for, render_template app = Flask(__name__) learner_inference = load_learner('../models/coins.pkl') @app.route('/') def upload_form(): return render_template('upload.html') @app.route('/', methods=['POST']) def upload_image(): if 'file' not in request.files: return redirect(request.url) file = request.files['file'] if file.filename == '': return redirect(request.url) if file: filename = str(uuid.uuid4()) filepath = os.path.join('./static/uploads', filename) file.save(filepath)