__init__.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. try:
  37. import torch
  38. assert torch.cuda.is_available(), "CUDA not available"
  39. typer.echo("CUDA seems to be working")
  40. except Exception as e:
  41. typer.echo(
  42. "Error when testing CUDA but USE_CUDA_DOCKER is true. "
  43. "Resetting USE_CUDA_DOCKER to false and removing "
  44. f"LD_LIBRARY_PATH modifications: {e}"
  45. )
  46. os.environ["USE_CUDA_DOCKER"] = "false"
  47. os.environ["LD_LIBRARY_PATH"] = ":".join(LD_LIBRARY_PATH)
  48. import open_webui.main # we need set environment variables before importing main
  49. uvicorn.run(open_webui.main.app, host=host, port=port, forwarded_allow_ips="*")
  50. @app.command()
  51. def dev(
  52. host: str = "0.0.0.0",
  53. port: int = 8080,
  54. reload: bool = True,
  55. ):
  56. uvicorn.run(
  57. "open_webui.main:app",
  58. host=host,
  59. port=port,
  60. reload=reload,
  61. forwarded_allow_ips="*",
  62. )
  63. if __name__ == "__main__":
  64. app()