Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,141 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
num_inference_steps,
|
| 34 |
-
progress=gr.Progress(track_tqdm=True),
|
| 35 |
-
):
|
| 36 |
-
if randomize_seed:
|
| 37 |
-
seed = random.randint(0, MAX_SEED)
|
| 38 |
-
|
| 39 |
-
generator = torch.Generator().manual_seed(seed)
|
| 40 |
-
|
| 41 |
-
image = pipe(
|
| 42 |
-
prompt=prompt,
|
| 43 |
-
negative_prompt=negative_prompt,
|
| 44 |
-
guidance_scale=guidance_scale,
|
| 45 |
-
num_inference_steps=num_inference_steps,
|
| 46 |
-
width=width,
|
| 47 |
-
height=height,
|
| 48 |
-
generator=generator,
|
| 49 |
-
).images[0]
|
| 50 |
-
|
| 51 |
-
return image, seed
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
examples = [
|
| 55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 56 |
-
"An astronaut riding a green horse",
|
| 57 |
-
"A delicious ceviche cheesecake slice",
|
| 58 |
-
]
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
guidance_scale = gr.Slider(
|
| 121 |
-
label="Guidance scale",
|
| 122 |
-
minimum=0.0,
|
| 123 |
-
maximum=10.0,
|
| 124 |
-
step=0.1,
|
| 125 |
-
value=0.0, # Replace with defaults that work for your model
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
num_inference_steps = gr.Slider(
|
| 129 |
-
label="Number of inference steps",
|
| 130 |
-
minimum=1,
|
| 131 |
-
maximum=50,
|
| 132 |
-
step=1,
|
| 133 |
-
value=2, # Replace with defaults that work for your model
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
-
gr.on(
|
| 138 |
-
triggers=[run_button.click, prompt.submit],
|
| 139 |
-
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
prompt,
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch, random
|
| 3 |
+
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, StableDiffusionXLInpaintPipeline, StableDiffusionLatentUpscalePipeline
|
| 4 |
|
| 5 |
+
# ---------------------------
|
| 6 |
+
# HARD CHOICES (donโt change)
|
| 7 |
+
# ---------------------------
|
| 8 |
+
BASE_MODEL = "Lykon/dreamshaper-xl-1-0" # High-quality SDXL finetune
|
| 9 |
+
UPSCALE_MODEL = "stabilityai/sdxl-upscaler" # x4 upscaler
|
| 10 |
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 13 |
+
|
| 14 |
+
# Load base text2img pipeline
|
| 15 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 16 |
+
BASE_MODEL, torch_dtype=dtype, use_safetensors=True
|
| 17 |
+
)
|
| 18 |
+
pipe.to(device)
|
| 19 |
+
|
| 20 |
+
# Load upscaler (x4)
|
| 21 |
+
upscale_pipe = StableDiffusionLatentUpscalePipeline.from_pretrained(
|
| 22 |
+
UPSCALE_MODEL, torch_dtype=dtype, use_safetensors=True
|
| 23 |
+
).to(device)
|
| 24 |
+
|
| 25 |
+
# ---------------------------
|
| 26 |
+
# Presets & helpers
|
| 27 |
+
# ---------------------------
|
| 28 |
+
ASPECTS = {
|
| 29 |
+
"1:1 (Square | 4096x4096)": (4096, 4096),
|
| 30 |
+
"16:9 (UHD | 3840x2160)": (3840, 2160),
|
| 31 |
+
"9:16 (Phone Tall | 2160x3840)": (2160, 3840), # Zedge/phone
|
| 32 |
+
"4:5 (Portrait | 4000x5000)": (4000, 5000), # Adobe Stock favorite
|
| 33 |
+
"3:4 (Poster | 3000x4000)": (3000, 4000),
|
| 34 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
STYLE_PRESETS = {
|
| 37 |
+
"Realistic Photo": {
|
| 38 |
+
"pos": "ultra-detailed realistic photo, natural lighting, sharp focus, DSLR depth of field, film grain subtle",
|
| 39 |
+
"neg": "blurry, lowres, artifact, extra fingers, deformed, watermark, text, logo, signature"
|
| 40 |
+
},
|
| 41 |
+
"3D Render": {
|
| 42 |
+
"pos": "high poly 3D render, global illumination, octane style, cinematic light, clean materials",
|
| 43 |
+
"neg": "lowpoly, noisy, aliased, bad shading, watermark, text, logo"
|
| 44 |
+
},
|
| 45 |
+
"Flat Illustration": {
|
| 46 |
+
"pos": "flat vector illustration, minimal shapes, smooth gradients, simple background, clean lines",
|
| 47 |
+
"neg": "noise, gradients banding, messy, sketchy, text"
|
| 48 |
+
},
|
| 49 |
+
"Minimalist": {
|
| 50 |
+
"pos": "minimalist composition, lots of negative space, clean background, simple geometric forms",
|
| 51 |
+
"neg": "cluttered, busy, noisy, text"
|
| 52 |
+
},
|
| 53 |
+
"Neon Wallpaper": {
|
| 54 |
+
"pos": "neon glow, vibrant colors, high contrast, dark background, crisp edges, perfect symmetry",
|
| 55 |
+
"neg": "dull, washed out, blurry, text, watermark"
|
| 56 |
+
}
|
| 57 |
}
|
| 58 |
+
|
| 59 |
+
def seed_or_random(seed):
|
| 60 |
+
if seed in [None, "", 0]:
|
| 61 |
+
return random.randint(1, 2**31 - 1)
|
| 62 |
+
return int(seed)
|
| 63 |
+
|
| 64 |
+
def generate(prompt, style, aspect_key, steps, guidance, batch, seed, add_negatives):
|
| 65 |
+
if not prompt or len(prompt.strip()) < 3:
|
| 66 |
+
return [None]*batch, f"โ Please enter a prompt."
|
| 67 |
+
w, h = ASPECTS[aspect_key]
|
| 68 |
+
s = seed_or_random(seed)
|
| 69 |
+
pos = prompt
|
| 70 |
+
neg = ""
|
| 71 |
+
|
| 72 |
+
# apply style preset
|
| 73 |
+
if style in STYLE_PRESETS:
|
| 74 |
+
pos = f"{STYLE_PRESETS[style]['pos']}, {prompt}"
|
| 75 |
+
if add_negatives:
|
| 76 |
+
neg = STYLE_PRESETS[style]['neg']
|
| 77 |
+
|
| 78 |
+
generator = torch.Generator(device=device).manual_seed(s)
|
| 79 |
+
|
| 80 |
+
images = pipe(
|
| 81 |
+
prompt=pos,
|
| 82 |
+
negative_prompt=neg,
|
| 83 |
+
guidance_scale=float(guidance),
|
| 84 |
+
num_inference_steps=int(steps),
|
| 85 |
+
width=int(w),
|
| 86 |
+
height=int(h),
|
| 87 |
+
num_images_per_prompt=int(batch),
|
| 88 |
+
generator=generator
|
| 89 |
+
).images
|
| 90 |
+
|
| 91 |
+
# Simple quality guard: remove any None
|
| 92 |
+
images = [im for im in images if im is not None]
|
| 93 |
+
return images, f"โ
Done. Seed: {s} | {aspect_key} | {style}"
|
| 94 |
+
|
| 95 |
+
def upscale(image, steps_up, guidance_up):
|
| 96 |
+
if image is None:
|
| 97 |
+
return None, "โ Generate an image first."
|
| 98 |
+
# Latent upscaler expects an image and prompt; prompt can be empty for neutral upscale
|
| 99 |
+
result = upscale_pipe(
|
| 100 |
+
prompt="high quality, detailed, clean edges, no artifacts",
|
| 101 |
+
image=image,
|
| 102 |
+
guidance_scale=float(guidance_up),
|
| 103 |
+
num_inference_steps=int(steps_up)
|
| 104 |
+
).images[0]
|
| 105 |
+
return result, "โ
Upscaled x4"
|
| 106 |
+
|
| 107 |
+
with gr.Blocks(title="My Personal MidJourney-Style Tool") as demo:
|
| 108 |
+
gr.Markdown(
|
| 109 |
+
"# ๐ฎ Personal MidJourney-Style Tool\n"
|
| 110 |
+
"Optimized for **Freepik / Adobe Stock / Zedge**.\n"
|
| 111 |
+
"- Choose a **style** โ set **aspect ratio** โ **Generate** (x4 grid) โ **Upscale x4** for stock.\n"
|
| 112 |
+
"- Tip: Use descriptive nouns, materials, lighting, and mood."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column(scale=1):
|
| 117 |
+
prompt = gr.Textbox(label="Prompt (describe what you want)", placeholder="e.g., futuristic city skyline at dusk, reflective water, birds, cinematic")
|
| 118 |
+
style = gr.Dropdown(choices=list(STYLE_PRESETS.keys()), value="Realistic Photo", label="Style preset")
|
| 119 |
+
aspect = gr.Dropdown(choices=list(ASPECTS.keys()), value="16:9 (UHD | 3840x2160)", label="Aspect Ratio")
|
| 120 |
+
steps = gr.Slider(5, 50, value=28, step=1, label="Steps (quality โ but slower)")
|
| 121 |
+
guidance = gr.Slider(1.0, 12.0, value=6.5, step=0.5, label="Prompt Strength (CFG)")
|
| 122 |
+
batch = gr.Slider(1, 4, value=4, step=1, label="Batch (number of images)")
|
| 123 |
+
seed = gr.Number(value=0, label="Seed (0 = random)")
|
| 124 |
+
add_negs = gr.Checkbox(value=True, label="Add smart negative prompts")
|
| 125 |
+
gen_btn = gr.Button("๐ Generate")
|
| 126 |
+
|
| 127 |
+
with gr.Column(scale=1):
|
| 128 |
+
gallery = gr.Gallery(label="Results", show_label=True, columns=2, rows=2, height=600)
|
| 129 |
+
status = gr.Markdown()
|
| 130 |
+
|
| 131 |
+
gr.Markdown("### Upscale (pick the best image in gallery and paste below)")
|
| 132 |
+
to_upscale = gr.Image(type="pil", label="Image to upscale (paste or upload one of the results)")
|
| 133 |
+
steps_up = gr.Slider(5, 30, value=15, step=1, label="Upscale Steps")
|
| 134 |
+
guidance_up = gr.Slider(1.0, 10.0, value=5.0, step=0.5, label="Upscale Guidance")
|
| 135 |
+
up_btn = gr.Button("โฌ๏ธ Upscale x4")
|
| 136 |
+
up_out = gr.Image(type="pil", label="Upscaled Output")
|
| 137 |
+
|
| 138 |
+
gen_btn.click(generate, [prompt, style, aspect, steps, guidance, batch, seed, add_negs], [gallery, status])
|
| 139 |
+
up_btn.click(upscale, [to_upscale, steps_up, guidance_up], [up_out, status])
|
| 140 |
+
|
| 141 |
+
demo.launch()
|