import subprocess import time import urllib.request import sys print("1. Starting Ollama server in the background...") server = subprocess.Popen(["ollama", "serve"]) print("2. Waiting for server to wake up...") ready = False for i in range(30): # Give it up to 30 seconds try: if urllib.request.urlopen("http://127.0.0.1:7860").getcode() == 200: ready = True break except Exception: time.sleep(1) if not ready: print("Error: Ollama server failed to start.") server.kill() sys.exit(1) print("3. Server is up! Pulling base model and creating quant-agent...") try: # This automatically downloads gemma3:4b and applies your Modelfile subprocess.run(["ollama", "create", "quant-agent", "-f", "/app/Tools.modelfile"], check=True) print("Model successfully baked!") except subprocess.CalledProcessError: print("Error during model creation.") server.kill() sys.exit(1) print("4. Shutting down background server to finalize Docker layer...") server.terminate() server.wait() print("Build script complete. Exiting cleanly.")