Create patch_gradio.py
Browse files- patch_gradio.py +51 -0
patch_gradio.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
FILE = Path("/usr/local/lib/python3.10/site-packages/gradio/templates/node/build/index.js")
|
| 5 |
+
|
| 6 |
+
if not FILE.exists():
|
| 7 |
+
print("index.js not found")
|
| 8 |
+
sys.exit(1)
|
| 9 |
+
|
| 10 |
+
text = FILE.read_text(encoding="utf-8")
|
| 11 |
+
|
| 12 |
+
MARKER = """
|
| 13 |
+
\tif (route === "python") {
|
| 14 |
+
\t\tproxy.web(req, res, { target: pythonTarget });
|
| 15 |
+
\t\treturn;
|
| 16 |
+
\t}
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
PATCH = """
|
| 20 |
+
\tif (route === "python") {
|
| 21 |
+
\t\tproxy.web(req, res, { target: pythonTarget });
|
| 22 |
+
\t\treturn;
|
| 23 |
+
\t}
|
| 24 |
+
|
| 25 |
+
\t// ===== CUSTOM NODE PROXY =====
|
| 26 |
+
|
| 27 |
+
\tif (path === "/node" || path.startsWith("/node/")) {
|
| 28 |
+
\t\treq.url = req.url.replace(/^\\/node/, "") || "/";
|
| 29 |
+
|
| 30 |
+
\t\tproxy.web(req, res, {
|
| 31 |
+
\t\t\ttarget: "http://127.0.0.1:8888",
|
| 32 |
+
\t\t\tchangeOrigin: true
|
| 33 |
+
\t\t});
|
| 34 |
+
|
| 35 |
+
\t\treturn;
|
| 36 |
+
\t}
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
if "// ===== CUSTOM NODE PROXY =====" in text:
|
| 40 |
+
print("Already patched")
|
| 41 |
+
sys.exit(0)
|
| 42 |
+
|
| 43 |
+
if MARKER not in text:
|
| 44 |
+
print("Marker not found")
|
| 45 |
+
sys.exit(1)
|
| 46 |
+
|
| 47 |
+
text = text.replace(MARKER, PATCH)
|
| 48 |
+
|
| 49 |
+
FILE.write_text(text, encoding="utf-8")
|
| 50 |
+
|
| 51 |
+
print("Patched successfully.")
|