File size: 2,542 Bytes
b7646ff
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: timeseries-forecasting-with-prophet\n", "### A simple dashboard showing pypi stats for python libraries. Updates on load, and has no buttons!\n", "        "]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio holidays==0.24 prophet==1.1.2 pandas pypistats plotly"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import pypistats\n", "from datetime import date\n", "from dateutil.relativedelta import relativedelta\n", "import pandas as pd\n", "from prophet import Prophet\n", "pd.options.plotting.backend = \"plotly\"\n", "\n", "def get_forecast(lib, time):\n", "\n", "    data = pypistats.overall(lib, total=True, format=\"pandas\")\n", "    data = data.groupby(\"category\").get_group(\"with_mirrors\").sort_values(\"date\")\n", "    start_date = date.today() - relativedelta(months=int(time.split(\" \")[0]))\n", "    df = data[(data['date'] > str(start_date))] \n", "\n", "    df1 = df[['date','downloads']]\n", "    df1.columns = ['ds','y']\n", "\n", "    m = Prophet()\n", "    m.fit(df1)\n", "    future = m.make_future_dataframe(periods=90)\n", "    forecast = m.predict(future)\n", "    fig1 = m.plot(forecast)\n", "    return fig1 \n", "\n", "with gr.Blocks() as demo:\n", "    gr.Markdown(\n", "    \"\"\"\n", "    **Pypi Download Stats \ud83d\udcc8 with Prophet Forecasting**: see live download stats for popular open-source libraries \ud83e\udd17 along with a 3 month forecast using Prophet. The [ source code for this Gradio demo is here](https://huggingface.co/spaces/gradio/timeseries-forecasting-with-prophet/blob/main/app.py).\n", "    \"\"\")\n", "    with gr.Row():\n", "        lib = gr.Dropdown([\"pandas\", \"scikit-learn\", \"torch\", \"prophet\"], label=\"Library\", value=\"pandas\")\n", "        time = gr.Dropdown([\"3 months\", \"6 months\", \"9 months\", \"12 months\"], label=\"Downloads over the last...\", value=\"12 months\")\n", "\n", "    plt = gr.Plot()\n", "\n", "    lib.change(get_forecast, [lib, time], plt, queue=False)\n", "    time.change(get_forecast, [lib, time], plt, queue=False)    \n", "    demo.load(get_forecast, [lib, time], plt, queue=False)    \n", "\n", "demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}