Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,75 @@
|
|
| 1 |
-
from diffusers import AutoPipelineForImage2Image, AutoPipelineForText2Image
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
import numpy as np
|
| 6 |
-
import gradio as gr
|
| 7 |
import time
|
| 8 |
import math
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
print(f"Running on DEVICE: {device}")
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"torch_dtype": torch_dtype,
|
| 22 |
-
"safety_checker": None if os.environ.get("SAFETY_CHECKER") != "True" else None,
|
| 23 |
-
"use_safetensors": True
|
| 24 |
-
}
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
| 29 |
-
t2i_pipe = AutoPipelineForText2Image.from_pretrained(
|
| 30 |
-
"stabilityai/sdxl-turbo", **pipe_kwargs
|
| 31 |
-
)
|
| 32 |
|
| 33 |
-
# OPTIMIZATION FOR 16GB RAM
|
| 34 |
-
# enables slicing the attention computation into steps to save memory
|
| 35 |
i2i_pipe.enable_attention_slicing()
|
| 36 |
t2i_pipe.enable_attention_slicing()
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
i2i_pipe.to("cpu")
|
| 40 |
-
t2i_pipe.to("cpu")
|
| 41 |
-
|
| 42 |
-
i2i_pipe.set_progress_bar_config(disable=False) # Enabled so you can see it's working
|
| 43 |
-
t2i_pipe.set_progress_bar_config(disable=False)
|
| 44 |
-
|
| 45 |
-
def resize_crop(image, size=512):
|
| 46 |
-
image = image.convert("RGB")
|
| 47 |
-
w, h = image.size
|
| 48 |
-
image = image.resize((size, int(size * (h / w))), Image.BICUBIC)
|
| 49 |
-
return image
|
| 50 |
-
|
| 51 |
-
async def predict(init_image, prompt, strength, steps, seed=1231231):
|
| 52 |
generator = torch.manual_seed(seed)
|
| 53 |
-
last_time = time.time()
|
| 54 |
-
|
| 55 |
-
# Ensure steps are sufficient for Turbo
|
| 56 |
-
if int(steps * strength) < 1:
|
| 57 |
-
steps = math.ceil(1 / max(0.10, strength))
|
| 58 |
-
|
| 59 |
if init_image is not None:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
guidance_scale=0.0,
|
| 67 |
-
strength=strength,
|
| 68 |
-
width=512,
|
| 69 |
-
height=512,
|
| 70 |
-
output_type="pil",
|
| 71 |
-
)
|
| 72 |
else:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
generator=generator,
|
| 76 |
-
num_inference_steps=int(steps),
|
| 77 |
-
guidance_scale=0.0,
|
| 78 |
-
width=512,
|
| 79 |
-
height=512,
|
| 80 |
-
output_type="pil",
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
print(f"Inference took {time.time() - last_time:.2f} seconds")
|
| 84 |
-
return results.images[0]
|
| 85 |
|
| 86 |
-
# ---
|
| 87 |
-
|
| 88 |
-
with gr.Blocks(css=css) as demo:
|
| 89 |
-
# ... (Keep your UI layout exactly the same)
|
| 90 |
-
# Ensure the button calls the predict function
|
| 91 |
-
# Note: Removed the automatic 'change' triggers to prevent CPU freezing
|
| 92 |
-
# while typing. Better to use the Generate button only.
|
| 93 |
-
|
| 94 |
-
init_image_state = gr.State()
|
| 95 |
with gr.Column(elem_id="container"):
|
| 96 |
-
gr.Markdown("# SDXL Turbo CPU
|
| 97 |
-
with gr.Row():
|
| 98 |
-
prompt = gr.Textbox(placeholder="Prompt...", scale=5)
|
| 99 |
-
generate_bt = gr.Button("Generate", scale=1)
|
| 100 |
|
|
|
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column():
|
| 103 |
-
|
| 104 |
with gr.Column():
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
generate_bt.click(fn=predict, inputs=inputs, outputs=image_output)
|
| 114 |
|
| 115 |
-
demo.queue()
|
| 116 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import os
|
| 3 |
+
import psutil
|
|
|
|
|
|
|
| 4 |
import time
|
| 5 |
import math
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from diffusers import AutoPipelineForImage2Image, AutoPipelineForText2Image
|
| 9 |
|
| 10 |
+
# --- System Resource Logic ---
|
| 11 |
+
def get_system_usage():
|
| 12 |
+
cpu = psutil.cpu_percent(interval=None)
|
| 13 |
+
ram = psutil.virtual_memory().percent
|
| 14 |
+
# Returns values for the progress bars
|
| 15 |
+
return cpu / 100, ram / 100, f"CPU: {cpu}% | RAM: {ram}%"
|
|
|
|
| 16 |
|
| 17 |
+
# --- Model Loading (Optimized for 16GB) ---
|
| 18 |
+
device = "cpu"
|
| 19 |
+
torch_dtype = torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
pipe_kwargs = {"torch_dtype": torch_dtype, "use_safetensors": True}
|
| 22 |
+
i2i_pipe = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", **pipe_kwargs)
|
| 23 |
+
t2i_pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo", **pipe_kwargs)
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
| 25 |
i2i_pipe.enable_attention_slicing()
|
| 26 |
t2i_pipe.enable_attention_slicing()
|
| 27 |
+
i2i_pipe.enable_vae_tiling()
|
| 28 |
+
t2i_pipe.enable_vae_tiling()
|
| 29 |
|
| 30 |
+
def predict(init_image, prompt, strength, steps, seed):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
generator = torch.manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if init_image is not None:
|
| 33 |
+
# Simple square crop for better results
|
| 34 |
+
w, h = init_image.size
|
| 35 |
+
s = min(w, h)
|
| 36 |
+
init_image = init_image.crop(((w-s)//2, (h-s)//2, (w+s)//2, (h+s)//2)).resize((512, 512))
|
| 37 |
+
return i2i_pipe(prompt=prompt, image=init_image, generator=generator, num_inference_steps=int(steps),
|
| 38 |
+
guidance_scale=0.0, strength=strength).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
+
return t2i_pipe(prompt=prompt, generator=generator, num_inference_steps=int(steps),
|
| 41 |
+
guidance_scale=0.0, width=512, height=512).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# --- UI Construction ---
|
| 44 |
+
with gr.Blocks(css="#container{ max-width: 60rem; margin: 0 auto; }") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
with gr.Column(elem_id="container"):
|
| 46 |
+
gr.Markdown("## 🚀 SDXL Turbo CPU + System Monitor")
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# System Usage Dashboard
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
| 51 |
+
cpu_bar = gr.Label(label="System Status") # Text label for quick reading
|
| 52 |
with gr.Column():
|
| 53 |
+
cpu_plot = gr.Slider(label="CPU Load", interactive=False)
|
| 54 |
+
ram_plot = gr.Slider(label="RAM Usage", interactive=False)
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
prompt = gr.Textbox(placeholder="A cinematic cat...", label="Prompt", scale=4)
|
| 58 |
+
generate_bt = gr.Button("Generate", variant="primary", scale=1)
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
image_input = gr.Image(type="pil", label="Input (i2i)")
|
| 62 |
+
image_output = gr.Image(label="Result")
|
| 63 |
+
|
| 64 |
+
with gr.Accordion("Settings", open=False):
|
| 65 |
+
strength = gr.Slider(0.0, 1.0, 0.7, label="Strength")
|
| 66 |
+
steps = gr.Slider(1, 4, 2, step=1, label="Steps")
|
| 67 |
+
seed = gr.Number(42, label="Seed")
|
| 68 |
+
|
| 69 |
+
# Auto-update logic (Runs every 2 seconds)
|
| 70 |
+
monitor_timer = gr.Timer(2)
|
| 71 |
+
monitor_timer.tick(get_system_usage, outputs=[cpu_plot, ram_plot, cpu_bar])
|
| 72 |
|
| 73 |
+
generate_bt.click(predict, [image_input, prompt, strength, steps, seed], image_output)
|
|
|
|
| 74 |
|
|
|
|
| 75 |
demo.launch()
|