serve_old_web.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Serve the legacy decompose demo from repo-local files.
  2. The legacy `web/index.html` expects assets at root paths such as
  3. `/frameworks.json`, `/payloads.json`, `/prompt-read/...`, and `/data/...`.
  4. This tiny server preserves those paths without changing the old frontend.
  5. """
  6. from __future__ import annotations
  7. from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
  8. from pathlib import Path
  9. from urllib.parse import unquote, urlparse
  10. ROOT = Path(__file__).resolve().parent.parent
  11. WEB = ROOT / "web"
  12. PROMPTS = ROOT / "prompts"
  13. SKILL_EXTRACTION = ROOT / "创作知识提取-skill" / "extraction"
  14. class LegacyDemoHandler(SimpleHTTPRequestHandler):
  15. def translate_path(self, path: str) -> str:
  16. parsed = urlparse(path)
  17. clean = unquote(parsed.path)
  18. if clean in {"", "/"}:
  19. return str(WEB / "index.html")
  20. if clean.startswith("/data/"):
  21. return str(ROOT / clean.lstrip("/"))
  22. if clean.startswith("/runs/"):
  23. return str(WEB / clean.lstrip("/"))
  24. if clean.startswith("/prompt-read/"):
  25. return str(PROMPTS / clean.removeprefix("/prompt-read/"))
  26. if clean.startswith("/prompt-skill/"):
  27. return str(SKILL_EXTRACTION / clean.removeprefix("/prompt-skill/"))
  28. return str(WEB / clean.lstrip("/"))
  29. def main() -> None:
  30. server = ThreadingHTTPServer(("127.0.0.1", 8127), LegacyDemoHandler)
  31. print("Serving legacy demo at http://127.0.0.1:8127/")
  32. server.serve_forever()
  33. if __name__ == "__main__":
  34. main()