Spaces:
Running on Zero
Running on Zero
fix: add @spaces.GPU decorators, dynamic device/dtype fallback, and Gradio 6.0 launch params
Browse files
README.md
CHANGED
|
@@ -5,6 +5,7 @@ colorFrom: gray
|
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.20.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.20.0
|
| 8 |
+
python_version: "3.10"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
app.py
CHANGED
|
@@ -6,8 +6,10 @@ import numpy as np
|
|
| 6 |
import requests
|
| 7 |
import torch
|
| 8 |
import gc
|
|
|
|
| 9 |
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 11 |
|
| 12 |
# Download and Create SAM Model
|
| 13 |
|
|
@@ -34,11 +36,11 @@ gc.collect()
|
|
| 34 |
print("Creating ControlNet Pipeline")
|
| 35 |
|
| 36 |
controlnet = ControlNetModel.from_pretrained(
|
| 37 |
-
"mfidabel/controlnet-segment-anything", torch_dtype=
|
| 38 |
).to(device)
|
| 39 |
|
| 40 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 41 |
-
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=
|
| 42 |
).to(device)
|
| 43 |
|
| 44 |
|
|
@@ -109,6 +111,7 @@ def show_anns(anns):
|
|
| 109 |
|
| 110 |
return final_img
|
| 111 |
|
|
|
|
| 112 |
def segment_image(image, seed = 0):
|
| 113 |
# Generate Masks
|
| 114 |
np.random.seed(int(seed))
|
|
@@ -121,6 +124,7 @@ def segment_image(image, seed = 0):
|
|
| 121 |
torch.cuda.empty_cache()
|
| 122 |
return map
|
| 123 |
|
|
|
|
| 124 |
def infer(prompts, negative_prompts, image, num_inference_steps = 50, seed = 4, num_samples = 4):
|
| 125 |
try:
|
| 126 |
# Segment Image
|
|
@@ -161,7 +165,7 @@ prompt = gr.Textbox(lines=1, label="Prompt", value=default_example[0])
|
|
| 161 |
negative_prompt = gr.Textbox(lines=1, label="Negative Prompt", value=default_example[1])
|
| 162 |
|
| 163 |
|
| 164 |
-
with gr.Blocks(
|
| 165 |
with gr.Row():
|
| 166 |
with gr.Column():
|
| 167 |
# Title
|
|
@@ -221,4 +225,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 221 |
outputs=segm_img)
|
| 222 |
|
| 223 |
demo.queue()
|
| 224 |
-
demo.launch()
|
|
|
|
| 6 |
import requests
|
| 7 |
import torch
|
| 8 |
import gc
|
| 9 |
+
import spaces
|
| 10 |
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 13 |
|
| 14 |
# Download and Create SAM Model
|
| 15 |
|
|
|
|
| 36 |
print("Creating ControlNet Pipeline")
|
| 37 |
|
| 38 |
controlnet = ControlNetModel.from_pretrained(
|
| 39 |
+
"mfidabel/controlnet-segment-anything", torch_dtype=dtype
|
| 40 |
).to(device)
|
| 41 |
|
| 42 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 43 |
+
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=dtype
|
| 44 |
).to(device)
|
| 45 |
|
| 46 |
|
|
|
|
| 111 |
|
| 112 |
return final_img
|
| 113 |
|
| 114 |
+
@spaces.GPU(duration=30)
|
| 115 |
def segment_image(image, seed = 0):
|
| 116 |
# Generate Masks
|
| 117 |
np.random.seed(int(seed))
|
|
|
|
| 124 |
torch.cuda.empty_cache()
|
| 125 |
return map
|
| 126 |
|
| 127 |
+
@spaces.GPU(duration=60)
|
| 128 |
def infer(prompts, negative_prompts, image, num_inference_steps = 50, seed = 4, num_samples = 4):
|
| 129 |
try:
|
| 130 |
# Segment Image
|
|
|
|
| 165 |
negative_prompt = gr.Textbox(lines=1, label="Negative Prompt", value=default_example[1])
|
| 166 |
|
| 167 |
|
| 168 |
+
with gr.Blocks() as demo:
|
| 169 |
with gr.Row():
|
| 170 |
with gr.Column():
|
| 171 |
# Title
|
|
|
|
| 225 |
outputs=segm_img)
|
| 226 |
|
| 227 |
demo.queue()
|
| 228 |
+
demo.launch(css=css)
|