__init__.py 1.7 KB

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