gradio/demo/stock_forecast.py

39 lines
1.2 KiB
Python
Raw Normal View History

2020-08-26 00:24:14 +08:00
# Demo: (Radio, CheckboxGroup, Slider, Checkbox, Dropdown) -> (Image)
2020-08-22 05:20:05 +08:00
2020-06-16 02:57:49 +08:00
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
2020-07-09 06:09:52 +08:00
2020-08-20 05:27:22 +08:00
def stock_forecast(final_year, companies, noise, show_legend, point_style):
2020-06-16 02:57:49 +08:00
start_year = 2020
x = np.arange(start_year, final_year + 1)
year_count = x.shape[0]
plt_format = ({"cross": "X", "line": "-", "circle": "o--"})[point_style]
2020-09-08 02:40:57 +08:00
fig = plt.figure()
ax = fig.add_subplot(111)
2020-06-16 02:57:49 +08:00
for i, company in enumerate(companies):
series = np.arange(0, year_count, dtype=float)
series = series ** 2 * (i + 1)
series += np.random.rand(year_count) * noise
2020-09-08 02:40:57 +08:00
ax.plot(x, series, plt_format)
2020-06-16 02:57:49 +08:00
if show_legend:
plt.legend(companies)
2020-09-23 21:47:55 +08:00
plt.close()
2020-09-08 02:40:57 +08:00
return fig
2020-06-16 02:57:49 +08:00
2020-07-09 06:09:52 +08:00
2020-11-11 22:15:53 +08:00
iface = gr.Interface(
2020-08-28 23:56:03 +08:00
stock_forecast,
[
gr.inputs.Radio([2025, 2030, 2035, 2040], label="Project to:"),
gr.inputs.CheckboxGroup(["Google", "Microsoft", "Gradio"]),
gr.inputs.Slider(1, 100),
"checkbox",
gr.inputs.Dropdown(["cross", "line", "circle"], label="Style")],
gr.outputs.Image(plot=True, label="forecast"))
2020-11-11 22:15:53 +08:00
iface.test_launch()
if __name__ == "__main__":
iface.launch()