File size: 5,114 Bytes
558b776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66823ff
 
 
558b776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6a73dd
558b776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
166
"""Shared production UI with explicitly simulated sessions; no models or physics."""

import json
import os
import sys
import time
from pathlib import Path

os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

import gradio as gr  # noqa: E402

from check_assets import check_assets  # noqa: E402
from ui import (
    FORCE_LIGHT_JS,
    NEXT_SESSION,
    THEME,
    build_demo,
    session_controls,
    status_html,
)  # noqa: E402

# Exact labels from hf-libero 0.1.4, used only by this simulator-free preview.
TASKS = json.loads((ROOT / "scripts/preview_tasks.json").read_text())
SESSIONS = {}


def send(kind, text, request):
    text = text.strip()
    session = SESSIONS.get(request.session_hash)
    if not text or (kind == "stop" and session is None):
        return "", False, gr.skip()
    if kind == "ask" and session is None:
        # As in app.py: a question never starts a session.
        return text, False, gr.skip()
    if kind == "stop":
        session["stop"] = True
        return "", False, gr.skip()
    start = session is None
    if start:
        session = {"stop": False, "chat": [], "qa": []}
        SESSIONS[request.session_hash] = session
    target = session["qa"] if kind == "ask" else session["chat"]
    target.append({"role": "user", "content": text})
    if kind == "ask":
        session["qa"].append(
            {
                "role": "assistant",
                "content": "This is a design preview; scene questions need the connected VLM.",
            }
        )
    else:
        session["task"] = text if kind == "task" else TASKS[7]
        session["goal"] = text if kind == "goal" else None
        session["chat"].append(
            {
                "role": "assistant",
                "content": f"**Preview instruction:** {session['task']}\n\nSimulated selection for interface testing. The robot will not move.",
            }
        )
    return (
        "",
        start,
        status_html("starting", "Preparing the preview") if start else gr.skip(),
    )


def send_goal(text: str, request: gr.Request):
    # As in app.py: a goal that starts a session shows in the task log at once.
    box, start, status = send("goal", text, request)
    return box, start, status, [{"role": "user", "content": text.strip()}] if start else gr.skip()


def send_question(text: str, request: gr.Request):
    return send("ask", text, request)


def send_stop(request: gr.Request):
    return send("stop", "stop", request)


def run(start: bool, request: gr.Request):
    if not start:
        # As in app.py: yielding nothing would blank every output.
        yield gr.skip()
        return
    session = SESSIONS[request.session_hash]
    try:
        yield (
            gr.skip(),
            session["chat"],
            session["qa"],
            *session_controls(TASKS, "starting", "Preparing the preview"),
        )
        time.sleep(2)  # long enough to see the starting popup
        started = time.monotonic()
        while time.monotonic() - started < 12 and not session["stop"]:
            task = session.get("task", TASKS[8])
            yield (
                gr.skip(),
                session["chat"],
                session["qa"],
                *session_controls(
                    TASKS,
                    "steering" if session.get("goal") else "running",
                    task,
                    session.get("goal"),
                    12 - (time.monotonic() - started),
                    note="Simulated UI state · no robot motion",
                ),
            )
            time.sleep(0.25)
        outcome = "stopped" if session["stop"] else "done"
        session["chat"].append(
            {
                "role": "assistant",
                "content": f"Preview {outcome}. No simulation was executed.",
            }
        )
        yield (
            gr.skip(),
            session["chat"],
            session["qa"],
            *session_controls(
                TASKS, outcome, session.get("task", TASKS[8]), note=NEXT_SESSION
            ),
        )
    finally:
        SESSIONS.pop(request.session_hash, None)


def build_preview():
    check_assets()
    web = ROOT / "web"
    player = (
        (ROOT / "player.js")
        .read_text()
        .replace("__SCENE_URL__", json.dumps(f"/gradio_api/file={web / 'scene.glb'}"))
        .replace(
            "__CAMERAS__", json.dumps(json.loads((web / "cameras.json").read_text()))
        )
    )
    gr.set_static_paths(paths=[str(web)])
    return build_demo(
        TASKS,
        player,
        run=run,
        send_goal=send_goal,
        send_question=send_question,
        send_stop=send_stop,
        preview=True,
    )


if __name__ == "__main__":
    build_preview().queue().launch(
        server_name=os.environ.get("GRADIO_SERVER_NAME", "127.0.0.1"),
        server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")),
        css=(ROOT / "style.css").read_text(),
        theme=THEME,
        js=FORCE_LIGHT_JS,
        show_error=True,
    )