heroku_部署先知预测模型,并简化到heroku

heroku

Streamlit and Facebook Prophet give us the ability to create a clean forecasting dashboard with minimal effort and no cost. This makes for a solid proof of concept workflow.

Streamlit和Facebook Prophet使我们能够以最少的工作量和成本创建干净的预测仪表板。 这为概念工作流程提供了可靠的证明。

In this project, we create a dashboard using Streamlit and Prophet to showcase data and make forecasts and then deploy our app to Heroku.

在这个项目中,我们使用Streamlit和Prophet创建一个仪表板来展示数据并做出预测,然后将我们的应用程序部署到Heroku。

Here is a link to the GitHub repository with our code for the project. Note that we use Pipenv to manage our dependencies.

这是带有我们项目代码的GitHub存储库的链接。 请注意,我们使用Pipenv来管理我们的依赖项。

什么是Streamlit? (What is Streamlit?)

Streamlit is a free and open-source app framework that presents an easy way for data scientists and machine learning engineers to create great Python apps quickly. Streamlit’s opinioned nature makes building frontends easy. Lastly, Streamlit can make experimentation and production of data apps and machine learning analysis easier to do and to iterate on.

Streamlit是一个免费的开源应用程序框架 呈现一个 数据科学家和机器学习工程师快速创建出色的Python应用程序的简便方法。 Streamlit自以为是的性质使构建前端变得容易。 最后,Streamlit可以使数据应用程序的实验和生产以及机器学习分析更容易进行和迭代。

Check it out on GitHub.

在GitHub上查看。

流线原则 (Streamlit principles)

Streamlit is excellent because it embraces Python scripting, treats widgets as variables, and caches data and computation for reuse. This allows us to have custom complex visualizations that only need to load once. Additionally, Streamlit can be used locally or run on the cloud.

Streamlit之所以出色,是因为它包含Python脚本,将小部件视为变量,并缓存数据和计算以供重用。 这使我们可以拥有仅需加载一次的定制复杂可视化文件。 此外,Streamlit可以在本地使用或在云上运行。

安装和运行Streamlit (Installing and Running Streamlit)

先决条件 (Prerequisites)

Before you get started, you’re going to need Python 3.6 or later.

在开始之前,您将需要Python 3.6或更高版本。

演示版 (Demo)

First, let’s install streamlit. If you have issues getting your machine set up, feel free to use our Pipfile from the project repository.

首先,让我们安装streamlit。 如果您在设置机器时遇到问题,请随时使用项目存储库中的Pipfile。

$ pip install streamlit

Now run the hello world app to make sure everything it’s working:

现在运行hello world应用程序以确保一切正常:

$ streamlit hello

This command opens a web server on your computer where you can play around with some demos! The first time you run this command, you may be prompted for an email that is added to ~/.streamlit/config.toml . It is not necessary to add your email to use to the software.

此命令在您的计算机上打开一个Web服务器,您可以在其中演示一些演示! 首次运行此命令时,系统可能会提示您输入一封电子邮件,该电子邮件已添加到~/.streamlit/config.toml 。 无需将您的电子邮件添加到该软件中。

It’s worth noting that this email prompt will appear each time you run Streamlit without a complete config.toml file. To get around this, we use this bash script to input our email and the port we want to run on.

值得注意的是,每次运行Streamlit时,如果没有完整的config.toml文件,此电子邮件提示就会出现。 为了解决这个问题,我们使用此bash脚本输入电子邮件和我们要在其上运行的端口。

mkdir -p ~/.streamlit/


echo "\
[general]\n\
email = \"your-email@domain.com\"\n\
" > ~/.streamlit/credentials.toml


echo "\
[server]\n\
headless = true\n\
enableCORS=false\n\
port = $PORT\n\
" > ~/.streamlit/config.toml

Once we run this setup.sh script, we have our email and port set.

运行此setup.sh脚本后,我们setup.sh设置了电子邮件和端口。

什么是Facebook先知? (What is Facebook Prophet?)

Facebook Prophet or FB Prophet is, fittingly, a prediction tool. In lieu of predicting the end-times, we are going to forecast time series COVID data. FB Prophet uses pandas and takes in and outputs a Dataframe.

Facebook Prophet或FB Prophet恰恰是一种预测工具。 代替预测结束时间,我们将预测时间序列COVID数据。 FB Prophet使用熊猫并接收并输出一个数据框。

The Dataframes passed to FB prophet must have a “ds” and “y” column. We then feed a Dataframe containing only dates into

