ivelin commited on
Commit
1b3abf3
·
verified ·
1 Parent(s): 3c596c8

darts gradio demo space

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pypistats
3
+ from datetime import date
4
+ from dateutil.relativedelta import relativedelta
5
+ from darts import TimeSeries
6
+ from darts.models import ExponentialSmoothing
7
+ from matplotlib import pyplot
8
+ import pandas as pd
9
+
10
+
11
+ def get_forecast(lib, time):
12
+ data = pypistats.overall(lib, total=True, format="pandas")
13
+ data = data.groupby("category").get_group("with_mirrors").sort_values("date")
14
+ start_date = date.today() - relativedelta(months=int(time.split(" ")[0]))
15
+ df = data[(data["date"] > str(start_date))]
16
+
17
+ df1 = df[["date", "downloads"]]
18
+ df1["date"] = pd.to_datetime(df1["date"])
19
+ df1 = df1.set_index(["date"])
20
+
21
+ target = TimeSeries.from_dataframe(df1, freq="D")
22
+
23
+ model = ExponentialSmoothing()
24
+ model.fit(target)
25
+ prediction = model.predict(90, num_samples=500)
26
+
27
+ fig, axes = pyplot.subplots(figsize=(20, 12))
28
+ target.plot()
29
+ prediction.plot()
30
+ pyplot.legend()
31
+ return fig
32
+
33
+
34
+ with gr.Blocks() as demo:
35
+ gr.Markdown(
36
+ """
37
+ **Pypi Download Stats 📈 with Darts Forecasting**: see live download stats for popular open-source libraries 🤗 along with a 3 month forecast using Darts. The [ source code for this Gradio demo is here](https://huggingface.co/spaces/gradio/timeseries-forecasting-with-darts/blob/main/app.py).
38
+ """
39
+ )
40
+ with gr.Row():
41
+ lib = gr.Dropdown(
42
+ ["pandas", "scikit-learn", "torch", "prophet", "darts"],
43
+ label="Library",
44
+ value="darts",
45
+ )
46
+ time = gr.Dropdown(
47
+ ["3 months", "6 months", "9 months", "12 months"],
48
+ label="Downloads over the last...",
49
+ value="12 months",
50
+ )
51
+
52
+ plt = gr.Plot()
53
+
54
+ lib.change(get_forecast, [lib, time], plt, queue=False)
55
+ time.change(get_forecast, [lib, time], plt, queue=False)
56
+ demo.load(get_forecast, [lib, time], plt, queue=False)
57
+
58
+ demo.launch()