# Fast api zero GPU problem

**URL:** <https://discuss.huggingface.co/t/fast-api-zero-gpu-problem/159053>\
**Category:** Beginners\
**Created:** [June 12, 2025, 2:45pm UTC](https://discuss.huggingface.co/t/fast-api-zero-gpu-problem/159053 "2025-06-12T14:45:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Munaf1987](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/munaf1987/32/48868_2.png) [@Munaf1987](https://discuss.huggingface.co/u/Munaf1987)\
**Post date:** [June 12, 2025, 2:45pm UTC](https://discuss.huggingface.co/t/fast-api-zero-gpu-problem/159053/1 "2025-06-12T14:45:54Z")

</div>

I have following code. I get error runtime error  
No @spaces.GPU function detected during startup

I tried so much but can’t execute the code

My code

import os  
import uuid  
import base64  
import numpy as np  
import cv2  
import onnxruntime as ort  
from PIL import Image  
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Query, Depends  
from fastapi.responses import JSONResponse, FileResponse, HTMLResponse  
from fastapi.staticfiles import StaticFiles  
from fastapi.middleware.cors import CORSMiddleware  
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials  
from pydantic import BaseModel, Field  
from typing import Optional, List, Union  
import gradio as gr  
import uvicorn  
from datetime import datetime  
import spaces

# Set up environment variables

os.environ[“GRADIO\_SERVER\_NAME”] = “0.0.0.0”  
os.environ[“GRADIO\_SERVER\_PORT”] = “7860”

# Define the FastAPI app

app = FastAPI(  
title=“Background Removal API”,  
description=“Background removal API with Gradio interface”,  
version=“2.0.0”,  
docs\_url=“/api/docs”,  
redoc\_url=“/api/redoc”,  
openapi\_url=“/api/openapi.json”,  
contact={  
“name”: “Background Removal API”,  
“url”: “[https://github.com/yourusername/background-removal-api](https://github.com/yourusername/background-removal-api)”,  
},  
license\_info={  
“name”: “MIT”,  
“url”: “[The MIT License – Open Source Initiative](https://opensource.org/licenses/MIT)”,  
},  
)

# Define Pydantic models for API documentation

class BackgroundRemovalRequest(BaseModel):  
image\_format: Optional[str] = Field(default=“PNG”, description=“Output image format (PNG, JPEG)”)  
quality: Optional[int] = Field(default=95, ge=1, le=100, description=“Output quality for JPEG (1-100)”)

class BackgroundRemovalResponse(BaseModel):  
status: str = Field(…, description=“Status of the operation”)  
image\_code: str = Field(…, description=“Base64 encoded image with data URI prefix”)  
processing\_time: float = Field(…, description=“Processing time in seconds”)  
original\_size: List[int] = Field(…, description=“Original image dimensions [width, height]”)  
output\_format: str = Field(…, description=“Output image format”)

class ErrorResponse(BaseModel):  
status: str = Field(…, description=“Status of the operation”)  
message: str = Field(…, description=“Error message”)  
error\_code: Optional[str] = Field(None, description=“Specific error code”)

class HealthResponse(BaseModel):  
status: str = Field(…, description=“Service status”)  
timestamp: str = Field(…, description=“Current timestamp”)  
version: str = Field(…, description=“API version”)  
gpu\_available: bool = Field(…, description=“Whether GPU is available”)  
model\_loaded: bool = Field(…, description=“Whether the model is loaded”)

# Define the core processing function

def process\_image\_core(image\_data, use\_gpu=False, output\_format=“PNG”, quality=95):  
# Load the model and preprocess the image  
model\_path = “BiRefNet-portrait-epoch\_150.onnx”  
input\_size = (1024, 1024)  
providers = [“CUDAExecutionProvider”, “CPUExecutionProvider”] if use\_gpu else [“CPUExecutionProvider”]  
session = ort.InferenceSession(model\_path, providers=providers)  
input\_name = session.get\_inputs()[0].name  
input\_tensor, original\_shape, original\_img = preprocess\_image(image\_data)  
output = session.run(None, {input\_name: input\_tensor})  
mask = output[0]  
result\_img = apply\_mask(original\_img, mask, original\_shape)  
result\_pil = Image.fromarray(cv2.cvtColor(result\_img, cv2.COLOR\_BGRA2RGBA))

```
# Save the result
result_filename = f"{uuid.uuid4()}.{output_format.lower()}"
output_path = f"tmp/{result_filename}"
if output_format.upper() == "PNG":
    result_pil.save(output_path, "PNG")
else:
    if result_pil.mode == 'RGBA':
        rgb_img = Image.new('RGB', result_pil.size, (255, 255, 255))
        rgb_img.paste(result_pil, mask=result_pil.split()[-1])
        rgb_img.save(output_path, "JPEG", quality=quality)
    else:
        result_pil.save(output_path, "JPEG", quality=quality)

# Encode the result to base64
with open(output_path, "rb") as img_file:
    base64_image = base64.b64encode(img_file.read()).decode("utf-8")

# Clean up temporary file
os.remove(output_path)

return base64_image

```

@spaces.GPU()

# Define the Gradio interface

def gradio\_processor(image\_np):  
# Convert numpy array to bytes  
success, img\_encoded = cv2.imencode(‘.jpg’, cv2.cvtColor(image\_np, cv2.COLOR\_RGB2BGR))  
if not success:  
raise ValueError(“Failed to encode image”)  
image\_bytes = img\_encoded.tobytes()

```
# Process the image
result_img = process_image_core(image_bytes, use_gpu=False, output_format="PNG")
return result_img

```

interface = gr.Interface(  
fn=gradio\_processor,  
inputs=gr.Image(type=“numpy”, label=“Upload Image”),  
outputs=gr.Image(type=“pil”, label=“Processed Image”),  
title=“Background Removal Service”,  
description=“Upload an image to remove its background”,  
examples=[  
# Add example images if you have them  
],  
flagging\_options=None,  
allow\_flagging=“never”  
)

# Mount the Gradio app

app = gr.mount\_gradio\_app(app, interface, path=“/gradio”, ssr\_mode=False)

# Run the local dev server

if **name** == “ **main** ”:  
uvicorn.run(  
“main:app”,  
host=“0.0.0.0”,  
port=7860,  
reload=False # Disable reload for production  
)

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [June 12, 2025, 3:07pm UTC](https://discuss.huggingface.co/t/fast-api-zero-gpu-problem/159053/2 "2025-06-12T15:07:33Z")

</div>

Hmm… For example, are you trying to use Zero GPU in Docker Space?  
It may work if you call FastAPI from Gradio Space, but I don’t think it is supported in Docker Space.

> [@Can't get Zero-GPU to work with Fastapi/Uvicorn](https://discuss.huggingface.co/t/cant-get-zero-gpu-to-work-with-fastapi-uvicorn/141634):
>
> Hello everyone – I’d really like to use ZeroGPU for my project, but I just can’t get it to work for my use-case, and have been working on it for days now. Basically the issue is: ZeroGPU only works for me when I enable SSR-mode in Gradio 5.+. If I don’t do that I’m running into all the issues about ZeroGPU not finding the account of the user, and applying the very restrictive IP-based limits, which have been discussed in this forum. That wouldn’t be a problem, but I need to server my applicati…

> **[Spaces ZeroGPU: Dynamic GPU Allocation for Spaces](https://huggingface.co/docs/hub/spaces-zerogpu#compatibility)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

> Currently, ZeroGPU Spaces are exclusively compatible with the **Gradio SDK**.
