Spaces:
Running on Zero
Running on Zero
Download backend/rendering.py from fffiloni/idol-commons: direct link, hf CLI and curl.
- Browser
- Download file 7.64 kB
-
https://huggingface.co/spaces/fffiloni/idol-commons/resolve/main/backend/rendering.py
- Command line
-
hf download hf://spaces/fffiloni/idol-commons/backend/rendering.py
-
curl -L -o rendering.py https://huggingface.co/spaces/fffiloni/idol-commons/resolve/main/backend/rendering.py
7.64 kB
| """Bounded subprocesses; no HTTP APIs accepting renderer URLs or file paths.""" | |
| import hashlib,json,math,os,re,signal,subprocess,sys,time,tempfile | |
| from pathlib import Path | |
| from .settings import ROOT | |
| from .validation import InvalidAsset,normalize_preview,IMAGE_ERRORS | |
| def bounded_run(args,cwd,env,timeout=120,max_rss=3*1024**3,require_group_memory=True): | |
| # Capture bounded diagnostics to a private regular file, never return secrets/logs to clients. | |
| proc_available=Path('/proc').is_dir() | |
| if require_group_memory and not proc_available:raise InvalidAsset('This runtime cannot enforce the preview memory limit.') | |
| log=Path(cwd)/'process.log' | |
| with log.open('wb') as output: | |
| process=subprocess.Popen(args,cwd=cwd,env=env,stdout=output,stderr=subprocess.STDOUT,start_new_session=True) | |
| started=time.monotonic() | |
| try: | |
| while process.poll() is None: | |
| if time.monotonic()-started>timeout:raise InvalidAsset('Preview processing exceeded its time limit.') | |
| total=0 | |
| for p in (Path('/proc').iterdir() if proc_available else []): | |
| if not p.name.isdigit():continue | |
| try: | |
| stat=(p/'stat').read_text().split(') ',1)[1].split() | |
| if int(stat[2])==process.pid:total+=int(stat[21])*os.sysconf('SC_PAGE_SIZE') | |
| except (OSError,ValueError,IndexError):pass | |
| if total>max_rss or log.stat().st_size>2*1024*1024:raise InvalidAsset('Preview processing exceeded its resource limit.') | |
| time.sleep(.1) | |
| if process.returncode: | |
| # Bounded diagnostics: private process logs are never returned to clients. | |
| with log.open('rb') as diagnostic_file:detail=diagnostic_file.read(2*1024*1024).decode('utf-8','replace') | |
| known={'socket_path':'Socket path too long','crashpad_home':'--database is required','sandbox':'No usable sandbox','webgl':'Error creating WebGL context', | |
| 'missing_browser':'Executable doesn\'t exist','browser_timeout':'browserType.launch: Timeout', | |
| 'render_timeout':'TimeoutError','module_missing':'ERR_MODULE_NOT_FOUND'} | |
| reason=next((k for k,v in known.items() if v in detail),'process_exit') | |
| # The subprocess has no credentials, prompt, metadata or remote | |
| # input. Strip private local paths and loopback capability URLs. | |
| diagnostic=re.sub(r'https?://[^\s\"\'<>]+','<url>',detail[-3500:]) | |
| diagnostic=re.sub(r'/(?:[^\s\"\'<>]+)','<path>',diagnostic) | |
| print(json.dumps({'event':'idol_preview_failure','exit_code':process.returncode,'reason':reason,'diagnostic':diagnostic[-2500:]}),flush=True) | |
| raise InvalidAsset('The preview could not be rendered. Your private result can be retried.') | |
| finally: | |
| # Kill remaining browser workers, also after parent exit. | |
| try:os.killpg(process.pid,signal.SIGKILL) | |
| except ProcessLookupError:pass | |
| process.wait(timeout=5) | |
| def validate_isolated(path,count): | |
| import tempfile | |
| with tempfile.TemporaryDirectory(dir=path.parent,prefix='validate-') as folder: | |
| env={'PATH':os.environ.get('PATH',''),'PYTHONPATH':str(ROOT),'OPENBLAS_NUM_THREADS':'1','OMP_NUM_THREADS':'1'} | |
| bounded_run([sys.executable,str(ROOT/'tools/validate_ply.py'),str(path),'--count',str(count)],folder,env,timeout=15,max_rss=768*1024**2,require_group_memory=False) | |
| lines=(Path(folder)/'process.log').read_text().splitlines() | |
| data=json.loads(lines[-1]) | |
| if not data.get('ok'):raise InvalidAsset('The generated PLY failed technical validation.') | |
| return data['result'] | |
| def normalize_image_isolated(raw,destination): | |
| import tempfile | |
| with tempfile.TemporaryDirectory(dir=destination.parent,prefix='decode-') as folder: | |
| source=Path(folder)/'input.raw';source.write_bytes(raw);os.chmod(source,0o600) | |
| env={'PATH':os.environ.get('PATH',''),'PYTHONPATH':str(ROOT),'OPENBLAS_NUM_THREADS':'1','OMP_NUM_THREADS':'1'} | |
| try:bounded_run([sys.executable,str(ROOT/'tools/normalize_image.py'),str(source),str(destination)],folder,env,timeout=15,require_group_memory=False) | |
| except InvalidAsset:raise InvalidAsset('This photo could not be prepared. Export it as JPEG or PNG, then try again.') from None | |
| data=json.loads((Path(folder)/'process.log').read_text().splitlines()[-1]) | |
| if not data.get('ok'):raise InvalidAsset(IMAGE_ERRORS.get(data.get('code'),IMAGE_ERRORS['decode'])) | |
| return data['result'] | |
| def frame_from_bounds(bounds): | |
| # IDOL's unmodified exporter maps (x,y,z) -> (x,-z,y): world up is -Y. | |
| # Match Gradio's gsplat orbit hemisphere (yaw=pitch=.5), which faces -Z. | |
| # Keep the PLY and Gaussian quaternions untouched. Both renderers use this camera. | |
| target=[(a+b)/2 for a,b in zip(bounds['min'],bounds['max'])] | |
| radius=max(.2,math.dist(bounds['min'],bounds['max'])/2) | |
| distance=radius/math.sin(math.radians(35)/2)*1.25 | |
| direction=[math.sin(.5)*math.cos(.5),-math.sin(.5),-math.cos(.5)*math.cos(.5)] | |
| return {'position':[c+d*distance for c,d in zip(target,direction)],'target':target,'up':[0.0,-1.0,0.0], | |
| 'fov_y_degrees':35.0,'near':.001,'far':max(100.0,distance+radius*5)} | |
| def render_preview(folder:Path,validation,direction=None): | |
| camera=frame_from_bounds(validation['bounds']) | |
| if direction is not None: | |
| # The browser chooses an orbit direction, never paths, images or an | |
| # arbitrary projection. Server bounds determine target and safe fit. | |
| distance=math.dist(camera['position'],camera['target']) | |
| camera['position']=[c+d*distance for c,d in zip(camera['target'],direction)] | |
| (folder/'camera.json').write_text(json.dumps(camera)) | |
| allowed=('PATH','CHROMIUM_EXECUTABLE','PLAYWRIGHT_BROWSERS_PATH','RENDER_SOFTWARE','CHROMIUM_NO_SANDBOX') | |
| # Chromium's Unix socket paths must fit Linux's ~108-byte limit. Nesting | |
| # its profile under a full generation UUID exceeds it. mkdtemp supplies a | |
| # short, unique 0700 directory; bounded_run kills the process group before | |
| # this context removes it. Source and output assets stay in private staging. | |
| with tempfile.TemporaryDirectory(prefix='idol-',dir='/tmp') as temporary: | |
| tmp=Path(temporary) | |
| env={k:os.environ[k] for k in allowed if k in os.environ};env['TMPDIR']=str(tmp) | |
| env.update(HOME=str(tmp),XDG_CONFIG_HOME=str(tmp/'config'),XDG_CACHE_HOME=str(tmp/'cache')) | |
| bounded_run(['node',str(ROOT/'frontend/scripts/render-splat.mjs'),str(folder/'model.ply'),str(folder/'preview-raw.png'),str(folder/'render.json'),str(folder/'camera.json')],folder,env) | |
| render=json.loads((folder/'render.json').read_text()) | |
| if render['count']!=validation['gaussian_count']:raise InvalidAsset('The renderer did not load every Gaussian.') | |
| previews=normalize_preview((folder/'preview-raw.png').read_bytes(),folder/'preview.png') | |
| # Hash the entire built renderer/application graph, not an invented build identifier. | |
| h=hashlib.sha256() | |
| for file in sorted((ROOT/'frontend/dist').rglob('*')): | |
| if file.is_file():h.update(str(file.relative_to(ROOT/'frontend/dist')).encode());h.update(file.read_bytes()) | |
| framing={'renderer':{'name':'spark','version':'2.4.0','bundle_sha256':h.hexdigest(),'profile':'idol-sh0-extended-v1'},'camera':camera, | |
| 'source_canvas':[1024,1024],'crop':previews['crop'],'alpha_threshold':3,'color_space':'srgb'} | |
| return previews,framing | |