From a8b362caae3bec05b2bcc8dd4f00b2eedf1419d2 Mon Sep 17 00:00:00 2001 From: Madiator2011 Date: Tue, 30 Jul 2024 12:27:59 +0200 Subject: [PATCH] Pushing final version of Better Comfy UI --- official-templates/better-comfyui/Dockerfile | 45 +++--- .../better-comfyui/comfyui_extras.ipynb | 138 ++++++++++++++++++ .../better-comfyui/docker-bake.hcl | 5 +- .../better-comfyui/pre_start.sh | 18 ++- 4 files changed, 180 insertions(+), 26 deletions(-) create mode 100644 official-templates/better-comfyui/comfyui_extras.ipynb diff --git a/official-templates/better-comfyui/Dockerfile b/official-templates/better-comfyui/Dockerfile index b4b8fc0..c830bee 100644 --- a/official-templates/better-comfyui/Dockerfile +++ b/official-templates/better-comfyui/Dockerfile @@ -1,36 +1,37 @@ # Stage 1: Base Image FROM madiator2011/better-base:cuda12.1 as base -ARG PYTHON_VERSION1=3.10 +ARG PYTHON_VERSION1=3.11 ARG TORCH=torch==2.3.0+cu121 # Stage 2: ComfyUI Installation FROM base as comfyui-install -# Create and activate virtual environment for ComfyUI installation -RUN python -m venv /venv -ENV PATH="/venv/bin:$PATH" -ENV VIRTUAL_ENV="/venv" +# Create virtual environment in /workspace/venv +RUN python -m venv /workspace/venv -# Install PyTorch within the virtual environment -RUN if [ -n "${TORCH}" ]; then \ - /venv/bin/pip install --upgrade --no-cache-dir ${TORCH}; \ - fi +# Set environment variables for the virtual environment +ENV VIRTUAL_ENV="/workspace/venv" +ENV PATH="$VIRTUAL_ENV/bin:$PATH" -RUN git clone https://github.com/comfyanonymous/ComfyUI.git && \ - cd /ComfyUI && \ - /venv/bin/pip install -r requirements.txt && \ - git clone https://github.com/ltdrdata/ComfyUI-Manager.git custom_nodes/ComfyUI-Manager && \ - cd custom_nodes/ComfyUI-Manager && \ - /venv/bin/pip install -r requirements.txt && \ - /venv/bin/pip install -U xformers --index-url https://download.pytorch.org/whl/cu121 && \ - /venv/bin/pip install -U accelerate wheel +# Activate the virtual environment and install packages +RUN $VIRTUAL_ENV/bin/pip install --upgrade pip setuptools wheel && \ + if [ -n "${TORCH}" ]; then \ + $VIRTUAL_ENV/bin/pip install --no-cache-dir ${TORCH}; \ + fi && \ + git clone https://github.com/comfyanonymous/ComfyUI.git /ComfyUI && \ + $VIRTUAL_ENV/bin/pip install -r /ComfyUI/requirements.txt && \ + git clone https://github.com/ltdrdata/ComfyUI-Manager.git /ComfyUI/custom_nodes/ComfyUI-Manager && \ + $VIRTUAL_ENV/bin/pip install -r /ComfyUI/custom_nodes/ComfyUI-Manager/requirements.txt && \ + $VIRTUAL_ENV/bin/pip install xformers accelerate wheel comfy-cli insightface && \ + $VIRTUAL_ENV/bin/pip install ipykernel ipywidgets && \ + $VIRTUAL_ENV/bin/python -m ipykernel install --name "python3" --display-name "Python 3 (Workspace Venv)" # Stage 3: Model Setup FROM comfyui-install as model-setup # Create model and cache directories -RUN mkdir -p /root/.cache/huggingface && mkdir -p /comfy-models +RUN mkdir -p /root/.cache/huggingface /comfy-models ARG INCLUDE_MODELS=false @@ -59,12 +60,15 @@ RUN if [ "${INCLUDE_MODELS}" = "true" ]; then \ # Stage 4: Final Image FROM comfyui-install as final +# Move virtual environment from /workspace/venv to /venv +RUN mv /workspace/venv /venv + # Copy models if they were included COPY --from=model-setup /comfy-models /comfy-models # Set environment variables for runtime -ENV PATH="/workspace/venv/bin:$PATH" ENV VIRTUAL_ENV="/workspace/venv" +ENV PATH="$VIRTUAL_ENV/bin:$PATH" # Copy the README.md COPY README.md /usr/share/nginx/html/README.md @@ -78,6 +82,7 @@ COPY pre_start.sh /pre_start.sh COPY --from=scripts start.sh / RUN chmod +x /start.sh RUN chmod +x /pre_start.sh +COPY comfyui_extras.ipynb /comfyui_extras.ipynb # CMD -CMD [ "/start.sh" ] +CMD [ "/start.sh" ] \ No newline at end of file diff --git a/official-templates/better-comfyui/comfyui_extras.ipynb b/official-templates/better-comfyui/comfyui_extras.ipynb new file mode 100644 index 0000000..505482d --- /dev/null +++ b/official-templates/better-comfyui/comfyui_extras.ipynb @@ -0,0 +1,138 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ComfyUI utility notebook ( Run first 2 cells first!!!)\n", + "This notebook helps set up the ComfyUI environment and download models. For some tasks, manual intervention in a terminal is required." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Navigate to the workspace directory where ComfyUI is located.\n", + "%cd /workspace" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Set the default path for ComfyUI.\n", + "!comfy --skip-prompt --no-enable-telemetry set-default /workspace/ComfyUI/" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Fix and Restore Custom Nodes\n", + "This command restores dependencies for ComfyUI. Use this if you have updated templates or if the virtual environment is broken. After running this, restart ComfyUI from the manager or restart the pod. Sometimes, custom nodes won't show up until you refresh the page in your browser." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Restore dependencies for ComfyUI.\n", + "!comfy node restore-dependencies" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Model Downloader\n", + "Enter the model URL below. If the model is from CivitAI, check the box to provide your API token. Hugging Face models are also supported." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "8e25344b209c4f7d91503c08c6007dfe", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(Text(value='', description='Model URL:', layout=Layout(width='80%')), Checkbox(value=False, des…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from ipywidgets import Text, Button, Checkbox, VBox, Output, Layout, Textarea\n", + "from IPython.display import display\n", + "\n", + "# Widgets for user input\n", + "url_input = Text(description='Model URL:', layout=Layout(width='80%'))\n", + "civitai_checkbox = Checkbox(description='CivitAI Model', layout=Layout(width='80%'))\n", + "token_input = Text(description='API Token:', placeholder='Enter CivitAI API token (if needed)', layout=Layout(width='80%'))\n", + "token_input.layout.visibility = 'hidden' # Initially hidden\n", + "output_area = Textarea(value='', placeholder='Generated command will appear here...', layout=Layout(width='80%', height='100px'))\n", + "\n", + "# Function to toggle API token input visibility\n", + "def toggle_token_input(change):\n", + " token_input.layout.visibility = 'visible' if change['new'] else 'hidden'\n", + "civitai_checkbox.observe(toggle_token_input, names='value')\n", + "\n", + "# Function to display the manual command\n", + "def display_command(b):\n", + " url = url_input.value\n", + " token = token_input.value if civitai_checkbox.value else ''\n", + " if not url:\n", + " output_area.value = \"Please enter a valid URL.\"\n", + " return\n", + " command = f\"comfy model download --url {url}\"\n", + " if token:\n", + " command += f\" --set-civitai-api-token {token}\"\n", + " output_area.value = command\n", + "\n", + "# Button to generate command\n", + "generate_button = Button(description='Generate Command')\n", + "generate_button.on_click(display_command)\n", + "\n", + "# Display the input widgets and output area\n", + "display(VBox([url_input, civitai_checkbox, token_input, generate_button, output_area]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Instructions for Executing the Command\n", + "1. **Copy the generated command** from the output area above.\n", + "2. **Open a terminal** in your environment. In VS Code, you can do this by pressing `Ctrl + ~` or right-clicking an empty space in the Explorer and selecting 'Open in Integrated Terminal'.\n", + "3. **Paste and run the command** in the terminal.\n", + "4. **Follow any prompts** in the terminal for additional input, such as entering a filename." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/official-templates/better-comfyui/docker-bake.hcl b/official-templates/better-comfyui/docker-bake.hcl index 2b0c9dc..5d4e892 100644 --- a/official-templates/better-comfyui/docker-bake.hcl +++ b/official-templates/better-comfyui/docker-bake.hcl @@ -1,5 +1,5 @@ group "default" { - targets = ["full-version", "light-version", "light-experimental"] + targets = ["full-version", "light-version"] } target "base" { @@ -7,7 +7,7 @@ target "base" { args = { BASE_IMAGE = "madiator2011/better-base:cuda12.1", TORCH = "torch==2.3.0+cu121 -f https://download.pytorch.org/whl/torch_stable.html", - PYTHON_VERSION1 = "3.10" + PYTHON_VERSION1 = "3.11" } contexts = { scripts = "../../container-template" @@ -41,3 +41,4 @@ target "light-version" { } tags = ["madiator2011/better-comfyui:light"] } + diff --git a/official-templates/better-comfyui/pre_start.sh b/official-templates/better-comfyui/pre_start.sh index e4cd1aa..20662a6 100644 --- a/official-templates/better-comfyui/pre_start.sh +++ b/official-templates/better-comfyui/pre_start.sh @@ -12,7 +12,7 @@ print_feedback() { # Function to run rsync with progress bar and optimizations rsync_with_progress() { - rsync -aHvx --info=progress2 "$@" + rsync -aHvx --info=progress2 --ignore-existing --update --stats "$@" } # Check for required commands @@ -25,6 +25,16 @@ done LOG_FILE="/workspace/comfyui.log" +# Copy the notebook to the /workspace directory +print_feedback "Copying notebook to /workspace..." +cp /comfyui_extras.ipynb /workspace/ + +# Check if the NO_SYNC variable is set to true +if [ "${NO_SYNC}" == "true" ]; then + print_feedback "Skipping sync and startup as per environment variable setting." + exec bash -c 'sleep infinity' +fi + print_feedback "Starting ComfyUI setup..." print_feedback "Syncing virtual environment..." @@ -33,7 +43,7 @@ rsync_with_progress /venv/ /workspace/venv/ print_feedback "Activating virtual environment..." export VIRTUAL_ENV="/workspace/venv" export PATH="$VIRTUAL_ENV/bin:$PATH" -source /workspace/venv/bin/activate +source "$VIRTUAL_ENV/bin/activate" export PYTHONUNBUFFERED=1 @@ -51,7 +61,7 @@ print_feedback "ComfyUI will be available at http://0.0.0.0:3000" # Check if CUSTOM_ARGS is set and not empty if [ -n "$CUSTOM_ARGS" ]; then - exec /workspace/venv/bin/python main.py --listen --port 3000 $CUSTOM_ARGS 2>&1 | tee -a $LOG_FILE + exec python main.py --listen --port 3000 $CUSTOM_ARGS 2>&1 | tee -a $LOG_FILE else - exec /workspace/venv/bin/python main.py --listen --port 3000 2>&1 | tee -a $LOG_FILE + exec python main.py --listen --port 3000 2>&1 | tee -a $LOG_FILE fi \ No newline at end of file