masa729406 commited on
Commit
6a2c3c8
ยท
1 Parent(s): bc7ab0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -27
app.py CHANGED
@@ -1,36 +1,140 @@
1
- !pip install prophet
2
-
3
  import gradio as gr
 
4
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- from prophet import Prophet
 
 
 
 
8
 
 
9
 
10
- def plot_forecast(example_name, period):
11
- df = pd.read_csv(f'https://raw.githubusercontent.com/facebook/prophet/main/examples/example_{example_name}.csv')
12
- df.columns = ['ds','y']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- m = Prophet()
15
- m.fit(df)
16
- future = m.make_future_dataframe(periods=period)
17
- forecast = m.predict(future)
18
- fig = m.plot(forecast)
19
- return fig
20
 
21
  with gr.Blocks() as demo:
22
- gr.Markdown(
23
- """
24
- ๆ™‚็ณปๅˆ—ไบˆๆธฌใƒขใƒ‡ใƒซใฎ็ตๆžœ
25
- """)
26
- with gr.Row():
27
- example = gr.Dropdown(["air_passengers", "pedestrians_covid", "retail_sales"], label="ใƒ‡ใƒผใ‚ฟใ‚ฝใƒผใ‚น", value="air_passengers")
28
- period = gr.Slider(25, 250, 25, step=25, label="ไบˆๆธฌๆœŸ้–“")
29
-
30
- plt = gr.Plot()
31
-
32
- example.change(plot_forecast, [example,period], plt, queue=False)
33
- period.change(plot_forecast, [example,period], plt, queue=False)
34
- demo.load(plot_forecast, [example,period], plt, queue=False)
35
-
36
- demo.launch()
 
1
+ import altair as alt
 
2
  import gradio as gr
3
+ import numpy as np
4
  import pandas as pd
5
+ from vega_datasets import data
6
+
7
+
8
+ def make_plot(plot_type):
9
+ if plot_type == "scatter_plot":
10
+ cars = data.cars()
11
+ return alt.Chart(cars).mark_point().encode(
12
+ x='Horsepower',
13
+ y='Miles_per_Gallon',
14
+ color='Origin',
15
+ )
16
+ elif plot_type == "heatmap":
17
+ # Compute x^2 + y^2 across a 2D grid
18
+ x, y = np.meshgrid(range(-5, 5), range(-5, 5))
19
+ z = x ** 2 + y ** 2
20
+
21
+ # Convert this grid to columnar data expected by Altair
22
+ source = pd.DataFrame({'x': x.ravel(),
23
+ 'y': y.ravel(),
24
+ 'z': z.ravel()})
25
+ return alt.Chart(source).mark_rect().encode(
26
+ x='x:O',
27
+ y='y:O',
28
+ color='z:Q'
29
+ )
30
+ elif plot_type == "us_map":
31
+ states = alt.topo_feature(data.us_10m.url, 'states')
32
+ source = data.income.url
33
+
34
+ return alt.Chart(source).mark_geoshape().encode(
35
+ shape='geo:G',
36
+ color='pct:Q',
37
+ tooltip=['name:N', 'pct:Q'],
38
+ facet=alt.Facet('group:N', columns=2),
39
+ ).transform_lookup(
40
+ lookup='id',
41
+ from_=alt.LookupData(data=states, key='id'),
42
+ as_='geo'
43
+ ).properties(
44
+ width=300,
45
+ height=175,
46
+ ).project(
47
+ type='albersUsa'
48
+ )
49
+ elif plot_type == "interactive_barplot":
50
+ source = data.movies.url
51
+
52
+ pts = alt.selection(type="single", encodings=['x'])
53
+
54
+ rect = alt.Chart(data.movies.url).mark_rect().encode(
55
+ alt.X('IMDB_Rating:Q', bin=True),
56
+ alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),
57
+ alt.Color('count()',
58
+ scale=alt.Scale(scheme='greenblue'),
59
+ legend=alt.Legend(title='Total Records')
60
+ )
61
+ )
62
+
63
+ circ = rect.mark_point().encode(
64
+ alt.ColorValue('grey'),
65
+ alt.Size('count()',
66
+ legend=alt.Legend(title='Records in Selection')
67
+ )
68
+ ).transform_filter(
69
+ pts
70
+ )
71
+
72
+ bar = alt.Chart(source).mark_bar().encode(
73
+ x='Major_Genre:N',
74
+ y='count()',
75
+ color=alt.condition(pts, alt.ColorValue("steelblue"), alt.ColorValue("grey"))
76
+ ).properties(
77
+ width=550,
78
+ height=200
79
+ ).add_selection(pts)
80
 
81
+ plot = alt.vconcat(
82
+ rect + circ,
83
+ bar
84
+ ).resolve_legend(
85
+ color="independent",
86
+ size="independent"
87
+ )
88
+ return plot
89
+ elif plot_type == "radial":
90
+ source = pd.DataFrame({"values": [12, 23, 47, 6, 52, 19]})
91
 
92
+ base = alt.Chart(source).encode(
93
+ theta=alt.Theta("values:Q", stack=True),
94
+ radius=alt.Radius("values", scale=alt.Scale(type="sqrt", zero=True, rangeMin=20)),
95
+ color="values:N",
96
+ )
97
 
98
+ c1 = base.mark_arc(innerRadius=20, stroke="#fff")
99
 
100
+ c2 = base.mark_text(radiusOffset=10).encode(text="values:Q")
101
+
102
+ return c1 + c2
103
+ elif plot_type == "multiline":
104
+ source = data.stocks()
105
+
106
+ highlight = alt.selection(type='single', on='mouseover',
107
+ fields=['symbol'], nearest=True)
108
+
109
+ base = alt.Chart(source).encode(
110
+ x='date:T',
111
+ y='price:Q',
112
+ color='symbol:N'
113
+ )
114
+
115
+ points = base.mark_circle().encode(
116
+ opacity=alt.value(0)
117
+ ).add_selection(
118
+ highlight
119
+ ).properties(
120
+ width=600
121
+ )
122
+
123
+ lines = base.mark_line().encode(
124
+ size=alt.condition(~highlight, alt.value(1), alt.value(3))
125
+ )
126
+
127
+ return points + lines
128
 
 
 
 
 
 
 
129
 
130
  with gr.Blocks() as demo:
131
+ button = gr.Radio(label="Plot type",
132
+ choices=['scatter_plot', 'heatmap', 'us_map',
133
+ 'interactive_barplot', "radial", "multiline"], value='scatter_plot')
134
+ plot = gr.Plot(label="Plot")
135
+ button.change(make_plot, inputs=button, outputs=[plot])
136
+ demo.load(make_plot, inputs=[button], outputs=[plot])
137
+
138
+
139
+ if __name__ == "__main__":
140
+ demo.launch()