Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,153 +1,169 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
import torch
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
else:
|
| 15 |
-
torch_dtype = torch.float32
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
MAX_IMAGE_SIZE = 1024
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
def infer(
|
| 26 |
-
prompt,
|
| 27 |
-
negative_prompt,
|
| 28 |
-
seed,
|
| 29 |
-
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
-
guidance_scale,
|
| 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 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
]
|
| 59 |
|
| 60 |
-
css =
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
}
|
| 65 |
-
"""
|
| 66 |
-
|
| 67 |
-
with gr.Blocks(css=css) as demo:
|
| 68 |
-
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
-
prompt = gr.
|
| 73 |
-
|
| 74 |
-
show_label=False,
|
| 75 |
-
max_lines=1,
|
| 76 |
-
placeholder="Enter your prompt",
|
| 77 |
-
container=False,
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 85 |
-
negative_prompt = gr.Text(
|
| 86 |
-
label="Negative prompt",
|
| 87 |
-
max_lines=1,
|
| 88 |
-
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=False,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
seed = gr.Slider(
|
| 93 |
-
label="Seed",
|
| 94 |
-
minimum=0,
|
| 95 |
-
maximum=MAX_SEED,
|
| 96 |
-
step=1,
|
| 97 |
-
value=0,
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
with gr.Row():
|
| 103 |
-
width = gr.Slider(
|
| 104 |
-
|
| 105 |
-
minimum=256,
|
| 106 |
-
maximum=MAX_IMAGE_SIZE,
|
| 107 |
-
step=32,
|
| 108 |
-
value=1024, # Replace with defaults that work for your model
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
height = gr.Slider(
|
| 112 |
-
label="Height",
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
with gr.Row():
|
| 120 |
-
|
| 121 |
-
|
| 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 |
-
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
|
| 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 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
# app.py - MangaMorph (Gradio) CPU-friendly template
|
| 2 |
+
import os
|
| 3 |
import random
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
import torch
|
| 8 |
+
from diffusers import DiffusionPipeline
|
| 9 |
+
from diffusers import EulerDiscreteScheduler # scheduler choice
|
| 10 |
|
| 11 |
+
# ---- CONFIG ----
|
| 12 |
+
# Change this model id if you prefer another (see note about license/access above)
|
| 13 |
+
MODEL_ID = os.getenv("MODEL_ID", "hakurei/waifu-diffusion")
|
| 14 |
|
| 15 |
+
# If your model requires a token, set HUGGINGFACE_HUB_TOKEN in Space secrets
|
| 16 |
+
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN", None)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
torch_dtype = torch.float32 if device == "cpu" else torch.float16
|
| 20 |
|
| 21 |
+
# Limits / defaults for CPU-friendly runs
|
| 22 |
+
DEFAULT_WIDTH = 512
|
| 23 |
+
DEFAULT_HEIGHT = 512
|
| 24 |
+
DEFAULT_STEPS = 20
|
| 25 |
+
DEFAULT_GUIDANCE = 7.5
|
| 26 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 27 |
|
| 28 |
+
# Load pipeline (wrapped in try/except so error messages are shown in app log)
|
| 29 |
+
def load_pipeline():
|
| 30 |
+
try:
|
| 31 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(MODEL_ID, subfolder="scheduler") if os.getenv("USE_EULER", "1") == "1" else None
|
| 32 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 33 |
+
MODEL_ID,
|
| 34 |
+
torch_dtype=torch_dtype,
|
| 35 |
+
use_auth_token=HF_TOKEN,
|
| 36 |
+
)
|
| 37 |
+
# attach scheduler only if available and desired
|
| 38 |
+
if isinstance(pipe.scheduler, type(None)) and scheduler is not None:
|
| 39 |
+
pipe.scheduler = scheduler
|
| 40 |
+
pipe = pipe.to(device)
|
| 41 |
+
# For CPU: disable safety checker to avoid long CPU runs (optional)
|
| 42 |
+
try:
|
| 43 |
+
pipe.safety_checker = None
|
| 44 |
+
except Exception:
|
| 45 |
+
pass
|
| 46 |
+
return pipe
|
| 47 |
+
except Exception as e:
|
| 48 |
+
raise RuntimeError(f"Failed to load model '{MODEL_ID}': {e}")
|
| 49 |
+
|
| 50 |
+
# lazy load
|
| 51 |
+
PIPE = None
|
| 52 |
+
def get_pipe():
|
| 53 |
+
global PIPE
|
| 54 |
+
if PIPE is None:
|
| 55 |
+
PIPE = load_pipeline()
|
| 56 |
+
return PIPE
|
| 57 |
+
|
| 58 |
+
# Default negative prompt tuned to reduce common artifacts
|
| 59 |
+
DEFAULT_NEGATIVE_PROMPT = (
|
| 60 |
+
"low quality, bad anatomy, blurry, deformed, extra limbs, mutated hands, "
|
| 61 |
+
"poorly drawn face, watermark, text, signature, lowres, oversaturated"
|
| 62 |
+
)
|
| 63 |
|
|
|
|
| 64 |
def infer(
|
| 65 |
+
prompt: str,
|
| 66 |
+
negative_prompt: str,
|
| 67 |
+
seed: int,
|
| 68 |
+
randomize_seed: bool,
|
| 69 |
+
width: int,
|
| 70 |
+
height: int,
|
| 71 |
+
guidance_scale: float,
|
| 72 |
+
num_inference_steps: int,
|
|
|
|
| 73 |
):
|
| 74 |
+
if not prompt:
|
| 75 |
+
return None, "Please enter a prompt."
|
| 76 |
+
|
| 77 |
if randomize_seed:
|
| 78 |
seed = random.randint(0, MAX_SEED)
|
| 79 |
|
| 80 |
+
gen = torch.Generator(device=device)
|
| 81 |
+
gen = gen.manual_seed(seed)
|
| 82 |
+
|
| 83 |
+
pipe = get_pipe()
|
| 84 |
+
|
| 85 |
+
# Cap size to avoid OOM on CPU
|
| 86 |
+
width = min(width, 768)
|
| 87 |
+
height = min(height, 768)
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
output = pipe(
|
| 91 |
+
prompt=prompt,
|
| 92 |
+
negative_prompt=(negative_prompt or DEFAULT_NEGATIVE_PROMPT),
|
| 93 |
+
width=width,
|
| 94 |
+
height=height,
|
| 95 |
+
guidance_scale=float(guidance_scale),
|
| 96 |
+
num_inference_steps=int(num_inference_steps),
|
| 97 |
+
generator=gen,
|
| 98 |
+
)
|
| 99 |
+
image = output.images[0]
|
| 100 |
+
# simple postprocessing: convert to RGB and return
|
| 101 |
+
if isinstance(image, Image.Image):
|
| 102 |
+
image = image.convert("RGB")
|
| 103 |
+
return image, f"Seed: {seed}"
|
| 104 |
+
except Exception as e:
|
| 105 |
+
# retry logic: try again with smaller steps/guidance if CPU fails
|
| 106 |
+
try:
|
| 107 |
+
output = pipe(
|
| 108 |
+
prompt=prompt,
|
| 109 |
+
negative_prompt=(negative_prompt or DEFAULT_NEGATIVE_PROMPT),
|
| 110 |
+
width=width,
|
| 111 |
+
height=height,
|
| 112 |
+
guidance_scale=max(3.0, float(guidance_scale) - 1.0),
|
| 113 |
+
num_inference_steps=max(5, int(num_inference_steps) - 5),
|
| 114 |
+
generator=gen,
|
| 115 |
+
)
|
| 116 |
+
image = output.images[0]
|
| 117 |
+
if isinstance(image, Image.Image):
|
| 118 |
+
image = image.convert("RGB")
|
| 119 |
+
return image, f"Recovered (retry) — Seed: {seed}"
|
| 120 |
+
except Exception as e2:
|
| 121 |
+
return None, f"Generation failed: {e2}"
|
| 122 |
+
|
| 123 |
+
# ---- UI ----
|
| 124 |
+
css = """
|
| 125 |
+
#main { max-width: 880px; margin: auto; }
|
| 126 |
+
.header { text-align: center; }
|
| 127 |
+
.small { font-size: 0.9rem; color: #666; }
|
| 128 |
+
"""
|
| 129 |
|
| 130 |
examples = [
|
| 131 |
+
"A young anime girl standing in a rain-soaked neon street, detailed lighting, cinematic",
|
| 132 |
+
"A samurai in traditional armor on a cliff at sunset, dramatic lighting, anime style",
|
| 133 |
+
"Cozy room with anime character by window reading, soft warm light"
|
| 134 |
]
|
| 135 |
|
| 136 |
+
with gr.Blocks(css=css, theme=gr.themes.Default()) as demo:
|
| 137 |
+
with gr.Column(elem_id="main"):
|
| 138 |
+
gr.Markdown("<div class='header'><h2>MangaMorph — Anime Scene Generator</h2>"
|
| 139 |
+
"<div class='small'>Text → Anime image | CPU-optimized | Use Model ID or set HF token in Secrets</div></div>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Describe the anime scene you want...", lines=2)
|
| 143 |
+
run_btn = gr.Button("Generate", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
with gr.Row():
|
| 146 |
+
gallery = gr.Image(label="Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
with gr.Accordion("Advanced settings", open=False):
|
| 149 |
+
negative = gr.Textbox(label="Negative prompt (optional)", placeholder=DEFAULT_NEGATIVE_PROMPT, lines=2, value=DEFAULT_NEGATIVE_PROMPT)
|
| 150 |
+
seed = gr.Number(label="Seed (0 = randomize)", value=0)
|
| 151 |
+
randomize = gr.Checkbox(label="Randomize seed", value=True)
|
| 152 |
with gr.Row():
|
| 153 |
+
width = gr.Slider(label="Width", minimum=256, maximum=768, step=64, value=DEFAULT_WIDTH)
|
| 154 |
+
height = gr.Slider(label="Height", minimum=256, maximum=768, step=64, value=DEFAULT_HEIGHT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
with gr.Row():
|
| 156 |
+
guidance = gr.Slider(label="Guidance scale", minimum=1.0, maximum=15.0, step=0.1, value=DEFAULT_GUIDANCE)
|
| 157 |
+
steps = gr.Slider(label="Steps", minimum=5, maximum=50, step=1, value=DEFAULT_STEPS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 160 |
+
|
| 161 |
+
status = gr.Textbox(label="Status / Seed", interactive=False)
|
| 162 |
+
|
| 163 |
+
run_btn.click(
|
| 164 |
fn=infer,
|
| 165 |
+
inputs=[prompt, negative, seed, randomize, width, height, guidance, steps],
|
| 166 |
+
outputs=[gallery, status],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|