| from pathlib import Path |
| import sys |
|
|
| FILE = Path("/usr/local/lib/python3.10/site-packages/gradio/templates/node/build/index.js") |
|
|
| if not FILE.exists(): |
| print("index.js not found") |
| sys.exit(1) |
|
|
| text = FILE.read_text(encoding="utf-8") |
|
|
| MARKER = """ |
| \tif (route === "python") { |
| \t\tproxy.web(req, res, { target: pythonTarget }); |
| \t\treturn; |
| \t} |
| """ |
|
|
| PATCH = """ |
| \tif (route === "python") { |
| \t\tproxy.web(req, res, { target: pythonTarget }); |
| \t\treturn; |
| \t} |
| |
| \t// ===== CUSTOM NODE PROXY ===== |
| |
| \tif (path === "/node" || path.startsWith("/node/")) { |
| \t\treq.url = req.url.replace(/^\\/node/, "") || "/"; |
| |
| \t\tproxy.web(req, res, { |
| \t\t\ttarget: "http://127.0.0.1:8888", |
| \t\t\tchangeOrigin: true |
| \t\t}); |
| |
| \t\treturn; |
| \t} |
| """ |
|
|
| if "// ===== CUSTOM NODE PROXY =====" in text: |
| print("Already patched") |
| sys.exit(0) |
|
|
| if MARKER not in text: |
| print("Marker not found") |
| sys.exit(1) |
|
|
| text = text.replace(MARKER, PATCH) |
|
|
| FILE.write_text(text, encoding="utf-8") |
|
|
| print("Patched successfully.") |