File size: 1,860 Bytes
0bd62e5
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: stock_forecast"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio numpy matplotlib"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "import gradio as gr\n", "\n", "def plot_forecast(final_year, companies, noise, show_legend, point_style):\n", "    start_year = 2020\n", "    x = np.arange(start_year, final_year + 1)\n", "    year_count = x.shape[0]\n", "    plt_format = ({\"cross\": \"X\", \"line\": \"-\", \"circle\": \"o--\"})[point_style]\n", "    fig = plt.figure()\n", "    ax = fig.add_subplot(111)\n", "    for i, company in enumerate(companies):\n", "        series = np.arange(0, year_count, dtype=float)\n", "        series = series**2 * (i + 1)\n", "        series += np.random.rand(year_count) * noise\n", "        ax.plot(x, series, plt_format)\n", "    if show_legend:\n", "        plt.legend(companies)\n", "    return fig\n", "\n", "demo = gr.Interface(\n", "    plot_forecast,\n", "    [\n", "        gr.Radio([2025, 2030, 2035, 2040], label=\"Project to:\"),\n", "        gr.CheckboxGroup([\"Google\", \"Microsoft\", \"Gradio\"], label=\"Company Selection\"),\n", "        gr.Slider(1, 100, label=\"Noise Level\"),\n", "        gr.Checkbox(label=\"Show Legend\"),\n", "        gr.Dropdown([\"cross\", \"line\", \"circle\"], label=\"Style\"),\n", "    ],\n", "    gr.Plot(label=\"forecast\", format=\"png\"),\n", ")\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}