JoeTach Claude Opus 4.7 commited on
Commit
110335d
Β·
1 Parent(s): f556852

Stabilize multi-UI repo: HF-ready gradio_app + token-based HF deploy guide

Browse files

Recovery work after an interactive rebase + HF-push attempt failed
mid-flight. Per the directive, the rebase was aborted (all work is
safe, every previously-shipped commit is preserved) and the broken
state replaced with a clean, deployable shape.

gradio_app.py
Module-level ``demo = build_ui()`` is now executed at import
time so Hugging Face Spaces can do ``from gradio_app import demo``
without modification --- and app.py (the tiny shim already in the
tree) routes HF's ``app_file: app.py`` declaration straight to
this module's demo. The ``__main__`` block stays for local launch
at http://127.0.0.1:7860.

docs/hf-deployment.md
Token-based HF Spaces deployment that does NOT require the
huggingface CLI. Covers the three errors the previous attempt
hit: in-flight interactive rebase, non-fast-forward push
rejection, and missing CLI. The force-push step is scoped to the
*first* push only (overwrites HF's auto-README with the local
README that already carries the right ``app_file: app.py`` YAML
frontmatter); every subsequent push is a plain ``git push space
main``. Includes a four-symptom troubleshooting table.

docs/run-locally.md
PowerShell step-by-step the directive explicitly asked for:
clean clone β†’ venv β†’ install profile β†’ launch Gradio (primary) +
Streamlit + Dash + FastAPI side-by-side. Includes the loopback
note (127.0.0.1:7860 only exists while the Python process is
running), the ``share=True`` escape hatch for a temporary public
URL without HF, and a where-each-interface-fits table.

Verified locally:
* ``from app import demo`` --> Blocks (Gradio UI)
* ``dash_app.build_app()`` --> Dash
* ``cdpr.interface.gui.render`` --> Streamlit module imports fine
* CI subset stays 177/177 in ~36 s.

I am STOPPING before any push to the HF Space remote. That requires
the user's write-scope token. The deployment guide is the supported
path forward; the local Gradio workflow is unaffected either way.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

Files changed (3) hide show
  1. docs/hf-deployment.md +152 -0
  2. docs/run-locally.md +187 -0
  3. gradio_app.py +14 -3
