Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,44 +2,40 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
model_id = "
|
| 7 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id)
|
| 8 |
-
pipe = pipe.to("cpu")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
image = pipe(
|
| 13 |
prompt=prompt,
|
| 14 |
-
negative_prompt=
|
|
|
|
|
|
|
| 15 |
num_inference_steps=steps,
|
| 16 |
-
guidance_scale=
|
| 17 |
-
generator=generator
|
| 18 |
).images[0]
|
| 19 |
return image
|
| 20 |
|
| 21 |
-
# Gradio UI
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
-
gr.Markdown("
|
| 24 |
-
|
| 25 |
-
with gr.Row():
|
| 26 |
-
prompt = gr.Textbox(label="Prompt", placeholder="A fantasy landscape with waterfalls")
|
| 27 |
-
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Blurry, low-resolution")
|
| 28 |
-
|
| 29 |
with gr.Row():
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
with gr.Row():
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
fn=generate_image,
|
| 41 |
-
inputs=[prompt, negative_prompt, steps, guidance_scale, seed],
|
| 42 |
-
outputs=output_image
|
| 43 |
-
)
|
| 44 |
|
| 45 |
demo.launch(show_api=True)
|
|
|
|
| 2 |
import torch
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
|
| 5 |
+
# Load smaller, faster model
|
| 6 |
+
model_id = "stabilityai/stable-diffusion-1-4"
|
| 7 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id).to("cpu")
|
|
|
|
| 8 |
|
| 9 |
+
# Inference function with portrait image size
|
| 10 |
+
def generate(prompt, negative, steps, scale, seed):
|
| 11 |
+
generator = torch.Generator("cpu").manual_seed(seed)
|
| 12 |
image = pipe(
|
| 13 |
prompt=prompt,
|
| 14 |
+
negative_prompt=negative,
|
| 15 |
+
height=768,
|
| 16 |
+
width=512,
|
| 17 |
num_inference_steps=steps,
|
| 18 |
+
guidance_scale=scale,
|
| 19 |
+
generator=generator,
|
| 20 |
).images[0]
|
| 21 |
return image
|
| 22 |
|
| 23 |
+
# Build Gradio UI
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("### 🎨 Stable Diffusion 1.4 (CPU Optimized Portrait Generator)")
|
| 26 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
with gr.Row():
|
| 28 |
+
prompt = gr.Textbox(label="Prompt", placeholder="e.g. A fantasy castle on a cliff")
|
| 29 |
+
negative = gr.Textbox(label="Negative Prompt", placeholder="e.g. low quality, blurry")
|
| 30 |
+
|
|
|
|
| 31 |
with gr.Row():
|
| 32 |
+
steps = gr.Slider(10, 50, value=20, label="Steps")
|
| 33 |
+
scale = gr.Slider(1, 20, value=7.5, step=0.1, label="Guidance Scale")
|
| 34 |
+
seed = gr.Slider(0, 100000, step=1, value=42, label="Seed", randomize=True)
|
| 35 |
|
| 36 |
+
run_btn = gr.Button("🎨 Generate Portrait")
|
| 37 |
+
output = gr.Image(label="Result", type="pil")
|
| 38 |
|
| 39 |
+
run_btn.click(fn=generate, inputs=[prompt, negative, steps, scale, seed], outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
demo.launch(show_api=True)
|