mirror of
https://github.com/harry0703/MoneyPrinterTurbo.git
synced 2026-11-14 18:22:59 +01:00
Adds missing imports (Path, subprocess & ngrok) that previously caused NameError failures. Corrects the REPO_DIR path issue that caused FileNotFoundError by ensuring /content/MoneyPrinterTurbo is properly defined and validated before launching Streamlit.
8.1 KiB
8.1 KiB
In [ ]:
import os
import subprocess
from pathlib import Path
REPO_DIR = Path("/content/MoneyPrinterTurbo")
REPO_URL = "https://github.com/harry0703/MoneyPrinterTurbo.git"
# Update an existing checkout so rerunning this cell does not fail during clone.
if (REPO_DIR / ".git").is_dir():
subprocess.run(
["git", "-C", str(REPO_DIR), "pull", "--ff-only"], check=True
)
elif REPO_DIR.exists():
raise RuntimeError(f"{REPO_DIR} exists but is not a Git repository")
else:
subprocess.run(
["git", "clone", "--depth", "1", REPO_URL, str(REPO_DIR)], check=True
)
os.chdir(REPO_DIR)
subprocess.run(["python", "-m", "pip", "install", "-q", "uv", "pyngrok"], check=True)
subprocess.run(["uv", "python", "install", "3.11"], check=True)
# Use the lockfile in an isolated environment without changing Colab's preinstalled packages.
subprocess.run(["uv", "sync", "--frozen", "--python", "3.11"], check=True)
print(f"MoneyPrinterTurbo is ready in {REPO_DIR}")In [ ]:
from getpass import getpass
from pyngrok import ngrok
# Close tunnels from earlier runs before configuring a new one.
ngrok.kill()
ngrok_token = getpass("Enter your ngrok authentication token: ").strip()
if not ngrok_token:
raise ValueError("An ngrok authentication token is required")
ngrok.set_auth_token(ngrok_token)
del ngrok_token
print("ngrok authentication configured")In [ ]:
import time
from pathlib import Path
import subprocess
from urllib.error import URLError
from urllib.request import urlopen
from pyngrok import ngrok
# Sanity checks before starting
REPO_DIR = Path("/content/MoneyPrinterTurbo")
if not REPO_DIR.is_dir():
raise RuntimeError(f"Repo directory does not exist: {REPO_DIR}")
if not (REPO_DIR / "webui" / "Main.py").is_file():
raise RuntimeError(f"webui/Main.py not found in {REPO_DIR}")
PORT = 8501
LOG_PATH = Path("/content/moneyprinterturbo-webui.log")
# Make this cell safe to rerun by closing the previous process, log handle, and tunnel.
previous_streamlit_proc = globals().get("streamlit_proc")
if previous_streamlit_proc is not None and previous_streamlit_proc.poll() is None:
previous_streamlit_proc.terminate()
previous_streamlit_proc.wait(timeout=10)
previous_streamlit_log = globals().get("streamlit_log")
if previous_streamlit_log is not None and not previous_streamlit_log.closed:
previous_streamlit_log.close()
try:
ngrok.kill()
except Exception:
pass
streamlit_log = LOG_PATH.open("w", encoding="utf-8")
streamlit_proc = subprocess.Popen(
[
"uv",
"run",
"streamlit",
"run",
"webui/Main.py",
f"--server.port={PORT}",
"--server.address=0.0.0.0",
"--browser.gatherUsageStats=False",
"--client.toolbarMode=minimal",
"--server.showEmailPrompt=False",
],
cwd=REPO_DIR,
stdout=streamlit_log,
stderr=subprocess.STDOUT,
text=True,
)
# Poll the health endpoint because startup time varies across Colab runtimes.
deadline = time.time() + 90
server_ready = False
while time.time() < deadline:
if streamlit_proc.poll() is not None:
break
try:
with urlopen(f"http://127.0.0.1:{PORT}/_stcore/health", timeout=2) as response:
server_ready = response.status == 200
except (URLError, TimeoutError):
pass
if server_ready:
break
time.sleep(2)
if not server_ready:
streamlit_log.flush()
recent_log = LOG_PATH.read_text(encoding="utf-8", errors="replace")[-4000:]
raise RuntimeError(f"Streamlit failed to start. Recent log:\n{recent_log}")
# Use an explicit IPv4 URL because pyngrok may otherwise route localhost through ::1.
public_tunnel = ngrok.connect(addr=f"http://127.0.0.1:{PORT}", proto="http", bind_tls=True)
print("MoneyPrinterTurbo is ready:")
print(public_tunnel.public_url)
print(f"Server log: {LOG_PATH}")