传递给FB先知的数据框必须具有“ ds”和“ y”列。 然后,我们将仅包含日期的数据框输入

FB Prophet and the forecast fills the missing values.

FB先知和预测填补了缺失的值。

We had some issues getting this set up locally on a windows 10 machine and within a docker container. However, we had no complications at all when using GCP AI Platform notebooks.

我们在Windows 10计算机和Docker容器中本地设置此设置时遇到了一些问题。 但是,使用GCP AI Platform笔记本电脑时,我们一点也不复杂。

安装FB Prophet (Installing FB Prophet)

You will need to install pystanbefore installing fbprophet .

在安装pystan之前,您需要安装fbprophet

pip install pystan

Then you should be able to run:

然后,您应该可以运行:

pip install fbprophet

Note that you might get an error with FB Prophet that states it can’t be found. We ran into this error and the package was found. If you have issues with setup, this try a cloud VM or using our Pipfile!

请注意,FB Prophet可能会出现错误,指出找不到该错误。 我们遇到此错误,找到了包。 如果您在设置方面遇到问题,请尝试使用云虚拟机或使用我们的Pipfile!

构建我们的Streamlit应用程序 (Building our Streamlit App)

Now that we have Streamlit and a basic understanding of what it does let’s get started with our forecasting app.

现在,我们已经有了Streamlit并对其功能有了基本了解,让我们开始使用我们的预测应用程序。

Pandas works well with Streamlit and is already a dependency for FB Prophet, so we are using it in the load_data.py file to make our lives a little easier.

Pandas与Streamlit配合良好,并且已经成为FB Prophet的依赖项,因此我们在load_data.py文件中使用它来使我们的生活更轻松。

All of our data is in the data.json file; we need to load it into Pandas and transform it into a Dataframe.

我们所有的数据都在data.json文件中; 我们需要将其加载到Pandas并将其转换为Dataframe。

import pandas as pd
import streamlit as st  # pylint: disable=import-error
@st.cache
def load_data(path):
    """Loads the dataset from a filepath."""
    return (
        pd.read_json(path)
        .rename(
            columns={
                "Total Results as of Date": "date",
                "Cases": "cumulative_cases",
                "Deaths": "cumulative_deaths",
                "Recovered": "cumulative_recoveries",
            }
        )
        .assign(date=lambda df: pd.to_datetime(df["date"]))
        .set_index("date")
    )

The rest of our app code is relatively simple and presents us with the dropdown options and select functions. This allows our app to get data and generate plots when a user selects an option from the dropdown.

我们其余的应用程序代码相对简单,为我们提供了下拉选项和选择功能。 当用户从下拉菜单中选择一个选项时,这允许我们的应用获取数据并生成图。

Check out the app.py code here.

在此处查看app.py代码。

运行应用 (Run the App)

Streamlit lets us write apps like they are Python scripts. This means that when a new selection is picked in our dropdown, the entire app file runs again to generate the plot.

Streamlit使我们可以像编写Python脚本一样编写应用程序。 这意味着当在下拉菜单中选择新的选择时,整个应用程序文件将再次运行以生成绘图。

Let run our app and see the default view. Use the following line to run your streamlit app on local port 8000.

让我们运行我们的应用程序并查看默认视图。 使用以下行在本地端口8000上运行streamlit app。

streamlit run app/app.py --server.port 8000

Our app is set up to load data and generate the ‘All — cumulative cases’ plot by default seen below.

我们的应用程序设置为加载数据并默认生成“全部-累积案例”图,如下所示。

Image for post
COVID forecast — Cumulative data
COVID预测-累积数据

Since our forecasting is live, it can take some time to generate plots after you select them from the dropdown. To reduce this loading time, we’ve added Streamlit cache to store results of the function, our plots.

由于我们的预测是实时的,因此从下拉列表中选择图表后,可能需要一些时间来生成图表。 为了减少加载时间,我们添加了Streamlit缓存来存储函数结果(绘图)。

Image for post
Dropdown options
下拉选项

Our app has a dropdown to select which plot you would like to view. When you choose an option that is not the default, FB prophet runs the forecast on demand. The resultant Dataframe is then passed to Streamlit, which displays our data.

我们的应用程序具有一个下拉菜单,用于选择您要查看的绘图。 当您选择非默认选项时,FB先知将按需运行预测。 然后将所得的数据帧传递到Streamlit,该流将显示我们的数据。

