Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
import numpy
|
2 |
-
import gradio as gr
|
3 |
import plotly.graph_objects as go
|
4 |
|
5 |
import torch
|
@@ -11,25 +9,32 @@ from point_e.models.download import load_checkpoint
|
|
11 |
from point_e.models.configs import MODEL_CONFIGS, model_from_config
|
12 |
from point_e.util.plotting import plot_point_cloud
|
13 |
|
|
|
|
|
|
|
14 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
15 |
|
16 |
-
|
|
|
17 |
base_name = 'base40M-textvec'
|
18 |
base_model = model_from_config(MODEL_CONFIGS[base_name], device)
|
19 |
base_model.eval()
|
20 |
base_diffusion = diffusion_from_config(DIFFUSION_CONFIGS[base_name])
|
21 |
|
22 |
-
|
|
|
23 |
upsampler_model = model_from_config(MODEL_CONFIGS['upsample'], device)
|
24 |
upsampler_model.eval()
|
25 |
upsampler_diffusion = diffusion_from_config(DIFFUSION_CONFIGS['upsample'])
|
26 |
|
27 |
-
|
|
|
28 |
base_model.load_state_dict(load_checkpoint(base_name, device))
|
29 |
|
30 |
-
print('
|
31 |
upsampler_model.load_state_dict(load_checkpoint('upsample', device))
|
32 |
|
|
|
33 |
sampler = PointCloudSampler(
|
34 |
device=device,
|
35 |
models=[base_model, upsampler_model],
|
@@ -37,50 +42,34 @@ sampler = PointCloudSampler(
|
|
37 |
num_points=[1024, 4096 - 1024],
|
38 |
aux_channels=['R', 'G', 'B'],
|
39 |
guidance_scale=[3.0, 0.0],
|
40 |
-
model_kwargs_key_filter=('texts', ''),
|
41 |
)
|
42 |
|
43 |
-
|
|
|
|
|
44 |
samples = None
|
45 |
-
for x in sampler.sample_batch_progressive(batch_size=1, model_kwargs=dict(texts=[
|
46 |
samples = x
|
|
|
|
|
47 |
pc = sampler.output_to_point_clouds(samples)[0]
|
48 |
-
pc = sampler.output_to_point_clouds(samples)[0]
|
49 |
-
colors=(238, 75, 43)
|
50 |
-
fig = go.Figure(
|
51 |
-
data=[
|
52 |
-
go.Scatter3d(
|
53 |
-
x=pc.coords[:,0], y=pc.coords[:,1], z=pc.coords[:,2],
|
54 |
-
mode='markers',
|
55 |
-
marker=dict(
|
56 |
-
size=2,
|
57 |
-
color=['rgb({},{},{})'.format(r,g,b) for r,g,b in zip(pc.channels["R"], pc.channels["G"], pc.channels["B"])],
|
58 |
-
)
|
59 |
-
)
|
60 |
-
],
|
61 |
-
layout=dict(
|
62 |
-
scene=dict(
|
63 |
-
xaxis=dict(visible=False),
|
64 |
-
yaxis=dict(visible=False),
|
65 |
-
zaxis=dict(visible=False)
|
66 |
-
)
|
67 |
-
),
|
68 |
-
)
|
69 |
-
return fig
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
demo = gr.Interface(
|
72 |
-
fn=
|
73 |
inputs="text",
|
74 |
-
outputs=gr.
|
75 |
-
|
76 |
-
|
77 |
-
["a RED pumpkin"],
|
78 |
-
["a yellow rubber duck"]
|
79 |
-
],
|
80 |
-
title="Point-E demo: text to 3D",
|
81 |
-
description="""Generated 3D Point Clouds with [Point-E](https://github.com/openai/point-e/tree/main). This demo uses a small, worse quality text-to-3D model to produce 3D point clouds directly from text descriptions.
|
82 |
-
Check out the [notebook](https://github.com/openai/point-e/blob/main/point_e/examples/text2pointcloud.ipynb).
|
83 |
-
"""
|
84 |
)
|
|
|
|
|
85 |
demo.queue(max_size=30)
|
86 |
-
demo.launch(debug=True)
|
|
|
|
|
|
|
1 |
import plotly.graph_objects as go
|
2 |
|
3 |
import torch
|
|
|
9 |
from point_e.models.configs import MODEL_CONFIGS, model_from_config
|
10 |
from point_e.util.plotting import plot_point_cloud
|
11 |
|
12 |
+
import gradio as gr
|
13 |
+
|
14 |
+
# Select device (CUDA if available, otherwise CPU)
|
15 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
16 |
|
17 |
+
# Initialize base model
|
18 |
+
print('Creating base model...')
|
19 |
base_name = 'base40M-textvec'
|
20 |
base_model = model_from_config(MODEL_CONFIGS[base_name], device)
|
21 |
base_model.eval()
|
22 |
base_diffusion = diffusion_from_config(DIFFUSION_CONFIGS[base_name])
|
23 |
|
24 |
+
# Initialize upsample model
|
25 |
+
print('Creating upsample model...')
|
26 |
upsampler_model = model_from_config(MODEL_CONFIGS['upsample'], device)
|
27 |
upsampler_model.eval()
|
28 |
upsampler_diffusion = diffusion_from_config(DIFFUSION_CONFIGS['upsample'])
|
29 |
|
30 |
+
# Load checkpoints
|
31 |
+
print('Downloading base checkpoint...')
|
32 |
base_model.load_state_dict(load_checkpoint(base_name, device))
|
33 |
|
34 |
+
print('Downloading upsampler checkpoint...')
|
35 |
upsampler_model.load_state_dict(load_checkpoint('upsample', device))
|
36 |
|
37 |
+
# Initialize sampler
|
38 |
sampler = PointCloudSampler(
|
39 |
device=device,
|
40 |
models=[base_model, upsampler_model],
|
|
|
42 |
num_points=[1024, 4096 - 1024],
|
43 |
aux_channels=['R', 'G', 'B'],
|
44 |
guidance_scale=[3.0, 0.0],
|
45 |
+
model_kwargs_key_filter=('texts', ''), # Do not condition the upsampler at all
|
46 |
)
|
47 |
|
48 |
+
# Function to create point clouds
|
49 |
+
def create_point_cloud(inp):
|
50 |
+
# Generate progressive samples
|
51 |
samples = None
|
52 |
+
for x in tqdm(sampler.sample_batch_progressive(batch_size=1, model_kwargs=dict(texts=[inp]))):
|
53 |
samples = x
|
54 |
+
|
55 |
+
# Extract the point cloud
|
56 |
pc = sampler.output_to_point_clouds(samples)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
# Generate a Plotly figure for visualization
|
59 |
+
fig = plot_point_cloud(pc, grid_size=3, fixed_bounds=((-0.75, -0.75, -0.75), (0.75, 0.75, 0.75)))
|
60 |
+
|
61 |
+
# Convert Plotly figure to HTML for Gradio compatibility
|
62 |
+
return fig.to_html(full_html=False)
|
63 |
+
|
64 |
+
# Create Gradio interface
|
65 |
demo = gr.Interface(
|
66 |
+
fn=create_point_cloud,
|
67 |
inputs="text",
|
68 |
+
outputs=gr.HTML(), # Gradio expects HTML for Plotly visualizations
|
69 |
+
title="Point-E Demo - Convert Text to 3D Point Clouds",
|
70 |
+
description="Generate and visualize 3D point clouds from textual descriptions using OpenAI's Point-E framework."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
)
|
72 |
+
|
73 |
+
# Enable queuing and launch Gradio app
|
74 |
demo.queue(max_size=30)
|
75 |
+
demo.launch(debug=True)
|