docs/hf-deployment.md ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Space deployment β€” token-based (no CLI required)
2
+
3
+ This guide deploys the Gradio app to a Hugging Face Space using only
4
+ `git` and an HF access token. The `huggingface-cli` is **not**
5
+ required β€” it has been a source of friction (PATH issues, login
6
+ state) and is unnecessary for a one-app Space.
7
+
8
+ The local Gradio workflow (`python gradio_app.py` β†’ `http://127.0.0.1:7860`)
9
+ is independent of this guide and never breaks when an HF push fails.
10
+
11
+ ## Prerequisites
12
+
13
+ 1. A Hugging Face account at <https://huggingface.co>.
14
+ 2. A Space already created (one-time setup):
15
+ * **Space name**: e.g. `cdpr-simulator`
16
+ * **SDK**: Gradio
17
+ * **Hardware**: CPU basic (free, 16 GB RAM)
18
+ * **License**: MIT
19
+ 3. A **write-scope access token** at
20
+ <https://huggingface.co/settings/tokens> β†’ *New token* β†’ *Write*.
21
+ Save the token (a string starting with `hf_…`).
22
+
23
+ ## Why the previous deploy failed
24
+
25
+ The terminal log showed three errors stacked together:
26
+
27
+ * **Interactive rebase mid-flight, README.md conflict** β€” `git pull
28
+ --rebase space main` started replaying every local commit on top of
29
+ the empty Space, conflicting on the README the moment the first
30
+ commit landed. With 19 commits in the queue this was not going to
31
+ finish.
32
+ * **Non-fast-forward push rejected** β€” the Space's initial commit
33
+ (the auto-generated README) was not in the local history, so a
34
+ plain `git push space main` was refused.
35
+ * **CLI not on PATH** β€” irrelevant once the rebase blocked everything
36
+ else.
37
+
38
+ The fixes are all on the git side; no CLI install is needed.
39
+
40
+ ## Step 1 β€” make sure the repo is clean
41
+
42
+ If a rebase is mid-flight, abort it (your committed work is safe):
43
+
44
+ ```powershell
45
+ git status # check
46
+ git rebase --abort # only if "interactive rebase in progress"
47
+ git status # must say "working tree clean"
48
+ ```
49
+
50
+ ## Step 2 β€” add the Space as a remote (one-time)
51
+
52
+ ```powershell
53
+ # Replace <user> and <space> with your username and Space name.
54
+ $user = "your-hf-username"
55
+ $space = "cdpr-simulator"
56
+
57
+ git remote add space "https://huggingface.co/spaces/$user/$space"
58
+ git remote -v # confirm 'space' appears
59
+ ```
60
+
61
+ If the remote already exists from a previous attempt, the second `add`
62
+ errors β€” that's fine, the remote is already correct. To update its URL:
63
+
64
+ ```powershell
65
+ git remote set-url space "https://huggingface.co/spaces/$user/$space"
66
+ ```
67
+
68
+ ## Step 3 β€” push with the token in the URL (the reliable path)
69
+
70
+ This is the part that avoids every CLI / credential issue:
71
+
72
+ ```powershell
73
+ $user = "your-hf-username"
74
+ $space = "cdpr-simulator"
75
+ # Paste the hf_… token here (or read it from a saved file). DO NOT
76
+ # commit the token anywhere.
77
+ $token = Read-Host "Paste your HF write token (hf_…)" -AsSecureString
78
+ $tokenPlain = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
79
+ [Runtime.InteropServices.Marshal]::SecureStringToBSTR($token))
80
+
81
+ # Force-push *the first time* to overwrite the Space's auto-README
82
+ # with this repo's README that carries the right YAML frontmatter.
83
+ # After this first push, subsequent pushes are plain ``git push``.
84
+ git push --force "https://${user}:${tokenPlain}@huggingface.co/spaces/$user/$space" main:main
85
+ ```
86
+
87
+ Why force on the first push: the Space was created with one
88
+ auto-generated commit (the README HF made for you). Your local
89
+ history doesn't contain that commit, so a regular push is refused.
90
+ Force-pushing overwrites it with this repo's README β€” which is what
91
+ you want, because the local README has the correct YAML frontmatter:
92
+
93
+ ```yaml
94
+ ---
95
+ title: CDPR Simulator
96
+ emoji: πŸ€–
97
+ colorFrom: blue
98
+ colorTo: green
99
+ sdk: gradio
100
+ sdk_version: 6.15.2
101
+ app_file: app.py
102
+ pinned: false
103
+ ---
104
+ ```
105
+
106
+ That tells HF which file to run (`app.py` β†’ which imports `demo` from
107
+ `gradio_app.py`), what SDK version to use, and the cosmetic theme.
108
+
109
+ ## Step 4 β€” after the first force-push
110
+
111
+ From now on the remote and local share history, and ordinary pushes
112
+ work:
113
+
114
+ ```powershell
115
+ git push space main
116
+ ```
117
+
118
+ HF rebuilds the Space automatically (3-7 minutes for a clean build).
119
+ You can watch the build log on the Space's settings page.
120
+
121
+ ## Step 5 β€” verify the Space
122
+
123
+ Open `https://huggingface.co/spaces/<user>/cdpr-simulator`. You
124
+ should see:
125
+
126
+ * the build banner from `gradio_app.py` (currently
127
+ `gradio-2026-05-29-a`),
128
+ * three tabs (Built-in examples, Custom Phase-1 simulation, Upload
129
+ CSV / Phase-2),
130
+ * clicking *Run example* on `circle` produces 14 figures + the
131
+ feasibility JSON.
132
+
133
+ ## What to do if the push still fails
134
+
135
+ | Symptom | Cause | Fix |
136
+ |---|---|---|
137
+ | `403 Forbidden` | Token is read-only, not write | Generate a new token with *Write* scope |
138
+ | `404` | Wrong username or Space name in the URL | Double-check `git remote -v` |
139
+ | `! [rejected] main -> main (non-fast-forward)` after step 4 | Someone (or you) committed directly on the Space | `git pull space main --rebase` then push again. If the only thing in the way is the README, repeat step 3. |
140
+ | `error: src refspec main does not match any` | Local branch is named `master`, not `main` | `git push space master:main --force` |
141
+
142
+ If anything else fails, the local Gradio workflow remains the source
143
+ of truth β€” it is independent of HF.
144
+
145
+ ## Cross-references
146
+
147
+ * [docs/run-locally.md](run-locally.md) β€” PowerShell step-by-step for
148
+ the local Gradio / Streamlit / Dash launchers
149
+ * [docs/frontend-architecture-revised.md](frontend-architecture-revised.md)
150
+ β€” why Gradio is the chosen hosted demo
151
+ * [docs/multi-frontend.md](multi-frontend.md) β€” topology of the four
152
+ coexisting frontends
docs/run-locally.md ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Running the CDPR Simulator locally β€” PowerShell step-by-step
2
+
3
+ The local Gradio UI at `http://127.0.0.1:7860` is the primary
4
+ interface for daily work. Streamlit and Dash are independent
5
+ secondary interfaces. None of them depend on Hugging Face
6
+ deployment β€” the cloud Space is purely a public-demo convenience.
7
+
8
+ > **About `127.0.0.1:7860`** β€” this address is loopback only. It
9
+ > exists *while the Python process is running* in your terminal. Close
10
+ > the terminal and the page goes 404. For a temporary public URL
11
+ > without HF Spaces you can use `demo.launch(share=True)` inside
12
+ > `gradio_app.py` (the line is commented out for safety; uncomment
13
+ > when you want it).
14
+
15
+ ## Prerequisites
16
+
17
+ * Python 3.10 / 3.11 / 3.12
18
+ * Git
19
+ * (Recommended) a virtual environment
20
+
21
+ ## 1) Clone and enter the repo
22
+
23
+ ```powershell
24
+ git clone https://github.com/Tachia/cdpr_simulator.git
25
+ cd cdpr_simulator
26
+ ```
27
+
28
+ ## 2) Create + activate a virtual environment
29
+
30
+ ```powershell
31
+ python -m venv .venv
32
+ .\.venv\Scripts\Activate.ps1
33
+ # If activation is blocked by execution policy:
34
+ # Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
35
+ ```
36
+
37
+ After activation `where.exe python` should point inside `.venv`.
38
+
39
+ ## 3) Install the package and the UI extras you want
40
+
41
+ ```powershell
42
+ # Minimum (CLI + plots):
43
+ python -m pip install --upgrade pip
44
+ python -m pip install -e ".[viz,data,api]"
45
+
46
+ # + Gradio UI (recommended local interface):
47
+ python -m pip install -e ".[viz,data,api,gradio]"
48
+
49
+ # + Streamlit (secondary dashboard):
50
+ python -m pip install -e ".[viz,data,api,gui]"
51
+
52
+ # + Dash (interactive scientific dashboard):
53
+ python -m pip install -e ".[viz,data,api,dash]"
54
+
55
+ # + Phase-2 training (PINN / MLP / PPO / SAC):
56
+ python -m pip install -e ".[viz,data,api,learn,rl]"
57
+
58
+ # Everything at once:
59
+ python -m pip install -e ".[viz,data,api,gradio,gui,dash,learn,rl]"
60
+ ```
61
+
62
+ ## 4) Launch the primary interface (Gradio)
63
+
64
+ ```powershell
65
+ python gradio_app.py
66
+ # Opens at http://127.0.0.1:7860/
67
+ ```
68
+
69
+ That's it. The terminal must stay open β€” closing it kills the server.
70
+
71
+ You should see three tabs: **Built-in examples**, **Custom Phase-1
72
+ simulation**, **Upload CSV / Phase-2**. Output of any run lands in
73
+ `out/` and persists across launches.
74
+
75
+ ## 5) Launch the secondary interfaces
76
+
77
+ Open additional PowerShell windows (the current one keeps Gradio
78
+ running):
79
+
80
+ ```powershell
81
+ # Streamlit dashboard at http://localhost:8501
82
+ streamlit run streamlit_app.py
83
+ ```
84
+
85
+ ```powershell
86
+ # Dash research dashboard at http://127.0.0.1:8050
87
+ python dash_app.py
88
+ ```
89
+
90
+ ```powershell
91
+ # FastAPI backend at http://localhost:8000 (interactive docs at /docs)
92
+ uvicorn cdpr.interface.api:app --reload --port 8000
93
+ ```
94
+
95
+ All four can run side-by-side on the same machine β€” they share the
96
+ same `cdpr` core and the same `out/` directory.
97
+
98
+ ## 6) Run a simulation from the terminal (no UI)
99
+
100
+ ```powershell
101
+ # List the 5 built-in examples:
102
+ python scripts\run_example.py --list
103
+
104
+ # Run any of them end-to-end:
105
+ python scripts\run_example.py --name circle --open
106
+ python scripts\run_example.py --name spiral --open
107
+ python scripts\run_example.py --name mshape --open
108
+ python scripts\run_example.py --name train --open
109
+ python scripts\run_example.py --name compare --open
110
+ ```
111
+
112
+ Each run produces `out/example-<name>/` with 14 figures +
113
+ `timeseries.csv` + `manifest.json` + `feasibility.json`. The `--open`
114
+ flag pops the folder in File Explorer on Windows.
115
+
116
+ ## 7) Custom simulations from the terminal
117
+
118
+ ```powershell
119
+ python scripts\run_simulation.py `
120
+ --robot dissertation_8cable `
121
+ --kind circle --radius 0.05 --duration 12 --dt 1e-3 `
122
+ --controller pd --kp-pos 400 --kp-rot 100 `
123
+ --t-min 5 --t-max 500 `
124
+ --out out\my-run --open
125
+ ```
126
+
127
+ See `python scripts\run_simulation.py --help` for every flag.
128
+
129
+ ## 8) Phase-2 training and comparison
130
+
131
+ ```powershell
132
+ # Train PINN on any timeseries.csv:
133
+ python scripts\train_from_csv.py `
134
+ --input out\example-circle\timeseries.csv `
135
+ --model pinn --epochs 80
136
+
137
+ # 5-model comparison:
138
+ python scripts\compare_models.py `
139
+ --input out\example-circle\timeseries.csv `
140
+ --out out\compare `
141
+ --models replay mlp pinn ppo sac `
142
+ --epochs 60 --rl-steps 2000
143
+
144
+ # Interactive prompts (CSV path β†’ model menu 1-7 β†’ run):
145
+ .\scripts\train_interactive.ps1
146
+ ```
147
+
148
+ ## Where each interface fits
149
+
150
+ | Surface | URL while running | Best for |
151
+ |---|---|---|
152
+ | **Gradio** (`python gradio_app.py`) | `http://127.0.0.1:7860/` | One-click examples, file uploads, casual experimentation |
153
+ | **Dash** (`python dash_app.py`) | `http://127.0.0.1:8050/` | Interactive Plotly plots (zoom + hover), parameter tweaking that doesn't re-run the simulation on every tick |
154
+ | **Streamlit** (`streamlit run streamlit_app.py`) | `http://localhost:8501/` | Heavier dashboards, multi-page Streamlit-style interaction |
155
+ | **FastAPI** (`uvicorn …`) | `http://localhost:8000/docs` | Programmatic access from notebooks / curl / other apps |
156
+ | **CLI** (`python scripts\run_example.py …`) | terminal only | Reproducible batch runs, dissertation regeneration |
157
+
158
+ ## When something doesn't work
159
+
160
+ | Symptom | Fix |
161
+ |---|---|
162
+ | `Activate.ps1 cannot be loaded` | `Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned` |
163
+ | `ModuleNotFoundError: cdpr` | The package isn't installed in the active venv β€” re-run step 3 |
164
+ | Browser shows `Address can't be reached` at `127.0.0.1:7860` | The Python process is not running β€” start `python gradio_app.py` again |
165
+ | `Port 7860 is already in use` | Another Gradio is already running β€” close the other terminal, or change the port with `$env:GRADIO_SERVER_PORT="7861"; python gradio_app.py` |
166
+ | Old simulation results polluting `out/` | `Remove-Item out -Recurse` β€” outputs are reproducible from `python scripts\run_example.py --name <id>` |
167
+ | Streamlit blanks on Cloud | Expected on the free 1 GB worker β€” use the local Gradio / Dash UIs instead. See [docs/frontend-architecture-revised.md](frontend-architecture-revised.md) for the architectural rationale. |
168
+
169
+ ## Quick reference card
170
+
171
+ ```powershell
172
+ # Clean clone to first simulation in under 10 minutes:
173
+ git clone https://github.com/Tachia/cdpr_simulator.git
174
+ cd cdpr_simulator
175
+ python -m venv .venv
176
+ .\.venv\Scripts\Activate.ps1
177
+ python -m pip install -e ".[viz,data,api,gradio]"
178
+ python gradio_app.py
179
+ # Open http://127.0.0.1:7860/
180
+ ```
181
+
182
+ ## Cross-references
183
+
184
+ * [docs/hf-deployment.md](hf-deployment.md) β€” push to a Hugging Face Space without the HF CLI
185
+ * [docs/terminal-execution.md](terminal-execution.md) β€” bash / zsh / CMD variants of the commands above
186
+ * [docs/examples.md](examples.md) β€” what each built-in example does
187
+ * [docs/frontend-architecture-revised.md](frontend-architecture-revised.md) β€” why each interface exists
gradio_app.py CHANGED
@@ -536,12 +536,23 @@ def build_ui() -> gr.Blocks:
536
  return demo
