Spaces:
Sleeping
Sleeping
File size: 7,944 Bytes
1b0992e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# modelblob.py
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, JSONResponse
import json, os, pathlib
router = APIRouter()
LOCAL_BLOB_PATH = os.getenv("MODEL_BLOB_PATH", "/tmp/model_blob.json")
# ---- EMBEDDED MODEL BLOB (exactly as provided) ----
EMBEDDED_MODEL_BLOB = {
"name": "publishers/hf-misri/models/realismenginesdxl_v30vae",
"versionId": "001",
"openSourceCategory": "THIRD_PARTY_OWNED_OSS",
"supportedActions": {
"deploy": {
"modelDisplayName": "misri/realismEngineSDXL_v30VAE",
"containerSpec": {
"imageUri": "us-docker.pkg.dev/deeplearning-platform-release/vertex-model-garden/hf-inference-toolkit.cu125.0-1.ubuntu2204.py311:model-garden.hf-inference-toolkit-0-1-release_20250927.00_p0",
"env": [
{"name": "HF_TASK","value": "text-to-image"},
{"name": "MODEL_ID","value": "misri/realismEngineSDXL_v30VAE"},
{"name": "HF_MODEL_ID","value": "misri/realismEngineSDXL_v30VAE"},
{"name": "HF_REVISION","value": "7d2f2de544b4aa26148b3a16b3469ed6dbb38a5c"},
{"name": "DEPLOY_SOURCE","value": "UI_HF_VERIFIED_MODEL"}
],
"ports": [{"containerPort": 8080}]
},
"dedicatedResources": {
"machineSpec": {
"machineType": "a3-highgpu-1g",
"acceleratorType": "NVIDIA_H100_80GB",
"acceleratorCount": 1
},
"maxReplicaCount": 1
},
"deployTaskName": "1 NVIDIA_H100_80GB a3-highgpu-1g",
"deployMetadata": {}
},
"multiDeployVertex": {
"multiDeployVertex": [
{
"modelDisplayName": "misri/realismEngineSDXL_v30VAE",
"containerSpec": {
"imageUri": "us-docker.pkg.dev/deeplearning-platform-release/vertex-model-garden/hf-inference-toolkit.cu125.0-1.ubuntu2204.py311:model-garden.hf-inference-toolkit-0-1-release_20250927.00_p0",
"env": [
{"name": "HF_TASK","value": "text-to-image"},
{"name": "MODEL_ID","value": "misri/realismEngineSDXL_v30VAE"},
{"name": "HF_MODEL_ID","value": "misri/realismEngineSDXL_v30VAE"},
{"name": "HF_REVISION","value": "7d2f2de544b4aa26148b3a16b3469ed6dbb38a5c"},
{"name": "DEPLOY_SOURCE","value": "UI_HF_VERIFIED_MODEL"}
],
"ports": [{"containerPort": 8080}]
},
"dedicatedResources": {
"machineSpec": {
"machineType": "a3-highgpu-1g",
"acceleratorType": "NVIDIA_H100_80GB",
"acceleratorCount": 1
},
"maxReplicaCount": 1
},
"deployTaskName": "1 NVIDIA_H100_80GB a3-highgpu-1g",
"deployMetadata": {}
},
{
"modelDisplayName": "misri/realismEngineSDXL_v30VAE",
"containerSpec": {
"imageUri": "us-docker.pkg.dev/deeplearning-platform-release/vertex-model-garden/hf-inference-toolkit.cu125.0-1.ubuntu2204.py311:model-garden.hf-inference-toolkit-0-1-release_20250927.00_p0",
"env": [
{"name": "HF_TASK","value": "text-to-image"},
{"name": "MODEL_ID","value": "misri/realismEngineSDXL_v30VAE"},
{"name": "HF_MODEL_ID","value": "misri/realismEngineSDXL_v30VAE"},
{"name": "HF_REVISION","value": "7d2f2de544b4aa26148b3a16b3469ed6dbb38a5c"},
{"name": "DEPLOY_SOURCE","value": "UI_HF_VERIFIED_MODEL"}
],
"ports": [{"containerPort": 8080}]
},
"dedicatedResources": {
"machineSpec": {
"machineType": "g2-standard-12",
"acceleratorType": "NVIDIA_L4",
"acceleratorCount": 1
},
"maxReplicaCount": 1
},
"deployTaskName": "1 NVIDIA_L4 g2-standard-12",
"deployMetadata": {}
}
]
}
}
}
# ---------------------------------------------------
def _ensure_dir(p: str):
pathlib.Path(p).parent.mkdir(parents=True, exist_ok=True)
def _pretty(obj) -> str:
try:
return json.dumps(obj, indent=2)
except Exception:
return str(obj)
@router.get("/modelblob", response_class=HTMLResponse)
def view_model_blob():
"""
Render: if /tmp/model_blob.json exists, show it; otherwise show the embedded blob.
"""
try:
if os.path.exists(LOCAL_BLOB_PATH):
raw = pathlib.Path(LOCAL_BLOB_PATH).read_text(encoding="utf-8")
try:
disp = json.dumps(json.loads(raw), indent=2)
except json.JSONDecodeError:
disp = raw
source = f"File • {LOCAL_BLOB_PATH}"
else:
disp = _pretty(EMBEDDED_MODEL_BLOB)
source = "Embedded (not yet written to file)"
except Exception as e:
disp = _pretty({"error": str(e)})
source = "Error"
html = f"""
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Model Blob</title>
<style>
body {{ font-family: ui-monospace, Menlo, Consolas, monospace; background:#0d1117; color:#c9d1d9; margin:24px; }}
pre {{ background:#161b22; padding:16px; border-radius:8px; overflow:auto; white-space:pre-wrap; }}
.row {{ max-width: 1000px; margin:auto; }}
a {{ color:#58a6ff; text-decoration:none; }}
.btn {{ display:inline-block; padding:8px 12px; border-radius:6px; background:#238636; color:#fff; margin-right:8px; }}
.btn.secondary {{ background:#444; }}
</style>
</head>
<body>
<div class="row">
<h2>Current Model Blob <small style="font-size:12px; color:#8b949e;">({source})</small></h2>
<div style="margin:10px 0;">
<a class="btn" href="/modelblob/write">Write to /tmp/model_blob.json</a>
<a class="btn secondary" href="/">Back</a>
</div>
<pre>{disp}</pre>
</div>
</body>
</html>
"""
return HTMLResponse(html)
@router.get("/modelblob/write")
def write_model_blob():
"""
Write the embedded blob verbatim to LOCAL_BLOB_PATH.
"""
try:
_ensure_dir(LOCAL_BLOB_PATH)
pathlib.Path(LOCAL_BLOB_PATH).write_text(_pretty(EMBEDDED_MODEL_BLOB), encoding="utf-8")
return JSONResponse({"ok": True, "path": LOCAL_BLOB_PATH})
except Exception as e:
return JSONResponse({"error": str(e)}, 500)
@router.post("/modelblob/overwrite")
async def overwrite_blob(req: Request):
"""
Overwrite the file with a posted blob (JSON or form 'blob').
"""
try:
ctype = req.headers.get("content-type","")
if "application/json" in ctype:
data = await req.json()
txt = data if isinstance(data, (dict, list)) else data.get("blob")
else:
form = await req.form()
txt = form.get("blob")
if isinstance(txt, (dict, list)):
out = json.dumps(txt, indent=2)
elif isinstance(txt, str) and txt.strip():
out = txt
else:
return JSONResponse({"error": "Missing valid blob"}, 400)
_ensure_dir(LOCAL_BLOB_PATH)
pathlib.Path(LOCAL_BLOB_PATH).write_text(out, encoding="utf-8")
return JSONResponse({"ok": True, "path": LOCAL_BLOB_PATH})
except Exception as e:
return JSONResponse({"error": str(e)}, 500)
# ---------------------------------------------------------------------
# JSON outlet for backend ingestion
# ---------------------------------------------------------------------
@router.get("/modelblob.json")
def modelblob_json():
"""Return the current model blob as JSON for backend ingestion."""
import os, json, pathlib
path = os.getenv("MODEL_BLOB_PATH", "/tmp/model_blob.json")
try:
if os.path.exists(path):
return json.loads(pathlib.Path(path).read_text(encoding="utf-8"))
except Exception:
pass
# fallback to embedded blob if file missing or unreadable
return EMBEDDED_MODEL_BLOB
|