Skip to content

Add fastai example #725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

- [Iris classification](pytorch/iris-classifier): deploy a model to classify iris flowers.

- [Search completion](pytorch/search-completer): deploy a Facebook's RoBERTa model to complete search terms.

- [Text generation](pytorch/text-generator): deploy Hugging Face's DistilGPT2 model to generate text.

- [Sentiment analysis](pytorch/sentiment-analyzer): deploy a fastai model for sentiment analysis.

- [Search completion](pytorch/search-completer): deploy a Facebook's RoBERTa model to complete search terms.

- [Answer generation](pytorch/answer-generator): deploy Microsoft's DialoGPT model to answer questions.

- [Text summarization](pytorch/text-summarizer): deploy a BERT model to summarize text.
Expand Down
1 change: 1 addition & 0 deletions examples/pytorch/sentiment-analyzer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Please refer to the [tutorial](https://cortex.dev/iris-classifier) to see how to deploy an example with Cortex.
12 changes: 12 additions & 0 deletions examples/pytorch/sentiment-analyzer/cortex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# WARNING: you are on the master branch, please refer to the examples on the branch that matches your `cortex version`

- kind: deployment
name: sentiment

- kind: api
name: analyzer
predictor:
type: python
path: predictor.py
compute:
cpu: 1
127 changes: 127 additions & 0 deletions examples/pytorch/sentiment-analyzer/fastai_sentiment_ulmfit.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "fastai-sentiment-ulmfit.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "U7wT7Te0Jxzr",
"colab_type": "text"
},
"source": [
"# Exporting ULMFiT with fastai\n",
"WARNING: you are on the master branch, please refer to the examples on the branch that matches your `cortex version`\n",
"\n",
"This file includes code which was modified from https://docs.fast.ai/text.html\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pPj45JaXsmfT",
"colab_type": "code",
"colab": {}
},
"source": [
"from fastai.text import *\n",
"import pandas as pd"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "_BB4WshHJuZ3",
"colab_type": "code",
"colab": {}
},
"source": [
"path = untar_data(URLs.IMDB_SAMPLE)\n",
"path"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "CJ4B9JVgJvUC",
"colab_type": "code",
"colab": {}
},
"source": [
"df = pd.read_csv(path/'texts.csv')\n",
"df.head()\n",
"\n",
"# Language model data\n",
"data_lm = TextLMDataBunch.from_csv(path, 'texts.csv')\n",
"\n",
"# Classifier model data\n",
"data_clas = TextClasDataBunch.from_csv(path, 'texts.csv', vocab=data_lm.train_ds.vocab, bs=32)\n",
"data_lm.save('data_lm_export.pkl')\n",
"data_clas.save('data_clas_export.pkl')\n",
"data_lm = load_data(path, 'data_lm_export.pkl')\n",
"data_clas = load_data(path, 'data_clas_export.pkl', bs=16)\n",
"\n",
"# Fine-tune model\n",
"learn = language_model_learner(data_lm, AWD_LSTM, drop_mult=0.5)\n",
"learn.fit_one_cycle(1, 1e-2)\n",
"learn.unfreeze()\n",
"learn.fit_one_cycle(1, 1e-3)\n",
"learn.predict(\"This is a review about\", n_words=10)\n",
"learn.save_encoder('ft_enc')\n",
"learn = text_classifier_learner(data_clas, AWD_LSTM, drop_mult=0.5)\n",
"learn.load_encoder('ft_enc')\n",
"data_clas.show_batch()\n",
"learn.fit_one_cycle(1, 1e-2)\n",
"learn.freeze_to(-2)\n",
"learn.fit_one_cycle(1, slice(5e-3/2., 5e-3))\n",
"learn.unfreeze()\n",
"learn.fit_one_cycle(1, slice(2e-3/100, 2e-3))\n",
"learn.predict(\"This was a great movie!\")\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "PhaDZglceCro",
"colab_type": "code",
"colab": {}
},
"source": [
"# Connect to Drive\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "SftzK0UadjZB",
"colab_type": "code",
"colab": {}
},
"source": [
"# Export model\n",
"learn.export('/content/drive/My Drive/export.pkl')"
],
"execution_count": 0,
"outputs": []
}
]
}
19 changes: 19 additions & 0 deletions examples/pytorch/sentiment-analyzer/predictor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# WARNING: you are on the master branch, please refer to the examples on the branch that matches your `cortex version`

from fastai.text import *
import requests


class PythonPredictor:
def __init__(self, config):
req = requests.get(
"https://cortex-examples.s3-us-west-2.amazonaws.com/pytorch/sentiment-analyzer/export.pkl"
)
with open("export.pkl", "wb") as model:
model.write(req.content)

self.predictor = load_learner(".")

def predict(self, payload):
prediction = self.predictor.predict(payload["text"])
return prediction[0].obj
1 change: 1 addition & 0 deletions examples/pytorch/sentiment-analyzer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fastai==1.*
3 changes: 3 additions & 0 deletions examples/pytorch/sentiment-analyzer/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"text": "best day ever"
}