537
 
538
 
 
 
 
 
 
 
 
 
539
  if __name__ == "__main__":
540
- demo = build_ui()
541
- # ``share=False`` keeps the app private on HF Spaces (the Space URL
542
- # is already public). ``server_name=0.0.0.0`` is required for HF.
543
  demo.launch(
544
  server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"),
545
  server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")),
546
  show_error=True,
 
 
 
547
  )
 
536
  return demo
537
 
538
 
539
+ # Module-level ``demo`` β€” required by Hugging Face Spaces, which does
540
+ # ``from gradio_app import demo`` (or ``from app import demo`` via the
541
+ # tiny app.py shim). Building it here at import time keeps the launch
542
+ # path identical between ``python gradio_app.py`` (local) and HF's
543
+ # auto-launch.
544
+ demo = build_ui()
545
+
546
+
547
  if __name__ == "__main__":
548
+ # Local launch. ``server_name=0.0.0.0`` makes the server reachable
549
+ # from the LAN (HF Spaces requires it; on the dev machine 127.0.0.1
550
+ # is the same address served at http://127.0.0.1:7860).
551
  demo.launch(
552
  server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"),
553
  server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")),
554
  show_error=True,
555
+ # share=True would give a temporary tunnel URL --- uncomment
556
+ # if you need a public link without going through HF Spaces.
557
+ # share=True,
558
  )