File size: 5,906 Bytes
bd65e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import datetime
from pathlib import Path
import polars as pl
import solara

from solara_app import sol_utils
from solara_app.css import ALIGN_CENTER, JUSTIFY_CENTER
from solara_app.folders import CHECKPOINTS, CONVERTED
from solara_app.infer import solara_run_inference
from solara_app.mini_components.c_inference import write_full_video, write_video
from solara_app.mini_components.simple import Progress, ProgressDynamic, Video
from utils import time_slice


def false() -> bool:
    return False


@solara.component
def DfSelectComponent(df: pl.DataFrame, file: str):
    extend_forward = solara.use_reactive({})
    extend_backward = solara.use_reactive({})
    disabled = solara.use_reactive({})
    selected_vid, set_selected_vid = solara.use_state(0)
    cut_off = solara.use_reactive(5)
    start_stop: solara.Reactive[list[datetime.datetime]] = solara.use_reactive(
        [df["timestamp"].min(), df["timestamp"].max()]
    )  # type: ignore
    clicks, set_clicks = solara.use_state(0)

    with solara.Card(
        "Highlight Selection & Editing",
        "Select highlight threshold, remove or expand clips",
    ):
        sol_utils.CutOffChartSelection(cut_off, start_stop, df)
        df = df.filter(
            pl.col("timestamp").is_between(start_stop.value[0], start_stop.value[1])
        )

        time_df = time_slice.create_start_end_time(
            df, cut_off.value, extend_forward.value, extend_backward.value
        )

        time_dict = time_df.select(pl.all().dt.strftime("%H:%M:%S")).to_dicts()
        time_dict = solara.use_reactive(time_dict)
        file_name = f"{file.replace('converted', 'downloaded')}.mp4"

        if len(time_dict.value) == 0:
            solara.Warning("No Highlights available...")
            return

        tstamp = time_dict.value[selected_vid]
        res = write_video.use_thread(
            tstamp["start"],
            tstamp["end"],
            selected_vid,
            Path(file_name).stem,
        )

        ProgressDynamic("Building Clip...", res)

        # TODO: extract into component.
        if res.state == solara.ResultState.FINISHED:
            with solara.Row(style={**JUSTIFY_CENTER, **ALIGN_CENTER}):
                solara.Button(
                    "<",
                    disabled=selected_vid == 0,
                    on_click=lambda: set_selected_vid(selected_vid - 1),
                )
                Video(res.value)
                solara.Button(
                    ">",
                    disabled=selected_vid == (len(time_dict.value) - 1),
                    on_click=lambda: set_selected_vid(selected_vid + 1),
                )

            with solara.Column(style=JUSTIFY_CENTER):
                with solara.Row(style=JUSTIFY_CENTER):
                    solara.InputInt(
                        "Expand Leftwards (s)",
                        value=extend_backward.value.get(selected_vid, 0),
                        on_value=lambda v: extend_backward.set(
                            {**extend_backward.value, selected_vid: v}
                        ),
                    )
                    solara.InputInt(
                        "Expand Rightwards (s)",
                        value=extend_forward.value.get(selected_vid, 0),
                        on_value=lambda v: extend_forward.set(
                            {**extend_forward.value, selected_vid: v}
                        ),
                    )

                def disable_vid(vid: int):
                    return lambda: disabled.set(
                        {**disabled.value, vid: not disabled.value.get(vid)}
                    )

                solara.Button(
                    (
                        "✅ Add Video"
                        if disabled.value.get(selected_vid)
                        else "❌ Remove Video"
                    ),
                    on_click=disable_vid(selected_vid),
                    style={"width": "25%"},
                )

        with solara.Card("Full Video", "Build the full video!"):
            solara.Button(
                "Build Full Video",
                color="primary",
                on_click=lambda: set_clicks(clicks + 1),
            )

            if clicks > 0:
                res_full = write_full_video.use_thread(
                    time_dict.value,
                    disabled.value,
                    Path(file_name).stem,
                    str(time_dict),
                )
                if res_full.state == solara.ResultState.RUNNING:
                    Progress("Building Full Clip...")
                elif res_full.state == solara.ResultState.FINISHED:
                    solara.FileDownload(
                        lambda: open(res_full.value, "rb"), Path(res_full.value).name
                    )


@solara.component
def ShowDfComponent(model: str, file: str):
    df = solara_run_inference.use_thread(
        Path(model),
        Path(file),
        aggregate_duration=10,
    )

    if df.state == solara.ResultState.RUNNING:
        Progress("Running...")
    elif df.state == solara.ResultState.FINISHED and df.value is not None:
        DfSelectComponent(df.value, file)


@solara.component()
def Inference():
    files = [str(p) for p in CONVERTED.glob("*") if p.is_dir()]
    models = [str(p) for p in CHECKPOINTS.rglob("*.ckpt")]
    file = solara.use_reactive(files[0] if len(files) else None)
    model = solara.use_reactive(models[0] if len(models) else None)

    if model.value is None or file.value is None:
        return solara.Markdown(
            "**It's required to at least download one stream and have one model available!**"
        )

    clicked = solara.use_reactive(False)

    sol_utils.ModelFileSelectComponent(file, model, clicked)

    if clicked.value:
        ShowDfComponent(model.value, file.value)
    else:
        solara.Markdown("**Start running to get further. 🚀**")