Spaces:
Runtime error
Runtime error
Upload with huggingface_hub
Browse files
README.md
CHANGED
@@ -1,12 +1,11 @@
|
|
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: 🔥
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.12.0
|
8 |
-
app_file:
|
9 |
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
|
2 |
---
|
3 |
+
title: altair_plot_main
|
4 |
emoji: 🔥
|
5 |
+
colorFrom: indigo
|
6 |
+
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
sdk_version: 3.12.0
|
9 |
+
app_file: run.py
|
10 |
pinned: false
|
11 |
---
|
|
|
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
altair
|
2 |
+
vega_datasets
|
3 |
+
https://gradio-main-build.s3.amazonaws.com/bed288a509ebd0edd56f812bc14fd947084e0933/gradio-3.12.0-py3-none-any.whl
|
run.ipynb
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: altair_plot"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio altair vega_datasets"]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import altair as alt\n", "import gradio as gr\n", "import numpy as np\n", "import pandas as pd\n", "from vega_datasets import data\n", "\n", "\n", "def make_plot(plot_type):\n", " if plot_type == \"scatter_plot\":\n", " cars = data.cars()\n", " return alt.Chart(cars).mark_point().encode(\n", " x='Horsepower',\n", " y='Miles_per_Gallon',\n", " color='Origin',\n", " )\n", " elif plot_type == \"heatmap\":\n", " # Compute x^2 + y^2 across a 2D grid\n", " x, y = np.meshgrid(range(-5, 5), range(-5, 5))\n", " z = x ** 2 + y ** 2\n", "\n", " # Convert this grid to columnar data expected by Altair\n", " source = pd.DataFrame({'x': x.ravel(),\n", " 'y': y.ravel(),\n", " 'z': z.ravel()})\n", " return alt.Chart(source).mark_rect().encode(\n", " x='x:O',\n", " y='y:O',\n", " color='z:Q'\n", " )\n", " elif plot_type == \"us_map\":\n", " states = alt.topo_feature(data.us_10m.url, 'states')\n", " source = data.income.url\n", "\n", " return alt.Chart(source).mark_geoshape().encode(\n", " shape='geo:G',\n", " color='pct:Q',\n", " tooltip=['name:N', 'pct:Q'],\n", " facet=alt.Facet('group:N', columns=2),\n", " ).transform_lookup(\n", " lookup='id',\n", " from_=alt.LookupData(data=states, key='id'),\n", " as_='geo'\n", " ).properties(\n", " width=300,\n", " height=175,\n", " ).project(\n", " type='albersUsa'\n", " )\n", " elif plot_type == \"interactive_barplot\":\n", " source = data.movies.url\n", "\n", " pts = alt.selection(type=\"single\", encodings=['x'])\n", "\n", " rect = alt.Chart(data.movies.url).mark_rect().encode(\n", " alt.X('IMDB_Rating:Q', bin=True),\n", " alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),\n", " alt.Color('count()',\n", " scale=alt.Scale(scheme='greenblue'),\n", " legend=alt.Legend(title='Total Records')\n", " )\n", " )\n", "\n", " circ = rect.mark_point().encode(\n", " alt.ColorValue('grey'),\n", " alt.Size('count()',\n", " legend=alt.Legend(title='Records in Selection')\n", " )\n", " ).transform_filter(\n", " pts\n", " )\n", "\n", " bar = alt.Chart(source).mark_bar().encode(\n", " x='Major_Genre:N',\n", " y='count()',\n", " color=alt.condition(pts, alt.ColorValue(\"steelblue\"), alt.ColorValue(\"grey\"))\n", " ).properties(\n", " width=550,\n", " height=200\n", " ).add_selection(pts)\n", "\n", " plot = alt.vconcat(\n", " rect + circ,\n", " bar\n", " ).resolve_legend(\n", " color=\"independent\",\n", " size=\"independent\"\n", " )\n", " return plot\n", " elif plot_type == \"radial\":\n", " source = pd.DataFrame({\"values\": [12, 23, 47, 6, 52, 19]})\n", "\n", " base = alt.Chart(source).encode(\n", " theta=alt.Theta(\"values:Q\", stack=True),\n", " radius=alt.Radius(\"values\", scale=alt.Scale(type=\"sqrt\", zero=True, rangeMin=20)),\n", " color=\"values:N\",\n", " )\n", "\n", " c1 = base.mark_arc(innerRadius=20, stroke=\"#fff\")\n", "\n", " c2 = base.mark_text(radiusOffset=10).encode(text=\"values:Q\")\n", "\n", " return c1 + c2\n", " elif plot_type == \"multiline\":\n", " source = data.stocks()\n", "\n", " highlight = alt.selection(type='single', on='mouseover',\n", " fields=['symbol'], nearest=True)\n", "\n", " base = alt.Chart(source).encode(\n", " x='date:T',\n", " y='price:Q',\n", " color='symbol:N'\n", " )\n", "\n", " points = base.mark_circle().encode(\n", " opacity=alt.value(0)\n", " ).add_selection(\n", " highlight\n", " ).properties(\n", " width=600\n", " )\n", "\n", " lines = base.mark_line().encode(\n", " size=alt.condition(~highlight, alt.value(1), alt.value(3))\n", " )\n", "\n", " return points + lines\n", "\n", "\n", "with gr.Blocks() as demo:\n", " button = gr.Radio(label=\"Plot type\",\n", " choices=['scatter_plot', 'heatmap', 'us_map',\n", " 'interactive_barplot', \"radial\", \"multiline\"], value='scatter_plot')\n", " plot = gr.Plot(label=\"Plot\")\n", " button.change(make_plot, inputs=button, outputs=[plot])\n", " demo.load(make_plot, inputs=[button], outputs=[plot])\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
run.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|