Running AI tools in Google Colab is powerful, but accessing web interfaces can sometimes be tricky. This guide shows you how to expose the AI-Toolkit UI to the public internet securely using cloudflared.
Quick Command
Copy and paste the following code block into a cell in your Google Colab notebook to start the UI and generate a public access URL.
#@title Run AI Tooklit UI
%cd /content/ai-toolkit/ui
import subprocess
import threading
import time
import socket
import urllib.request
def iframe_thread(port):
while True:
time.sleep(0.5)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', port))
if result == 0:
break
sock.close()
print("\nAI-Toolkit UI finished loading, trying to launch cloudflared (if it gets stuck here cloudflared is having issues)\n")
p = subprocess.Popen(["cloudflared", "tunnel", "--url", "http://127.0.0.1:{}".format(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in p.stderr:
l = line.decode()
if "trycloudflare.com " in l:
print("This is the URL to access AI Tookit UI:", l[l.find("http"):], end='')
#print(l, end='')
threading.Thread(target=iframe_thread, daemon=True, args=(8675,)).start()
# Replace YOUR_SUPER_SECRET_PASSWORD_HERE with your own password
!AI_TOOLKIT_AUTH=YOUR_SUPER_SECRET_PASSWORD_HERE npm run build_and_start
How It Works
- Port Monitoring: The script runs a background thread that continuously checks if port
8675(the default port for AI-Toolkit UI) is open onlocalhost. - Cloudflared Tunnel: Once the port is active, it launches
cloudflaredto create a secure tunnel pointing tohttp://127.0.0.1:8675. - URL Extraction: It parses the output from Cloudflare to find the
trycloudflare.comURL and prints it for you to click. - Secure Access: The application starts with
AI_TOOLKIT_AUTHenabled, ensuring that only users with the password can access your instance.
