Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import random
|
| 4 |
-
import spaces
|
| 5 |
-
from diffusers import DiffusionPipeline
|
| 6 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 13 |
)
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
-
MAX_IMAGE_SIZE = 1024
|
| 20 |
|
| 21 |
-
|
| 22 |
-
def infer(
|
| 23 |
-
prompt,
|
| 24 |
-
negative_prompt,
|
| 25 |
-
seed,
|
| 26 |
-
randomize_seed,
|
| 27 |
-
width,
|
| 28 |
-
height,
|
| 29 |
-
guidance_scale,
|
| 30 |
-
num_inference_steps,
|
| 31 |
-
progress=gr.Progress(track_tqdm=True),
|
| 32 |
-
):
|
| 33 |
if randomize_seed:
|
| 34 |
seed = random.randint(0, MAX_SEED)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
pipe.to("cuda")
|
| 38 |
-
try:
|
| 39 |
-
pipe.enable_xformers_memory_efficient_attention()
|
| 40 |
-
except Exception:
|
| 41 |
-
pass # Fallback if xformers isn't needed
|
| 42 |
-
|
| 43 |
-
generator = torch.Generator("cuda").manual_seed(seed)
|
| 44 |
|
|
|
|
| 45 |
image = pipe(
|
| 46 |
prompt=prompt,
|
| 47 |
-
negative_prompt=negative_prompt,
|
| 48 |
-
guidance_scale=guidance_scale,
|
| 49 |
-
num_inference_steps=num_inference_steps,
|
| 50 |
-
width=width,
|
| 51 |
-
height=height,
|
| 52 |
generator=generator,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
).images[0]
|
| 54 |
|
| 55 |
return image, seed
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
with gr.
|
| 61 |
-
|
| 62 |
-
gr.
|
|
|
|
| 63 |
|
| 64 |
-
with gr.
|
| 65 |
-
prompt = gr.Text(label="Prompt", show_label=False, placeholder="Enter your prompt", container=False)
|
| 66 |
-
run_button = gr.Button("Run", variant="primary")
|
| 67 |
-
|
| 68 |
-
result = gr.Image(label="Result", show_label=False)
|
| 69 |
-
|
| 70 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 71 |
-
negative_prompt = gr.Text(label="Negative prompt", placeholder="Optional")
|
| 72 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 73 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
|
| 77 |
-
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
|
| 78 |
-
|
| 79 |
-
with gr.Row():
|
| 80 |
-
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=0.0)
|
| 81 |
-
num_inference_steps = gr.Slider(label="Steps", minimum=1, maximum=4, step=1, value=2)
|
| 82 |
|
| 83 |
run_button.click(
|
| 84 |
fn=infer,
|
| 85 |
-
inputs=[prompt,
|
| 86 |
outputs=[result, seed],
|
| 87 |
api_name="predict"
|
| 88 |
)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
import random
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# 1. Load a CPU-optimized model
|
| 8 |
+
# 'segmind/tiny-sd' is much smaller and faster on CPUs than SDXL
|
| 9 |
+
model_id = "segmind/tiny-sd"
|
| 10 |
|
| 11 |
+
# Use float32 because CPU doesn't support float16 well
|
| 12 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 13 |
+
model_id,
|
| 14 |
+
torch_dtype=torch.float32
|
|
|
|
| 15 |
)
|
| 16 |
+
pipe = pipe.to("cpu")
|
| 17 |
|
| 18 |
+
# Optimize for CPU speed
|
| 19 |
+
pipe.set_progress_bar_config(disable=True)
|
| 20 |
|
| 21 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 22 |
|
| 23 |
+
def infer(prompt, seed, randomize_seed, width, height):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if randomize_seed:
|
| 25 |
seed = random.randint(0, MAX_SEED)
|
| 26 |
|
| 27 |
+
generator = torch.Generator("cpu").manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# We use very low steps (10-15) because CPU is slow
|
| 30 |
image = pipe(
|
| 31 |
prompt=prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
generator=generator,
|
| 33 |
+
num_inference_steps=15,
|
| 34 |
+
guidance_scale=7.0,
|
| 35 |
+
width=width,
|
| 36 |
+
height=height
|
| 37 |
).images[0]
|
| 38 |
|
| 39 |
return image, seed
|
| 40 |
|
| 41 |
+
# Simple UI
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("# CodeIgnite CPU Image Engine")
|
| 44 |
+
with gr.Column():
|
| 45 |
+
prompt = gr.Textbox(label="Prompt", placeholder="A simple cat drawing")
|
| 46 |
+
run_button = gr.Button("Generate (CPU Mode)")
|
| 47 |
+
result = gr.Image(label="Result")
|
| 48 |
|
| 49 |
+
with gr.Accordion("Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 51 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 52 |
+
width = gr.Slider(label="Width", minimum=256, maximum=512, step=32, value=384)
|
| 53 |
+
height = gr.Slider(label="Height", minimum=256, maximum=512, step=32, value=384)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
run_button.click(
|
| 56 |
fn=infer,
|
| 57 |
+
inputs=[prompt, seed, randomize_seed, width, height],
|
| 58 |
outputs=[result, seed],
|
| 59 |
api_name="predict"
|
| 60 |
)
|