Spaces:
Running on Zero
Running on Zero
Fix GPU task abort and duplicate processing
Browse files- Set @spaces.GPU(duration=300) to prevent ZeroGPU timeout (8B model needs ~3min)
- Add explicit pipe.to("cuda") inside GPU function
- Remove input_image.change handler causing duplicate GPU tasks
- Use BriaFiboEditPipeline directly (built into diffusers, trust_remote_code unnecessary)
- Move theme param to launch() per Gradio 6.0 API
- Add show_error=True for better error visibility
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- app.py +8 -17
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,30 +1,27 @@
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
from diffusers import
|
| 5 |
from PIL import Image
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
-
# Load the model
|
| 9 |
print("Loading Fibo-Edit-RMBG model...")
|
| 10 |
-
pipe =
|
| 11 |
"briaai/Fibo-Edit-RMBG",
|
| 12 |
torch_dtype=torch.bfloat16,
|
| 13 |
-
trust_remote_code=True,
|
| 14 |
)
|
| 15 |
-
|
| 16 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
-
# pipe.to(device)
|
| 18 |
-
print(f"Model loaded on {device}")
|
| 19 |
|
| 20 |
INSTRUCTION = {'edit_instruction':'Generate a detailed grayscale alpha matte. Map the opaque foreground to white and the background to black. Produce soft, anti-aliased grayscale gradients at the edges of the subject to represent fine details and transparency.'}
|
| 21 |
|
| 22 |
|
| 23 |
-
@spaces.GPU
|
| 24 |
def process(image, num_steps=10, guidance_scale=1.0):
|
| 25 |
if image is None:
|
| 26 |
return None, None
|
| 27 |
|
|
|
|
| 28 |
mask = pipe(
|
| 29 |
image=image,
|
| 30 |
prompt=INSTRUCTION,
|
|
@@ -46,7 +43,7 @@ def process(image, num_steps=10, guidance_scale=1.0):
|
|
| 46 |
return (image.convert("RGBA"), result), tmp.name
|
| 47 |
|
| 48 |
|
| 49 |
-
with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal"
|
| 50 |
gr.Markdown("""
|
| 51 |
# Fibo-Edit-RMBG - Background Removal
|
| 52 |
|
|
@@ -114,11 +111,5 @@ For commercial use, please [contact Bria AI](https://bria.ai/contact-us).
|
|
| 114 |
outputs=[slider, download_file],
|
| 115 |
)
|
| 116 |
|
| 117 |
-
input_image.change(
|
| 118 |
-
fn=process,
|
| 119 |
-
inputs=[input_image],
|
| 120 |
-
outputs=[slider, download_file],
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
demo.launch()
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
from diffusers import BriaFiboEditPipeline
|
| 5 |
from PIL import Image
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
+
# Load the model on CPU at module level (ZeroGPU moves to CUDA inside @spaces.GPU)
|
| 9 |
print("Loading Fibo-Edit-RMBG model...")
|
| 10 |
+
pipe = BriaFiboEditPipeline.from_pretrained(
|
| 11 |
"briaai/Fibo-Edit-RMBG",
|
| 12 |
torch_dtype=torch.bfloat16,
|
|
|
|
| 13 |
)
|
| 14 |
+
print("Model loaded on CPU")
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
INSTRUCTION = {'edit_instruction':'Generate a detailed grayscale alpha matte. Map the opaque foreground to white and the background to black. Produce soft, anti-aliased grayscale gradients at the edges of the subject to represent fine details and transparency.'}
|
| 17 |
|
| 18 |
|
| 19 |
+
@spaces.GPU(duration=300)
|
| 20 |
def process(image, num_steps=10, guidance_scale=1.0):
|
| 21 |
if image is None:
|
| 22 |
return None, None
|
| 23 |
|
| 24 |
+
pipe.to("cuda")
|
| 25 |
mask = pipe(
|
| 26 |
image=image,
|
| 27 |
prompt=INSTRUCTION,
|
|
|
|
| 43 |
return (image.convert("RGBA"), result), tmp.name
|
| 44 |
|
| 45 |
|
| 46 |
+
with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
| 47 |
gr.Markdown("""
|
| 48 |
# Fibo-Edit-RMBG - Background Removal
|
| 49 |
|
|
|
|
| 111 |
outputs=[slider, download_file],
|
| 112 |
)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
demo.launch(show_error=True, theme=gr.themes.Soft(primary_hue="sky"))
|
requirements.txt
CHANGED
|
@@ -5,4 +5,4 @@ accelerate==1.13.0
|
|
| 5 |
safetensors>=0.7.0
|
| 6 |
pillow==12.1.1
|
| 7 |
gradio==6.9.0
|
| 8 |
-
spaces==0.47.0
|
|
|
|
| 5 |
safetensors>=0.7.0
|
| 6 |
pillow==12.1.1
|
| 7 |
gradio==6.9.0
|
| 8 |
+
spaces==0.47.0
|