<jupyter_start><jupyter_text>!pip install flask-restful==0.3.6!pip install flask==0.12.2!pip install flask-jsonpify==1.5.0 --user<jupyter_code>import pickle
import pandas as pd

from flask import Flask, request
from flask_restful import Api, Resource
from flask.ext.jsonpify import jsonpify
# "100-323-4124.4324-0.4242-5".split("-")
import numpy as np
import time
t = time.strftime('%x %X')
t
def escreve_log(path='log.log', text=''):
    with open(path, 'a') as log:
        t = time.strftime('%x %X')
        log.write(t + str(text) + '\n')
def open_pkl(path=''):
    escreve_log(text='comecou a carregar o pkl')
    file = open(path, 'rb')
    model = pickle.load(file)
    file.close
    escreve_log(text='terminou de carregar o pkl')
    return model


class Index(Resource):
    def get(self):
        escreve_log(text='acessou o index')
        return ('''
        teste
        ''')


class Prediction(Resource):
    def get(self, features):
        
        escreve_log(text='usuario iniciou uma prediction')
        features = str(features).split("-")
        X = np.array(features).reshape(1,5)
        escreve_log(text=f'valores:{X}' )
        df = pd.DataFrame(X, columns=['LotArea','YearBuilt','OverallQual','WoodDeckSF','YrSold'])
        model = open_pkl('model.pkl')
        
        yhat = model.predict(df)
        escreve_log(text=f'predicao:{yhat}')
        return jsonpify(yhat[0])


def main():
    app = Flask(__name__)
    api = Api(app)
    
    api.add_resource(Index, '/')
    api.add_resource(Prediction, '/predict/<features>')

    app.run(port=1247, host='0.0.0.0')


main()<jupyter_output>* Running on http://0.0.0.0:1247/ (Press CTRL+C to quit)
127.0.0.1 - - [03/Dec/2018 22:13:09] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:13:55] "GET /predict/100-323-4124.4324-0.4242-5 HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:13:55] "GET /predict/100-323-4124.4324-0.4242-5 HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:13:55] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:14:00] "GET /predict/100-323-4124.4324-0.4242-5 HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:14:05] "GET /predict/100-323-10.4324-0.4242-5 HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:14:11] "GET /predict/1-323-10.4324-0.4242-5 HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:14:17] "GET /predict/1-323-10.4324-0.4242-1 HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:16:22] "GET /predict/1-323-10.4324-0.4242-1 HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:16:30] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:16:53] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Dec/2018 22:17:32] "GET / HTTP/1.1" 200 [...]
{'fraction_non_alphanumeric': 0.1996190476190476, 'fraction_numerical': 0.1718095238095238, 'mean_word_length': 2.735419630156472, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '440648', 'license': 'MIT', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1143, 'n_words': 203, 'path': '98-dados/api/criando APIs/webservice.ipynb', 'repo_name': 'abefukasawa/datascience_course', 'size': '2625'}