如何在Google Colab中挂载多Google Drive账户以支持qDiffusion?
问题描述
我尝试在Google Colab中连接并挂载多个Google Drive账户,使qDiffusion Application能够从两个Google Drive账户加载文件并在client app中显示。
原代码(Notebook)
%cd /content import IPython.display import os import sys if not os.path.exists("/content/sd-inference-server"): !git clone https://github.com/arenasys/sd-inference-server.git %cd /content/sd-inference-server model_folder = "/content/sd-inference-server/models" try: # decline the popup to use the local folder ^ from google.colab import drive drive.mount('/content/drive', force_remount=True) model_folder = "/content/drive/My Drive/qDiffusion/models" if not os.path.exists(model_folder): !mkdir '/content/drive/My Drive/qDiffusion' -p !cp -r 'models/' '/content/drive/My Drive/qDiffusion/' except Exception as e: pass if not os.path.exists("venv"): !curl -OJL https://huggingface.co/datasets/arenasys/qDiffusion/resolve/main/cached_venv5.tar !tar xf cached_venv5.tar; mv cached_venv5 venv; rm cached_venv5.tar !rm /content/sd-inference-server/venv/lib/python3.10/site-packages/pathlib.py !pip install timm==0.6.13 !pip install tomesd==0.1.3 !pip install torchmetrics !pip uninstall -y tensorflow !wget http://launchpadlibrarian.net/367274644/libgoogle-perftools-dev_2.5-2.2ubuntu3_amd64.deb !wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/google-perftools_2.5-2.2ubuntu3_all.deb !wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/libtcmalloc-minimal4_2.5-2.2ubuntu3_amd64.deb !wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/libgoogle-perftools4_2.5-2.2ubuntu3_amd64.deb !apt install -qq libunwind8-dev !dpkg -i *.deb %env LD_PRELOAD=libtcmalloc.so IPython.display.clear_output() if not sys.path[0] == "/content/sd-inference-server/": sys.path.insert(0, "/content/sd-inference-server/venv/lib/python3.10/site-packages") sys.path.insert(0, "/content/sd-inference-server/") from pycloudflared import try_cloudflare tunnel_url = try_cloudflare(port=28888, verbose=False) print("ENDPOINT:", tunnel_url.tunnel.replace("https", "wss")) !python remote.py "$model_folder" try_cloudflare.terminate(28888)
多账户挂载参考代码
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null !apt-get update -qq 2>&1 > /dev/null !apt-get -y install -qq google-drive-ocamlfuse fuse from google.colab import auth auth.authenticate_user() from oauth2client.client import GoogleCredentials creds = GoogleCredentials.get_application_default() import getpass !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL vcode = getpass.getpass() !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} !mkdir -p /drive2 !google-drive-ocamlfuse /drive2
合并后的代码
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null !apt-get update -qq 2>&1 > /dev/null !apt-get -y install -qq google-drive-ocamlfuse fuse from google.colab import auth auth.authenticate_user() from oauth2client.client import GoogleCredentials # Define a function to connect and mount Google Drive def mount_drive(): creds = GoogleCredentials.get_application_default() import getpass vcode = getpass.getpass() !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} -code={vcode} # Define a function to mount a specific Google Drive account def mount_google_drive(account_name, drive_path): # Create a folder to mount the drive mount_folder = f"/drive_{account_name}" !mkdir -p {mount_folder} # Mount the drive !google-drive-ocamlfuse {mount_folder} # Move the contents to the desired drive path !mv {mount_folder}/* {drive_path} # Remove the empty folder !rm -rf {mount_folder} # Example usage: mount_drive() # Mount and specify the accounts and paths accounts = [ {"name": "account1", "drive_path": "/content/drive/My Drive/qDiffusion/models"}, {"name": "account2", "drive_path": "/content/drive/My Drive/other_drive"} ] for account in accounts: print(f"Mounting {account['name']}...") mount_google_drive(account["name"], account["drive_path"]) print(f"{account['name']} mounted at {account['drive_path']}") %cd /content import IPython.display import os import sys if not os.path.exists("/content/sd-inference-server"): !git clone https://github.com/arenasys/sd-inference-server.git %cd /content/sd-inference-server model_folder = "/content/sd-inference-server/models" try: # decline the popup to use the local folder ^ from google.colab import drive drive.mount('/content/drive', force_remount=True) model_folder = "/content/drive/My Drive/qDiffusion/models" if not os.path.exists(model_folder): !mkdir '/content/drive/My Drive/qDiffusion' -p !cp -r 'models/' '/content/drive/My Drive/qDiffusion/' except Exception as e: pass if not os.path.exists("venv"): !curl -OJL https://huggingface.co/datasets/arenasys/qDiffusion/resolve/main/cached_venv5.tar !tar xf cached_venv5.tar; mv cached_venv5 venv; rm cached_venv5.tar !rm /content/sd-inference-server/venv/lib/python3.10/site-packages/pathlib.py !pip install timm==0.6.13 !pip install tomesd==0.1.3 !pip install torchmetrics !pip uninstall -y tensorflow !wget http://launchpadlibrarian.net/367274644/libgoogle-perftools-dev_2.5-2.2ubuntu3_amd64.deb !wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/google-perftools_2.5-2.2ubuntu3_all.deb !wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/libtcmalloc-minimal4_2.5-2.2ubuntu3_amd64.deb !wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/libgoogle-perftools4_2.5-2.2ubuntu3_amd64.deb !apt install -qq libunwind8-dev !dpkg -i *.deb %env LD_PRELOAD=libtcmalloc.so IPython.display.clear_output() if not sys.path[0] == "/content/sd-inference-server": sys.path.insert(0, "/content/sd-inference-server/venv/lib/python3.10/site-packages") sys.path.insert(0, "/content/sd-inference-server/") from pycloudflared import try_cloudflare tunnel_url = try_cloudflare(port=28888, verbose=False) print("ENDPOINT:", tunnel_url.tunnel.replace("https", "wss")) !python remote.py "$model_folder" try_cloudflare.terminate(28888)
问题
合并后的代码未按预期工作:没有弹出连接不同Google Drive账户的窗口,也没有生成TryCloudflare公网Endpoint URL。
内容的提问来源于stack exchange,提问作者Mehul Kumar
相关产品推荐
相关产品推荐

