| 
									
										
										
										
											2024-05-14 11:53:46 +08:00
										 |  |  | import base64 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import random | 
					
						
							|  |  |  | from pathlib import Path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import typer | 
					
						
							|  |  |  | import uvicorn | 
					
						
							| 
									
										
										
										
											2025-01-13 16:34:15 +08:00
										 |  |  | from typing import Optional | 
					
						
							|  |  |  | from typing_extensions import Annotated | 
					
						
							| 
									
										
										
										
											2024-05-14 11:53:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | app = typer.Typer() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | KEY_FILE = Path.cwd() / ".webui_secret_key" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 16:34:15 +08:00
										 |  |  | def version_callback(value: bool): | 
					
						
							|  |  |  |     if value: | 
					
						
							|  |  |  |         from open_webui.env import VERSION | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         typer.echo(f"Open WebUI version: {VERSION}") | 
					
						
							|  |  |  |         raise typer.Exit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @app.command() | 
					
						
							|  |  |  | def main( | 
					
						
							|  |  |  |     version: Annotated[ | 
					
						
							|  |  |  |         Optional[bool], typer.Option("--version", callback=version_callback) | 
					
						
							|  |  |  |     ] = None, | 
					
						
							|  |  |  | ): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 11:53:46 +08:00
										 |  |  | @app.command() | 
					
						
							|  |  |  | def serve( | 
					
						
							|  |  |  |     host: str = "0.0.0.0", | 
					
						
							|  |  |  |     port: int = 8080, | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-09-07 12:18:52 +08:00
										 |  |  |     os.environ["FROM_INIT_PY"] = "true" | 
					
						
							| 
									
										
										
										
											2024-05-14 11:53:46 +08:00
										 |  |  |     if os.getenv("WEBUI_SECRET_KEY") is None: | 
					
						
							|  |  |  |         typer.echo( | 
					
						
							|  |  |  |             "Loading WEBUI_SECRET_KEY from file, not provided as an environment variable." | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         if not KEY_FILE.exists(): | 
					
						
							|  |  |  |             typer.echo(f"Generating a new secret key and saving it to {KEY_FILE}") | 
					
						
							|  |  |  |             KEY_FILE.write_bytes(base64.b64encode(random.randbytes(12))) | 
					
						
							|  |  |  |         typer.echo(f"Loading WEBUI_SECRET_KEY from {KEY_FILE}") | 
					
						
							|  |  |  |         os.environ["WEBUI_SECRET_KEY"] = KEY_FILE.read_text() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if os.getenv("USE_CUDA_DOCKER", "false") == "true": | 
					
						
							|  |  |  |         typer.echo( | 
					
						
							|  |  |  |             "CUDA is enabled, appending LD_LIBRARY_PATH to include torch/cudnn & cublas libraries." | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         LD_LIBRARY_PATH = os.getenv("LD_LIBRARY_PATH", "").split(":") | 
					
						
							|  |  |  |         os.environ["LD_LIBRARY_PATH"] = ":".join( | 
					
						
							|  |  |  |             LD_LIBRARY_PATH | 
					
						
							|  |  |  |             + [ | 
					
						
							|  |  |  |                 "/usr/local/lib/python3.11/site-packages/torch/lib", | 
					
						
							|  |  |  |                 "/usr/local/lib/python3.11/site-packages/nvidia/cudnn/lib", | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-09-13 23:12:00 +08:00
										 |  |  |         try: | 
					
						
							|  |  |  |             import torch | 
					
						
							| 
									
										
										
										
											2024-09-13 23:18:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-13 23:12:00 +08:00
										 |  |  |             assert torch.cuda.is_available(), "CUDA not available" | 
					
						
							|  |  |  |             typer.echo("CUDA seems to be working") | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             typer.echo( | 
					
						
							|  |  |  |                 "Error when testing CUDA but USE_CUDA_DOCKER is true. " | 
					
						
							|  |  |  |                 "Resetting USE_CUDA_DOCKER to false and removing " | 
					
						
							|  |  |  |                 f"LD_LIBRARY_PATH modifications: {e}" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             os.environ["USE_CUDA_DOCKER"] = "false" | 
					
						
							|  |  |  |             os.environ["LD_LIBRARY_PATH"] = ":".join(LD_LIBRARY_PATH) | 
					
						
							| 
									
										
										
										
											2024-09-24 22:19:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-04 23:02:10 +08:00
										 |  |  |     import open_webui.main  # we need set environment variables before importing main | 
					
						
							| 
									
										
										
										
											2025-04-09 00:55:07 +08:00
										 |  |  |     from open_webui.env import UVICORN_WORKERS  # Import the workers setting | 
					
						
							| 
									
										
										
										
											2024-05-20 11:12:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 00:55:07 +08:00
										 |  |  |     uvicorn.run( | 
					
						
							| 
									
										
										
										
											2025-04-15 15:55:35 +08:00
										 |  |  |         "open_webui.main:app", | 
					
						
							| 
									
										
										
										
											2025-04-13 07:35:11 +08:00
										 |  |  |         host=host, | 
					
						
							|  |  |  |         port=port, | 
					
						
							| 
									
										
										
										
											2025-04-09 00:55:07 +08:00
										 |  |  |         forwarded_allow_ips="*", | 
					
						
							| 
									
										
										
										
											2025-04-13 07:35:11 +08:00
										 |  |  |         workers=UVICORN_WORKERS, | 
					
						
							| 
									
										
										
										
											2025-04-09 00:55:07 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-05-14 11:53:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-20 11:12:03 +08:00
										 |  |  | @app.command() | 
					
						
							|  |  |  | def dev( | 
					
						
							|  |  |  |     host: str = "0.0.0.0", | 
					
						
							|  |  |  |     port: int = 8080, | 
					
						
							|  |  |  |     reload: bool = True, | 
					
						
							|  |  |  | ): | 
					
						
							|  |  |  |     uvicorn.run( | 
					
						
							| 
									
										
										
										
											2024-09-04 23:02:10 +08:00
										 |  |  |         "open_webui.main:app", | 
					
						
							|  |  |  |         host=host, | 
					
						
							|  |  |  |         port=port, | 
					
						
							|  |  |  |         reload=reload, | 
					
						
							|  |  |  |         forwarded_allow_ips="*", | 
					
						
							| 
									
										
										
										
											2024-05-20 11:12:03 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 11:53:46 +08:00
										 |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     app() |