Spaces:
Sleeping
Sleeping
Actually include app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import urllib
|
2 |
+
from collections import namedtuple
|
3 |
+
from math import cos, sin
|
4 |
+
from typing import Any
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
import numpy as np
|
8 |
+
import rerun as rr
|
9 |
+
import rerun.blueprint as rrb
|
10 |
+
from fastapi import FastAPI
|
11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
12 |
+
|
13 |
+
CUSTOM_PATH = "/"
|
14 |
+
|
15 |
+
app = FastAPI()
|
16 |
+
|
17 |
+
origins = [
|
18 |
+
"https://app.rerun.io",
|
19 |
+
]
|
20 |
+
|
21 |
+
app.add_middleware(
|
22 |
+
CORSMiddleware,
|
23 |
+
allow_origins=origins,
|
24 |
+
)
|
25 |
+
|
26 |
+
|
27 |
+
ColorGrid = namedtuple("ColorGrid", ["positions", "colors"])
|
28 |
+
|
29 |
+
|
30 |
+
def build_color_grid(x_count: int = 10, y_count: int = 10, z_count: int = 10, twist: float = 0) -> ColorGrid:
|
31 |
+
"""
|
32 |
+
Create a cube of points with colors.
|
33 |
+
|
34 |
+
The total point cloud will have x_count * y_count * z_count points.
|
35 |
+
|
36 |
+
Parameters
|
37 |
+
----------
|
38 |
+
x_count, y_count, z_count:
|
39 |
+
Number of points in each dimension.
|
40 |
+
twist:
|
41 |
+
Angle to twist from bottom to top of the cube
|
42 |
+
|
43 |
+
"""
|
44 |
+
|
45 |
+
grid = np.mgrid[
|
46 |
+
slice(-x_count, x_count, x_count * 1j),
|
47 |
+
slice(-y_count, y_count, y_count * 1j),
|
48 |
+
slice(-z_count, z_count, z_count * 1j),
|
49 |
+
]
|
50 |
+
|
51 |
+
angle = np.linspace(-float(twist) / 2, float(twist) / 2, z_count)
|
52 |
+
for z in range(z_count):
|
53 |
+
xv, yv, zv = grid[:, :, :, z]
|
54 |
+
rot_xv = xv * cos(angle[z]) - yv * sin(angle[z])
|
55 |
+
rot_yv = xv * sin(angle[z]) + yv * cos(angle[z])
|
56 |
+
grid[:, :, :, z] = [rot_xv, rot_yv, zv]
|
57 |
+
|
58 |
+
positions = np.vstack([xyz.ravel() for xyz in grid])
|
59 |
+
|
60 |
+
colors = np.vstack([
|
61 |
+
xyz.ravel()
|
62 |
+
for xyz in np.mgrid[
|
63 |
+
slice(0, 255, x_count * 1j),
|
64 |
+
slice(0, 255, y_count * 1j),
|
65 |
+
slice(0, 255, z_count * 1j),
|
66 |
+
]
|
67 |
+
])
|
68 |
+
|
69 |
+
return ColorGrid(positions.T, colors.T.astype(np.uint8))
|
70 |
+
|
71 |
+
|
72 |
+
def html_template(rrd: str, app_url: str = "https://app.rerun.io") -> str:
|
73 |
+
encoded_url = urllib.parse.quote(rrd)
|
74 |
+
return f"""<div style="width:100%; height:70vh;"><iframe style="width:100%; height:100%;" src="{app_url}?url={encoded_url}" frameborder="0" allowfullscreen=""></iframe></div>"""
|
75 |
+
|
76 |
+
|
77 |
+
def show_cube(x: int, y: int, z: int) -> str:
|
78 |
+
rr.init("my data")
|
79 |
+
|
80 |
+
cube = build_color_grid(int(x), int(y), int(z), twist=0)
|
81 |
+
rr.log("cube", rr.Points3D(cube.positions, colors=cube.colors, radii=0.5))
|
82 |
+
|
83 |
+
blueprint = rrb.Spatial3DView(origin="cube")
|
84 |
+
|
85 |
+
rr.save("cube.rrd", default_blueprint=blueprint)
|
86 |
+
|
87 |
+
return "cube.rrd"
|
88 |
+
|
89 |
+
|
90 |
+
with gr.Blocks() as demo:
|
91 |
+
with gr.Row():
|
92 |
+
x_count = gr.Number(minimum=1, maximum=10, value=5, precision=0, label="X Count")
|
93 |
+
y_count = gr.Number(minimum=1, maximum=10, value=5, precision=0, label="Y Count")
|
94 |
+
z_count = gr.Number(minimum=1, maximum=10, value=5, precision=0, label="Z Count")
|
95 |
+
button = gr.Button("Show Cube")
|
96 |
+
with gr.Row():
|
97 |
+
rrd = gr.File()
|
98 |
+
with gr.Row():
|
99 |
+
viewer = gr.HTML()
|
100 |
+
|
101 |
+
button.click(show_cube, inputs=[x_count, y_count, z_count], outputs=rrd)
|
102 |
+
rrd.change(
|
103 |
+
html_template,
|
104 |
+
js="""(rrd) => { console.log(rrd.url); return rrd.url}""",
|
105 |
+
inputs=[rrd],
|
106 |
+
outputs=viewer,
|
107 |
+
preprocess=False,
|
108 |
+
)
|
109 |
+
|
110 |
+
|
111 |
+
app = gr.mount_gradio_app(app, demo, path=CUSTOM_PATH)
|
112 |
+
|
113 |
+
|
114 |
+
# Run this from the terminal as you would normally start a FastAPI app: `uvicorn run:app`
|
115 |
+
# and navigate to http://localhost:8000 in your browser.
|