File size: 2,759 Bytes
dee645c
 
 
 
 
 
0838154
 
 
 
dee645c
 
 
 
 
 
cd3dd7d
dee645c
 
 
 
 
 
 
a627180
 
dee645c
 
a627180
 
 
4b0990a
a627180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b0990a
a627180
 
 
 
 
 
 
 
 
 
 
 
 
dee645c
a627180
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from pulsar_clip import PulsarCLIP, CONFIG_SPEC
from datetime import datetime
import gradio as gr


def generate(*args):
    pc = PulsarCLIP(dict([(k, t(v) if not isinstance(t, (tuple, list))
                           else (type(t[0])(v) if isinstance(t, tuple) else v))
                          for v, (k, v0, t) in zip(args,
                                                   (y for _, x in CONFIG_SPEC for y in x))]))
    frames = []
    for image in pc.generate():
        frames.append(image)
    from tqdm.auto import tqdm
    from subprocess import Popen, PIPE
    fps = 30
    video_path = f"{datetime.strftime(datetime.now(), '%Y-%m-%d-%H-%M-%S')}.mp4"
    if frames:
        p = Popen((f"ffmpeg -y -f image2pipe -vcodec png -r {fps} -i - -vcodec libx264 -r {fps} "
                   f"-pix_fmt yuv420p -crf 17 -preset fast ").split() + [str(video_path)], stdin=PIPE)
        for im in tqdm(frames):
            im.save(p.stdin, "PNG")
        p.stdin.close()
        p.wait()
    model_path = None  # TODO
    return [video_path, model_path]


with gr.Blocks() as ui:
    gr.Markdown("# Pulsar+CLIP")
    gr.Markdown("Generate 3D point clouds from text!")
    
    with gr.Group():
        gr.Markdown("## Settings")
        inputs = []
        defaults = []
        with gr.Tabs():
            for name, section in CONFIG_SPEC:
                with gr.TabItem(name):
                    for k, v0, t in section:
                        if t in (float, int):
                            element = gr.Number(label=k, value=v0)
                        elif t == str:
                            element = gr.Textbox(label=k, value=v0)
                        elif t == bool:
                            element = gr.Checkbox(label=k, value=v0)
                        elif isinstance(t, tuple):
                            element = gr.Slider(*t, label=k, value=v0)
                        elif isinstance(t, list):
                            element = gr.Dropdown(label=k, value=v0, choices=t)
                        else:
                            raise TypeError(f"Input format {t} should be one of str, int, bool, tuple, list")
                            element = 1/0
                        inputs.append(element)
                        defaults.append(v0)
    
    button = gr.Button("Run")
    gr.Markdown("## Result")
    with gr.Row():
        with gr.Column():
            video = gr.Video()
        with gr.Column():
            model = gr.Model3D()
    
    button.click(fn=generate, inputs=inputs, outputs=[video, model])
    
    gr.Markdown("## Examples")
    gr.Examples(fn=generate, inputs=inputs, outputs=[video, model],
                examples=[defaults], cache_examples=True, examples_per_page=1)

ui.launch()
demo = ui