Spaces:
Running on Zero
Running on Zero
Download serving/app/fallback.py from DavidPanSX/pharmascan-api: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/DavidPanSX/pharmascan-api/resolve/main/serving/app/fallback.py
- Command line
-
hf download hf://spaces/DavidPanSX/pharmascan-api/serving/app/fallback.py
-
curl -L -o fallback.py https://huggingface.co/spaces/DavidPanSX/pharmascan-api/resolve/main/serving/app/fallback.py
1.15 kB
| """The Space's model pass (serving/space_app.py): a ZeroGPU GPU when one is granted, else the CPU.""" | |
| import time | |
| class GpuOrCpu: | |
| """ | |
| Calls gpu(x); when it raises (ZeroGPU allowance used up, queue too long), answers with cpu(x) instead and skips | |
| the GPU for `cooldown` seconds, so a refused GPU doesn't slow every request. `.device` says where photos run now. | |
| """ | |
| def __init__(self, gpu, cpu, cooldown=600, clock=time.monotonic): | |
| self.gpu, self.cpu, self.cooldown, self.clock = gpu, cpu, cooldown, clock | |
| self.cpu_until = float("-inf") | |
| def device(self): | |
| return "zerogpu" if self.clock() >= self.cpu_until else "cpu (ZeroGPU refused a recent call)" | |
| def __call__(self, *args): | |
| if self.clock() >= self.cpu_until: | |
| try: | |
| return self.gpu(*args) | |
| except Exception as error: # ZeroGPU raises gradio.Error for quota and queue refusals | |
| self.cpu_until = self.clock() + self.cooldown | |
| print(f"ZeroGPU refused, CPU for {self.cooldown} s: {type(error).__name__}: {error}", flush=True) | |
| return self.cpu(*args) | |