What we cache with Streamlit doesn’t have to be data. We can cache anything, like our forecasts! Notice the alert in yellow “Running make_forecast(…)” appears only the first time an option is selected. Then when we reselect “Cumulative Recoveries,” there is no forecast; instead, our plot is pulled out of the cache.

我们使用Streamlit缓存的内容不必是数据。 我们可以缓存任何内容,例如我们的预测! 请注意,仅在第一次选择选项时,以黄色“正在运行make_forecast(…)”警报出现。 然后,当我们重新选择“累积恢复”时,就没有预测。 而是将我们的绘图从缓存中拉出。

Image for post
Forecasting on demand
按需预测

This caching allows for smooth swapping between plots once they are forecasted.

一旦预测出地块,这种缓存可以在地块之间进行平滑的交换。

部署到Heroku (Deploy to Heroku)

To run our app on Heroku, we need to add a Procifle to our GitHub repository to serve our app on their platform. Here is our procfile.

要在Heroku上运行我们的应用,我们需要向GitHub存储库添加一个Procifle以在其平台上提供我们的应用。 这是我们的procfile。

web: sh setup.sh && streamlit run app/app.py

Notice that we are calling our setup.sh file to set our email and port on the Heroku machine. Then we tell streamlit to run our app. Just add this Procfile to your project root and you'll be able to run on Heroku.

注意,我们正在调用setup.sh文件以在Heroku机器上设置我们的电子邮件和端口。 然后,我们告诉streamlit运行我们的应用程序。 只需将此Procfile添加到项目根目录,即可在Heroku上运行。

Now let’s go to Heroku’s website and deploy our app from GitHub using the GUI.

现在,我们进入Heroku的网站,并使用GUI从GitHub部署我们的应用程序。

Image for post
Heroku GUI for App Deployment
用于应用程序部署的Heroku GUI

To connect your repository, select GitHub, enter the name of the repository and hit search. Once your username and repository appear, click connect. Now select the desired branch and click deploy.

要连接您的存储库,请选择GitHub,输入存储库的名称,然后点击搜索。 用户名和存储库出现后,单击“连接”。 现在选择所需的分支,然后单击部署。

Build logs will begin to populate a console on the page. Notice that Heroku looks for a requirements.txt file first then installs dependencies from Pipenv’s Pipfile.lock. If you don’t use Pipenv, you will need to have a requiremnts.txt for this build to occur.

构建日志将开始在页面上填充控制台。 请注意,Heroku首先会查找require.txt文件,然后从Pipenv的Pipfile.lock安装依赖项。 如果您不使用Pipenv,则需要具有requiremnts.txt才能进行此构建。

Once your environment has been built from the Pipfile.lock and the build is successful, you will see the below message.

从Pipfile.lock构建环境并且构建成功后,您将看到以下消息。

Image for post
Successful app deployment
成功部署应用

The app is successfully deployed! Click to view button to see the deployed app on Heroku. Now we can view our app live on Heroku and run forecasts on their platform.

该应用已成功部署! 单击查看按钮以查看Heroku上已部署的应用程序。 现在,我们可以在Heroku上实时查看我们的应用,并在其平台上运行预测。

Check out our app here!

在这里查看我们的应用程序!

自动部署 (Automatic deploy)

We can enable automatic deployment to have changes to the Github master be displayed on Heroku as they are pushed. If you use this method, you’ll want to be sure that you always have a working master branch.

我们可以启用自动部署,以使对Github主服务器的更改在推送时显示在Heroku上。 如果使用此方法,则需要确保始终有一个正常的master分支。

Image for post
Enable automatic deploys
启用自动部署

结论 (Conclusion)

To review we discussed Streamlit and Facebook Prophet as tools that allow us to make forecasting dashboards with minimal effort and no cost. Streamlit is opinionated, easy to use, and allows us to have our front and backend code in a single file.

回顾一下,我们讨论了Streamlit和Facebook Prophet,它们是使我们能够以最小的努力和免费的方式制作预测仪表板的工具。 Streamlit固执己见,易于使用,并允许我们将前端和后端代码存储在一个文件中。

We used these tools to make a simple dashboard showing COVID data, what will you use them for?

我们使用这些工具制作了一个简单的仪表板来显示COVID数据,您将它们用于什么目的?

Thank you for reading! Please let us know what tool you use for dashboards and what you are looking to learn more about.

感谢您的阅读! 请让我们知道您用于仪表板的工具以及您想了解更多的信息。

翻译自: https://towardsdatascience.com/deploying-a-prophet-forecasting-model-with-streamlit-to-heroku-caf1729bd917

heroku

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值