__init__.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import base64
  2. import os
  3. import random
  4. from pathlib import Path
  5. import typer
  6. import uvicorn
  7. app = typer.Typer()
  8. KEY_FILE = Path.cwd() / ".webui_secret_key"
  9. if (frontend_build_dir := Path(__file__).parent / "frontend").exists():
  10. os.environ["FRONTEND_BUILD_DIR"] = str(frontend_build_dir)
  11. @app.command()
  12. def serve(
  13. host: str = "0.0.0.0",
  14. port: int = 8080,
  15. ):
  16. if os.getenv("WEBUI_SECRET_KEY") is None:
  17. typer.echo(
  18. "Loading WEBUI_SECRET_KEY from file, not provided as an environment variable."
  19. )
  20. if not KEY_FILE.exists():
  21. typer.echo(f"Generating a new secret key and saving it to {KEY_FILE}")
  22. KEY_FILE.write_bytes(base64.b64encode(random.randbytes(12)))
  23. typer.echo(f"Loading WEBUI_SECRET_KEY from {KEY_FILE}")
  24. os.environ["WEBUI_SECRET_KEY"] = KEY_FILE.read_text()
  25. if os.getenv("USE_CUDA_DOCKER", "false") == "true":
  26. typer.echo(
  27. "CUDA is enabled, appending LD_LIBRARY_PATH to include torch/cudnn & cublas libraries."
  28. )
  29. LD_LIBRARY_PATH = os.getenv("LD_LIBRARY_PATH", "").split(":")
  30. os.environ["LD_LIBRARY_PATH"] = ":".join(
  31. LD_LIBRARY_PATH
  32. + [
  33. "/usr/local/lib/python3.11/site-packages/torch/lib",
  34. "/usr/local/lib/python3.11/site-packages/nvidia/cudnn/lib",
  35. ]
  36. )
  37. import open_webui.main # we need set environment variables before importing main
  38. uvicorn.run(open_webui.main.app, host=host, port=port, forwarded_allow_ips="*")
  39. @app.command()
  40. def dev(
  41. host: str = "0.0.0.0",
  42. port: int = 8080,
  43. reload: bool = True,
  44. ):
  45. uvicorn.run(
  46. "open_webui.main:app",
  47. host=host,
  48. port=port,
  49. reload=reload,
  50. forwarded_allow_ips="*",
  51. )
  52. if __name__ == "__main__":
  53. app()