Skip to content

Instantly share code, notes, and snippets.

@luckylionheart
Last active March 9, 2026 14:08
Show Gist options
  • Select an option

  • Save luckylionheart/5fc0f6baee9a445a71f364d0266db800 to your computer and use it in GitHub Desktop.

Select an option

Save luckylionheart/5fc0f6baee9a445a71f364d0266db800 to your computer and use it in GitHub Desktop.
Make the VastAI instance using api key
import requests
import json
import sys
import time
API_KEY = "***"
MIN_VRAM_MB = 80 * 1024 # 81 920 MB → covers full 80 GB cards
DISK_GB = 80 # local disk for model weights + outputs
NUM_GPUS = 1
MAX_PRICE_PER_HR = 1.50 # USD – raise if no results found
DOCKER_IMAGE = "pytorch/pytorch:2.2.2-cuda12.1-cudnn8-devel"
ONSTART_SCRIPT = """\
pip install -q deepseek-vl2 transformers accelerate &&
echo 'Environment ready for DeepSeek VL2'
"""
BASE_URL = "https://console.vast.ai/api/v0"
def headers():
return {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
def search_offers() -> list[dict]:
"""
Search for available on-demand GPU instances with >= 80 GB VRAM.
Returns a list of offers sorted by price (cheapest first).
"""
params = {
"q": json.dumps({
"gpu_ram": {"gte": MIN_VRAM_MB},
"num_gpus": {"eq": NUM_GPUS},
"rentable": {"eq": True},
"rented": {"eq": False},
"dph_total": {"lte": MAX_PRICE_PER_HR},
"type": "on-demand",
"order": [["dph_total", "asc"]], # cheapest first
"limit": 20,
})
}
resp = requests.get(f"{BASE_URL}/bundles/", headers=headers(), params=params, timeout=30)
resp.raise_for_status()
offers = resp.json().get("offers", [])
return offers
def display_offers(offers: list[dict]) -> None:
print(f"\n{'─'*72}")
print(f" {'#':<4} {'ID':<12} {'GPU':<22} {'VRAM (GB)':<12} {'$/hr':<8} {'DLPerf':<8}")
print(f"{'─'*72}")
for i, o in enumerate(offers, 1):
vram_gb = o.get("gpu_ram", 0) / 1024
gpu_name = o.get("gpu_name", "unknown")
price = o.get("dph_total", 0)
dlperf = o.get("dlperf", 0)
oid = o.get("id", "?")
print(f" {i:<4} {oid:<12} {gpu_name:<22} {vram_gb:<12.0f} {price:<8.3f} {dlperf:<8.1f}")
print(f"{'─'*72}\n")
def create_instance(offer_id: int) -> dict:
"""
Create (rent) an instance from the given offer ID.
Returns the API response dict.
"""
payload = {
"client_id": "me",
"image": DOCKER_IMAGE,
"disk": DISK_GB,
"onstart": ONSTART_SCRIPT,
"runtype": "ssh", # SSH access
"image_login": None,
"python_utf8": False,
"lang_utf8": False,
"use_jupyter_lab": False,
"jupyter_dir": "/",
"create_from": "",
"force": False,
}
resp = requests.put(
f"{BASE_URL}/asks/{offer_id}/",
headers=headers(),
json=payload,
timeout=30,
)
resp.raise_for_status()
return resp.json()
def get_instance_info(instance_id: int) -> dict:
"""Fetch current status of a running instance."""
resp = requests.get(
f"{BASE_URL}/instances/{instance_id}/",
headers=headers(),
timeout=30,
)
resp.raise_for_status()
return resp.json()
def wait_for_running(instance_id: int, timeout_sec: int = 300) -> dict:
"""Poll until instance status == 'running' or timeout."""
print("⏳ Waiting for instance to start ", end="", flush=True)
deadline = time.time() + timeout_sec
while time.time() < deadline:
info = get_instance_info(instance_id)
status = info.get("actual_status", "")
if status == "running":
print(" ✓")
return info
print(".", end="", flush=True)
time.sleep(10)
print()
raise TimeoutError(f"Instance {instance_id} did not reach 'running' within {timeout_sec}s")
def main():
if API_KEY == "YOUR_VAST_API_KEY_HERE":
sys.exit("❌ Please set your API_KEY at the top of the script.")
# 1. Search
print(f"🔍 Searching for offers with ≥{MIN_VRAM_MB // 1024} GB VRAM "
f"at ≤ ${MAX_PRICE_PER_HR}/hr …")
offers = search_offers()
if not offers:
sys.exit("❌ No matching offers found. Try raising MAX_PRICE_PER_HR or relaxing filters.")
display_offers(offers)
# 2. Let the user pick (or auto-select the cheapest)
if len(offers) == 1:
chosen = offers[0]
print(f"✅ Auto-selecting the only match: ID {chosen['id']}")
else:
raw = input(f"Enter offer number to rent [1-{len(offers)}] (Enter = cheapest): ").strip()
idx = (int(raw) - 1) if raw.isdigit() else 0
chosen = offers[max(0, min(idx, len(offers) - 1))]
offer_id = chosen["id"]
gpu_name = chosen.get("gpu_name", "?")
vram_gb = chosen.get("gpu_ram", 0) / 1024
price = chosen.get("dph_total", 0)
print(f"\n🖥️ Renting {gpu_name} ({vram_gb:.0f} GB VRAM) @ ${price:.3f}/hr [offer {offer_id}]")
confirm = input("Confirm? [y/N]: ").strip().lower()
if confirm != "y":
sys.exit("Aborted.")
# 3. Create instance
print("🚀 Creating instance …")
result = create_instance(offer_id)
if not result.get("success"):
sys.exit(f"❌ Failed to create instance:\n{json.dumps(result, indent=2)}")
instance_id = result.get("new_contract")
print(f"✅ Instance created → ID: {instance_id}")
# 4. Wait until running
try:
info = wait_for_running(instance_id)
except TimeoutError as e:
print(f"⚠️ {e}")
print(" Check https://cloud.vast.ai/instances/ manually.")
return
# 5. Print connection details
ssh_host = info.get("ssh_host", "")
ssh_port = info.get("ssh_port", "")
print("\n" + "═"*60)
print(" 🎉 Instance is RUNNING!")
print("═"*60)
print(f" Instance ID : {instance_id}")
print(f" GPU : {info.get('gpu_name')} ×{info.get('num_gpus', 1)}")
print(f" VRAM : {info.get('gpu_ram', 0)/1024:.0f} GB")
print(f" Price : ${info.get('dph_total', 0):.3f} / hr")
print(f" SSH : ssh -p {ssh_port} root@{ssh_host}")
print("═"*60)
print("\n Run 'python vast_destroy.py' (or use the dashboard) to stop billing.\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment