Spaces:
Running on Zero
Running on Zero
Load only the 0.8B adapter eagerly; lazy-load the 2B on first use (fix silent main-process kill)
Browse files9 unmodified lines101112131415161718192050 unmodified lines717273747576777 unmodified lines858687888990919249 unmodified lines1421431441451461479 unmodified linesMODEL_CHOICES = ["naver-ai/ReadPoint-0.8B", "naver-ai/ReadPoint-2B"]DEFAULT_MODE = "Point + box + self-check"# ZeroGPU rule: load every variant eagerly at module scope. A lazily-filled dict would be lost on# every cold worker fork (workers inherit the main process's memory), forcing a full re-download# and re-merge inside the GPU call. Both adapters together are ~5.6 GB of bf16 — well inside a# 48 GB worker — and each is .to("cuda")-packed once so any call streams in quickly.READERS = {name: FastReader(name) for name in MODEL_CHOICES}def _load_font(size: int):50 unmodified linesreturn Image.fromarray(arr)@spaces.GPU(duration=120)def read_point(image, target: str = "", model_choice: str = MODEL_CHOICES[0], mode: str = DEFAULT_MODE):"""Point at a target in an image and read the probability of every point in one forward pass.7 unmodified linesraise gr.Error("Please provide an image.")if not target or not target.strip():raise gr.Error("Please describe what to point at.")img = image if isinstance(image, Image.Image) else Image.open(image).convert("RGB")reader = READERS[model_choice if model_choice in READERS else MODEL_CHOICES[0]]full = mode == "Point + box + self-check"t0 = time.perf_counter()res = reader.read(img, target, full=full)49 unmodified linesoverlay_out = gr.Image(type="pil", label="Point (and box) overlay", height=340, show_label=True)heat_out = gr.Image(type="pil", label="Probability of every point (1000 × 1000 grid)", height=340)report = gr.Markdown()gr.Examples(examples=[["examples/cat_tabby.jpg", "the cat's left eye"],9 unmodified lines1011121314151617181920212223242526272829303132333450 unmodified lines858687888990917 unmodified lines9910010110210310410510610749 unmodified lines1571581591601611621639 unmodified linesMODEL_CHOICES = ["naver-ai/ReadPoint-0.8B", "naver-ai/ReadPoint-2B"]DEFAULT_MODE = "Point + box + self-check"# ZeroGPU rule: the primary adapter loads eagerly at module scope so every cold worker inherits it.# The 2B variant is loaded on first use inside the GPU call (a cold worker pays the download+merge# once; warm workers keep it) — the duration estimator below grants time for exactly that case.READERS = {MODEL_CHOICES[0]: FastReader(MODEL_CHOICES[0])}def _get_reader(model_choice: str) -> FastReader:"""Return the reader for the chosen adapter, loading the 2B on first use."""if model_choice not in READERS:READERS[model_choice] = FastReader(model_choice)return READERS[model_choice]def _estimate_duration(*args, **kwargs):"""Grant extra GPU time only when the chosen adapter still needs a cold download + merge."""# read_point(image, target, model_choice, mode): the adapter is the 3rd positional arg,# or a keyword; examples only pass (image, target), which lands on the eagerly-loaded 0.8B.mc = kwargs.get("model_choice") or (args[2] if len(args) > 2 else MODEL_CHOICES[0])return 360 if mc not in READERS else 120def _load_font(size: int):50 unmodified linesreturn Image.fromarray(arr)@spaces.GPU(duration=_estimate_duration)def read_point(image, target: str = "", model_choice: str = MODEL_CHOICES[0], mode: str = DEFAULT_MODE):"""Point at a target in an image and read the probability of every point in one forward pass.7 unmodified linesraise gr.Error("Please provide an image.")if not target or not target.strip():raise gr.Error("Please describe what to point at.")img = image if isinstance(image, Image.Image) else Image.open(image)img = img.convert("RGB")reader = _get_reader(model_choice if model_choice in MODEL_CHOICES else MODEL_CHOICES[0])full = mode == "Point + box + self-check"t0 = time.perf_counter()res = reader.read(img, target, full=full)49 unmodified linesoverlay_out = gr.Image(type="pil", label="Point (and box) overlay", height=340, show_label=True)heat_out = gr.Image(type="pil", label="Probability of every point (1000 × 1000 grid)", height=340)report = gr.Markdown()run.click(read_point, [image_in, target, model_choice, mode], [overlay_out, heat_out, report])gr.Examples(examples=[["examples/cat_tabby.jpg", "the cat's left eye"],
app.py+24-8