Update app.py
Browse files
app.py
CHANGED
|
@@ -15,11 +15,12 @@ def get_torch_dtype():
|
|
| 15 |
def get_device():
|
| 16 |
return "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
|
| 18 |
-
# ===
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
pipe = DiffusionPipeline.from_pretrained(MODEL_REPO_ID, torch_dtype=
|
| 22 |
-
pipe = pipe
|
|
|
|
| 23 |
|
| 24 |
# === Define custom prompt builder ===
|
| 25 |
def build_prompt(word):
|
|
@@ -35,15 +36,16 @@ def build_prompt(word):
|
|
| 35 |
# === Image generation function ===
|
| 36 |
def generate_image(prompt, negative_prompt, guidance_scale, num_inference_steps, width, height, seed):
|
| 37 |
generator = torch.Generator().manual_seed(seed)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
# === Inference wrapper ===
|
| 49 |
def infer(
|
|
@@ -103,7 +105,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=3.5)
|
| 106 |
-
num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=50, step=1, value=
|
| 107 |
|
| 108 |
run_button.click(
|
| 109 |
fn=infer,
|
|
|
|
| 15 |
def get_device():
|
| 16 |
return "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
|
| 18 |
+
# === Lazy load the diffusion model ===
|
| 19 |
+
def get_pipe():
|
| 20 |
+
if not hasattr(get_pipe, "pipe"):
|
| 21 |
+
pipe = DiffusionPipeline.from_pretrained(MODEL_REPO_ID, torch_dtype=get_torch_dtype()).to(get_device())
|
| 22 |
+
get_pipe.pipe = pipe
|
| 23 |
+
return get_pipe.pipe
|
| 24 |
|
| 25 |
# === Define custom prompt builder ===
|
| 26 |
def build_prompt(word):
|
|
|
|
| 36 |
# === Image generation function ===
|
| 37 |
def generate_image(prompt, negative_prompt, guidance_scale, num_inference_steps, width, height, seed):
|
| 38 |
generator = torch.Generator().manual_seed(seed)
|
| 39 |
+
with torch.inference_mode():
|
| 40 |
+
return get_pipe()(
|
| 41 |
+
prompt=prompt,
|
| 42 |
+
negative_prompt=negative_prompt,
|
| 43 |
+
guidance_scale=guidance_scale,
|
| 44 |
+
num_inference_steps=num_inference_steps,
|
| 45 |
+
width=width,
|
| 46 |
+
height=height,
|
| 47 |
+
generator=generator,
|
| 48 |
+
).images[0]
|
| 49 |
|
| 50 |
# === Inference wrapper ===
|
| 51 |
def infer(
|
|
|
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=3.5)
|
| 108 |
+
num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=50, step=1, value=4)
|
| 109 |
|
| 110 |
run_button.click(
|
| 111 |
fn=infer,
|