Beispiel #1
0
def make_fastai_serializable(learner):
    path = learner.path
    now = int(time.time())
    temp_file_name = f'temp_learner_{str(now)}.pkl'
    learner.export(temp_file_name, destroy=False)
    serializable_learner = load_learner(path, temp_file_name)
    os.remove(os.path.join(path, temp_file_name))
    return serializable_learner
Beispiel #2
0
    def process(self, element, model_side_input):
        if not hasattr(self, 'learn'):
            from fastai.tabular import load_learner
            import io
            self.learn = load_learner('/some/path',
                                      file=io.BytesIO(model_side_input[0]))

        element['prediction'] = self.learn.predict(element)[1].item()
        yield element
Beispiel #3
0
def perform_fastai_return_scoring(gw):
    export_dir = Path("./data/model_outputs/fastai_star_target_model")
    learn = load_learner(export_dir)
    XY_train, XY_test, XY_scoring, features_dict = load_data(gw)

    n_ex = len(XY_scoring)
    fast_scores = []
    for idx in tqdm(range(n_ex)):
        _, _, this_pred = learn.predict(XY_scoring.iloc[idx])
        fast_scores.append(this_pred[1].item())

    df = pd.DataFrame()
    df["player_id"] = XY_scoring["player_id"].values
    df["gw"] = gw
    df['fastai_return_pred'] = fast_scores
    save_path = os.path.join("./data/model_outputs",
                             "fastai_return_predictions_gw_{}.csv".format(gw))
    df.to_csv(save_path, index=False)
    print(df.head())
    result_code = 1
    return result_code
Beispiel #4
0
from fastai.tabular import load_learner, add_datepart
import pandas as pd
import numpy as np

infer = load_learner('.', file='date_prev_btc_model.pkl')


def pred_single(date, prev, learn=infer):
    print(
        f'Getting predictions for date {date} with prev closing price of {prev}'
    )
    df = pd.DataFrame(dict(Date=date, prev=prev), index=[0])
    add_datepart(df, 'Date')
    pred = learn.predict(df.iloc[0])
    print(pred)
    res = round(np.exp(pred[0].data.item()), 2)
    print(res)
    return res


if __name__ == "__main__":
    pred_single('2019-09-26', 8438)
Beispiel #5
0
from starlette.applications import Starlette
from starlette.responses import JSONResponse, PlainTextResponse
from fastai.tabular import load_learner, DataFrame
from pathlib import Path

path = Path('ml')
learn = load_learner(path)
app = Starlette()


@app.route('/', methods=['GET'])
async def root(request):
    return PlainTextResponse('Dude, suh?')


@app.route("/predict", methods=["POST"])
async def classify_url(request):
    json = await request.json()
    data = json['data']
    df = DataFrame.from_dict(data)

    predictions = []
    for _, row in df.iterrows():
        _, _, tensor = learn.predict(row)
        height = tensor.item()
        predictions.append(height)

    return JSONResponse({'predictions': predictions})