heroku_如何通过5个步骤在Heroku上部署机器学习UI

heroku

In this tutorial, I’m going to show you, step by step, how to create and deploy your machine learning model and UI on Heroku. I’ll use this drag-and-drop image interface that I created for the Inception Net model as an example:

在本教程中,我将逐步向您展示如何在Heroku上创建和部署机器学习模型和UI。 我将使用为Inception Net模型创建的此拖放图像界面作为示例:

You can try out the GUI at: https://arcane-tundra-83748.herokuapp.com/ (might take a minute for the dyno to start up)

您可以在以下位置试用GUI: https//arcane-tundra-83748.herokuapp.com/ (可能需要一分钟才能使dyno启动)

格拉迪奥作品 (The Gradio Piece)

I’m assuming that you’re already somewhat familiar with the Gradio library (https://github.com/gradio-app/gradio). If not, this MNIST tutorial is a good place to get started. The quick overview is: Gradio is a Python library that lets you create UIs around your machine learning model by specifying 3 things: a Python function, input component, and an output component.

我假设您已经对Gradio库( https://github.com/gradio-app/gradio )有所了解。 如果没有,那么此MNIST教程是入门的好地方。 快速的概述是:Gradio是一个Python库,通过指定3种内容,您可以围绕机器学习模型创建UI:Python函数,输入组件和输出组件。

Step 1: Create your Gradio app

第1步:创建您的Gradio应用

We’ll start with a script that creates a drag-and-drop image interface around the Inception model. Create a brand new directory, and copy-paste the code below into a python file that we’ll call app.py:

我们将从创建一个围绕Inception模型创建拖放图像界面的脚本开始。 创建一个全新的目录,然后将以下代码复制粘贴到我们称为app.py的python文件中:

What’s happening in this script is that we’re loading the Inception Net image classifier, using Tensorflow Keras. Since this is an image classification model, we will use the Image input interface. We'll output a dictionary of labels and their corresponding confidence scores with the Label output interface. (To deploy your own model, swap this out with your own code).

该脚本中发生的事情是,我们正在使用Tensorflow Keras加载Inception Net图像分类器。 由于这是图像分类模型,因此我们将使用“ Image输入”界面。 我们将通过Label输出界面输出标签字典及其对应的置信度得分。 ( 要部署您自己的模型,请将其替换为您自己的代码 )。

Step 2: Write your requirements.txt file

第2步:编写您的 requirements.txt 文件

We need to make sure that we designate all of the Python libraries that we will need as dependencies. So create a requirements.txt file with the dependencies in the same directory as app.py. In my case, that’s:

我们需要确保将所有需要的Python库指定为依赖项。 因此,创建一个requirements.txt 文件,其依赖项与app.py位于同一目录中。 就我而言,那就是:

Getting the package versions to work on Heroku can be a little tricky, but the packages and versions above work for me. Important: Heroku limits the size of your build to be 500 MB, which can fill up easily given the size of the Tensorflow package. I suggest using the Tensorflow 2.0.0 library, which is smaller than some of other versions of the library.

使软件包版本在Heroku上工作可能会有些棘手,但是上面的软件包和版本对我有用。 重要提示: Heroku将构建的大小限制为500 MB,考虑到Tensorflow软件包的大小,它可以轻松填充。 我建议使用Tensorflow 2.0.0库,该库比其他版本的库要小。

If you want to test the script locally (which you should!), run from a Terminal: pip install -r requirements.txt to install the dependencies, and then python app.py to launch the UI. At this point, you should see that your interface is running on localhost:7860 (or another port), which if you navigate to in your browser should show your GUI!

如果要在本地测试脚本(应该这样做),请从终端运行: pip install -r requirements.txt以安装依赖项,然后使用python app.py启动UI。 此时,您应该看到您的界面正在localhost:7860 (或其他端口)上运行,如果您在浏览器中导航到该界面,则应显示GUI!

This URL is locally accessible. How do we share this with the world? Gradio comes with a built-in share parameter that can create public URLs, but those expire after a certain amount of time. To create permanent public links, we’ll be using Heroku!

此URL可在本地访问。 我们如何与世界分享这一点? Gradio带有一个内置的share参数,该参数可以创建公共URL,但是这些URL将在一定时间后失效。 要创建永久的公共链接,我们将使用Heroku!

Heroku件 (The Heroku Piece)

To deploy your web app on Heroku, you’ll need to have a Heroku account, and it’s very convenient to have the Heroku CLI as well. So go ahead and create a Heroku account and download the CLI, if you haven’t done that already.

要在Heroku上部署Web应用程序,您需要拥有一个Heroku帐户,并且拥有Heroku CLI也非常方便。 因此,继续创建Heroku帐户下载CLI ,如果尚未完成的话。

Now, on to the deployment!

现在,开始部署!

Step 3: Create a setup.sh file

步骤3:创建一个 setup.sh 文件

In order to deploy our app correctly on Heroku, we need to make sure it’s served on the right URL & port. The commands below do that, so put them inside a file called setup.sh in the same directory as your app.

为了在Heroku上正确部署我们的应用,我们需要确保在正确的URL和端口上提供了该应用。 下面的命令可以执行此操作,因此请将它们放在一个名为setup.sh的文件中 与您的应用位于同一目录中。

What’s happening here is that we’re telling Gradio to serve the UI on 0.0.0.0 , and specifically using the port that the Heroku dyno makes visible to the world.

这里发生的是,我们要告诉Gradio在0.0.0.0上提供UI,特别是要使用Heroku dyno使世界可见的端口。

Step 4: Create a Procfile

步骤4:创建一个 Procfile

Now, we’re going to create a file called Procfile (that’s it, there’s no extension for this file!) whose job it is to tell Heroku what commands to run to start the Gradio app. There are only two commands: to run the bash script we created in the previous step, and then to launch our app. So our Heroku Procfile looks like this:

现在,我们将创建一个名为Procfile的文件(就是这个文件,没有扩展名!),它的工作是告诉Heroku运行哪些命令来启动Gradio应用程序。 只有两个命令:运行在上一步中创建的bash脚本,然后启动我们的应用程序。 所以我们的Heroku Procfile 看起来像这样:

web: source setup.sh && python app.py

If you’re wondering why we’re using source instead of sh , it’s because we need the environment variables to be saved after the script is finished executing.

如果您想知道为什么我们使用source而不是sh ,那是因为我们需要在脚本执行完后保存环境变量。

Step 5: Deploy!

步骤5:部署!

At this point, your directory should look like this:

此时,您的目录应如下所示:

Image for post
Just 4 files in 1 directory — super simple!
1个目录中只有4个文件-超级简单!

If you haven’t already added these files to a git repo, do it by running the following in your terminal:

如果您尚未将这些文件添加到git仓库中,请在终端中运行以下命令来进行操作:

git init
git add -A
git commit -am "commit message here"

We just need to push this to a Heroku dyno. First, let’s allocate a Heroku dyno for our app by running:

我们只需要将其推送到Heroku测功机即可。 首先,让我们通过运行以下命令为我们的应用分配一个Heroku dyno:

heroku create

Heroku will automatically spin up and assign a random name to your app. Now, we push our changes to Heroku:

Heroku将自动旋转并为您的应用分配一个随机名称。 现在,我们将更改推送到Heroku:

git push heroku master

Give this a few minutes to install all of the dependencies on your dyno. It’s possible to run into dependency issues here, which you can resolve by reading the error messages and installing the right version of each library (see my note above about Heroku’s size limits). If you don’t see any errors, then you should be able to open up your app by running:

花几分钟时间在dyno上安装所有依赖项。 在这里可能会遇到依赖关系问题,您可以通过阅读错误消息并安装每个库的正确版本来解决( 请参见上面有关Heroku大小限制的注释 )。 如果没有看到任何错误,那么您应该可以通过运行以下命令来打开您的应用程序:

heroku ps:scale web=1
heroku open

That last command will open up your default browser and bring you to the app. You’ll notice the web address is the automatically generated instance name plus the Heroku domain.

最后一条命令将打开您的默认浏览器,并将您带到应用程序。 您会注意到该网址是自动生成的实例名称加上Heroku域。

Image for post

That’s it! You have a live link that you can share with anyone. Time to start running some predictions :)

而已! 您有一个实时链接,可以与任何人共享。 是时候开始运行一些预测了:)

翻译自: https://towardsdatascience.com/how-to-deploy-a-machine-learning-ui-on-heroku-in-5-steps-b8cd3c9208e6

heroku

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值