Fast api zero GPU problem

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”,
},
license_info={
“name”: “MIT”,
“url”: “The MIT License – Open Source Initiative”,
},
)

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
)

1 Like

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.

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