Download model_runtime.py from loveaa/OneAlign: direct link, hf CLI and curl.
- Browser
- Download file 3.16 kB
-
https://huggingface.co/spaces/loveaa/OneAlign/resolve/main/model_runtime.py
- Command line
-
hf download hf://spaces/loveaa/OneAlign/model_runtime.py
-
curl -L -o model_runtime.py https://huggingface.co/spaces/loveaa/OneAlign/resolve/main/model_runtime.py
3.16 kB
| """HF-hosted imajev, using the same adapter revision as the verified local app.""" | |
| from pathlib import Path | |
| import base64 | |
| import io | |
| import json | |
| import os | |
| import sys | |
| from time import perf_counter | |
| ROOT = Path(__file__).resolve().parent | |
| for folder in (ROOT/'upstream/src', ROOT/'upstream/scripts', ROOT/'upstream/scripts/playground'): | |
| sys.path.insert(0, str(folder)) | |
| def load_model(): | |
| from huggingface_hub import snapshot_download | |
| from server import TorchBackend | |
| from vision_decision.calibration import TemperatureCalibrator | |
| base = snapshot_download('Qwen/Qwen3.5-4B', revision='851bf6e806efd8d0a36b00ddf55e13ccb7b8cd0a', allow_patterns=['*.json','*.safetensors','*.txt','*.jinja'], token=False) | |
| adapter = snapshot_download('mohit67890/imajev-4b', revision='f8d8234cebc6c99065c07731e59716dc0a6e27ab', allow_patterns=['*.json','*.safetensors'], ignore_patterns=['mlx/*','assets/*'], token=False) | |
| runtime = Path(os.getenv('HOME_PULSE_DATA_DIR', '/tmp/home-pulse')) | |
| runtime.mkdir(parents=True, exist_ok=True) | |
| bundle = runtime/'model-bundle.json' | |
| bundle.write_text(json.dumps({'path':base})) | |
| # ZeroGPU emulates CUDA at module startup and materializes it inside spaces.GPU. | |
| backend = TorchBackend(bundle=bundle, adapter=Path(adapter), device='cuda', rotations=1, max_input_tokens=4096, fast=False, merge_lora=False) | |
| backend.model = 'imajev-4b-hf-bf16' | |
| backend.engine.processor._vision() | |
| calibration = TemperatureCalibrator.load(Path(adapter)/'calibration.json') | |
| return backend, calibration | |
| def predict(backend, calibration, payload): | |
| from PIL import Image | |
| import torch | |
| from server import compile_payload | |
| from vision_decision.jev_api import to_response | |
| start=perf_counter() | |
| supplied=payload.get('images',[]) | |
| if not 1<=len(supplied)<=2: raise ValueError('Provide one or two images') | |
| images=[] | |
| for value in supplied: | |
| if not isinstance(value,str) or not value.startswith('data:image/') or ';base64,' not in value: raise ValueError('Invalid image') | |
| blob=base64.b64decode(value.split(',',1)[1],validate=True) | |
| if len(blob)>3_000_000: raise ValueError('Image too large') | |
| with Image.open(io.BytesIO(blob)) as original: | |
| if original.width*original.height>16_000_000: raise ValueError('Image dimensions too large') | |
| image=original.convert('RGB');image.thumbnail((640,640));images.append(image) | |
| if not 1<=len(payload.get('questions',{}))<=4: raise ValueError('Provide 1–4 questions') | |
| request, plan=compile_payload({k:v for k,v in payload.items() if k not in ('images','model','thinking')},backend.max_options) | |
| with torch.inference_mode(): results,usage=backend.score(images,request) | |
| torch.cuda.synchronize() | |
| results=[calibration.calibrate_result(result,field.type,len(result.scores)-1,image=True,photo_only=not request.state) for field,result in zip(request.fields,results)] | |
| response=to_response(request,results,model=backend.model,plan=plan) | |
| response['usage']={**usage,'total_ms':round((perf_counter()-start)*1000,1),'processed_image_sizes':[list(im.size) for im in images]} | |
| return response | |