Spaces:
Running
on
Zero
Running
on
Zero
Upload 7 files
Browse files- app.py +3 -40
- config.py +3 -6
- generator.py +8 -33
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
-
# --- 4. Import GC for memory management ---
|
| 5 |
-
import gc
|
| 6 |
from model import ModelHandler
|
| 7 |
from generator import Generator
|
| 8 |
# --- IMPORT CONFIG ---
|
|
@@ -24,11 +22,7 @@ def process_img(
|
|
| 24 |
steps,
|
| 25 |
img_strength,
|
| 26 |
depth_strength,
|
| 27 |
-
edge_strength
|
| 28 |
-
# --- 2. Add negative prompt ---
|
| 29 |
-
negative_prompt,
|
| 30 |
-
# --- 3. Add face likeness ---
|
| 31 |
-
face_likeness
|
| 32 |
):
|
| 33 |
if image is None:
|
| 34 |
raise gr.Error("Please upload an image first.")
|
|
@@ -43,20 +37,9 @@ def process_img(
|
|
| 43 |
num_inference_steps=steps,
|
| 44 |
img2img_strength=img_strength,
|
| 45 |
depth_strength=depth_strength,
|
| 46 |
-
lineart_strength=edge_strength
|
| 47 |
-
# --- 2. Pass negative prompt ---
|
| 48 |
-
negative_prompt=negative_prompt,
|
| 49 |
-
# --- 3. Pass face likeness ---
|
| 50 |
-
face_likeness=face_likeness
|
| 51 |
)
|
| 52 |
print("--- Generation Complete ---")
|
| 53 |
-
|
| 54 |
-
# --- 4. Add memory optimization ---
|
| 55 |
-
print("Cleaning up memory...")
|
| 56 |
-
gc.collect()
|
| 57 |
-
torch.cuda.empty_cache()
|
| 58 |
-
print("Cleanup complete.")
|
| 59 |
-
|
| 60 |
return result
|
| 61 |
|
| 62 |
except Exception as e:
|
|
@@ -81,13 +64,6 @@ with gr.Blocks(title="Face To Pixel Art", theme=gr.themes.Soft()) as demo:
|
|
| 81 |
info="The trigger words 'p1x3l4rt, pixel art' are added automatically."
|
| 82 |
)
|
| 83 |
|
| 84 |
-
# --- 2. Add Negative Prompt Textbox ---
|
| 85 |
-
negative_prompt = gr.Textbox(
|
| 86 |
-
label="Negative Prompt",
|
| 87 |
-
value=Config.NEGATIVE_PROMPT,
|
| 88 |
-
info="What to avoid generating."
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
# --- MOVED ACCORDION HERE ---
|
| 92 |
with gr.Accordion("Advanced Settings", open=False):
|
| 93 |
cfg_scale = gr.Slider(
|
|
@@ -135,16 +111,6 @@ with gr.Blocks(title="Face To Pixel Art", theme=gr.themes.Soft()) as demo:
|
|
| 135 |
value=Config.EDGE_STRENGTH,
|
| 136 |
label="EdgeMap Strength (LineArt)"
|
| 137 |
)
|
| 138 |
-
|
| 139 |
-
# --- 3. Add Face Likeness Slider ---
|
| 140 |
-
face_likeness = gr.Slider(
|
| 141 |
-
elem_id="face_likeness",
|
| 142 |
-
minimum=0.0,
|
| 143 |
-
maximum=1.0,
|
| 144 |
-
step=0.05,
|
| 145 |
-
value=Config.FACE_LIKENESS_STRENGTH,
|
| 146 |
-
label="Face Likeness (InstantID)"
|
| 147 |
-
)
|
| 148 |
# --- END OF MOVED BLOCK ---
|
| 149 |
|
| 150 |
run_btn = gr.Button("Generate Pixel Art", variant="primary")
|
|
@@ -165,10 +131,7 @@ with gr.Blocks(title="Face To Pixel Art", theme=gr.themes.Soft()) as demo:
|
|
| 165 |
steps,
|
| 166 |
img_strength,
|
| 167 |
depth_strength,
|
| 168 |
-
edge_strength
|
| 169 |
-
# --- 2 & 3. Add new inputs ---
|
| 170 |
-
negative_prompt,
|
| 171 |
-
face_likeness
|
| 172 |
]
|
| 173 |
|
| 174 |
run_btn.click(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
from model import ModelHandler
|
| 5 |
from generator import Generator
|
| 6 |
# --- IMPORT CONFIG ---
|
|
|
|
| 22 |
steps,
|
| 23 |
img_strength,
|
| 24 |
depth_strength,
|
| 25 |
+
edge_strength
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
):
|
| 27 |
if image is None:
|
| 28 |
raise gr.Error("Please upload an image first.")
|
|
|
|
| 37 |
num_inference_steps=steps,
|
| 38 |
img2img_strength=img_strength,
|
| 39 |
depth_strength=depth_strength,
|
| 40 |
+
lineart_strength=edge_strength
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
print("--- Generation Complete ---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return result
|
| 44 |
|
| 45 |
except Exception as e:
|
|
|
|
| 64 |
info="The trigger words 'p1x3l4rt, pixel art' are added automatically."
|
| 65 |
)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# --- MOVED ACCORDION HERE ---
|
| 68 |
with gr.Accordion("Advanced Settings", open=False):
|
| 69 |
cfg_scale = gr.Slider(
|
|
|
|
| 111 |
value=Config.EDGE_STRENGTH,
|
| 112 |
label="EdgeMap Strength (LineArt)"
|
| 113 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
# --- END OF MOVED BLOCK ---
|
| 115 |
|
| 116 |
run_btn = gr.Button("Generate Pixel Art", variant="primary")
|
|
|
|
| 131 |
steps,
|
| 132 |
img_strength,
|
| 133 |
depth_strength,
|
| 134 |
+
edge_strength
|
|
|
|
|
|
|
|
|
|
| 135 |
]
|
| 136 |
|
| 137 |
run_btn.click(
|
config.py
CHANGED
|
@@ -9,7 +9,7 @@ class Config:
|
|
| 9 |
REPO_ID = "primerz/pixagram"
|
| 10 |
CHECKPOINT_FILENAME = "horizon.safetensors"
|
| 11 |
LORA_FILENAME = "retroart.safetensors"
|
| 12 |
-
LORA_STRENGTH = 1.
|
| 13 |
|
| 14 |
# Trigger Words for the LoRA
|
| 15 |
STYLE_TRIGGER = "p1x3l4rt, pixel art"
|
|
@@ -36,8 +36,5 @@ class Config:
|
|
| 36 |
CGF_SCALE = 2.4
|
| 37 |
STEPS_NUMBER = 8
|
| 38 |
IMG_STRENGTH = 0.8
|
| 39 |
-
DEPTH_STRENGTH = 0.
|
| 40 |
-
EDGE_STRENGTH = 0.6
|
| 41 |
-
FACE_LIKENESS_STRENGTH = 0.8
|
| 42 |
-
CLIP_SKIP = 2
|
| 43 |
-
NEGATIVE_PROMPT = "Photography, Ugly, Blurry, Disformed, Artifacts, Wrong colors, Wrong, Bad, Worse."
|
|
|
|
| 9 |
REPO_ID = "primerz/pixagram"
|
| 10 |
CHECKPOINT_FILENAME = "horizon.safetensors"
|
| 11 |
LORA_FILENAME = "retroart.safetensors"
|
| 12 |
+
LORA_STRENGTH = 1.25 # Fixed strength for fusion
|
| 13 |
|
| 14 |
# Trigger Words for the LoRA
|
| 15 |
STYLE_TRIGGER = "p1x3l4rt, pixel art"
|
|
|
|
| 36 |
CGF_SCALE = 2.4
|
| 37 |
STEPS_NUMBER = 8
|
| 38 |
IMG_STRENGTH = 0.8
|
| 39 |
+
DEPTH_STRENGTH = 0.9
|
| 40 |
+
EDGE_STRENGTH = 0.6
|
|
|
|
|
|
|
|
|
generator.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
import torch
|
| 2 |
-
# --- 4. Import GC for memory management ---
|
| 3 |
-
import gc
|
| 4 |
from config import Config
|
| 5 |
from utils import resize_image_to_1mp, get_caption
|
| 6 |
from PIL import Image
|
|
@@ -28,11 +26,6 @@ class Generator:
|
|
| 28 |
depth_map = depth_map_raw.resize((width, height), Image.LANCZOS)
|
| 29 |
lineart_map = lineart_map_raw.resize((width, height), Image.LANCZOS)
|
| 30 |
|
| 31 |
-
# --- 4. Add memory optimization ---
|
| 32 |
-
del depth_map_raw
|
| 33 |
-
del lineart_map_raw
|
| 34 |
-
gc.collect()
|
| 35 |
-
|
| 36 |
return depth_map, lineart_map
|
| 37 |
|
| 38 |
def predict(
|
|
@@ -43,11 +36,7 @@ class Generator:
|
|
| 43 |
num_inference_steps=6,
|
| 44 |
img2img_strength=0.3,
|
| 45 |
depth_strength=0.3,
|
| 46 |
-
lineart_strength=0.3
|
| 47 |
-
# --- 2. Add negative prompt ---
|
| 48 |
-
negative_prompt="",
|
| 49 |
-
# --- 3. Add face likeness ---
|
| 50 |
-
face_likeness=0.7
|
| 51 |
):
|
| 52 |
# 1. Pre-process Inputs
|
| 53 |
print("Processing Input...")
|
|
@@ -78,20 +67,16 @@ class Generator:
|
|
| 78 |
# ControlNet order: [InstantID, Zoe, LineArt]
|
| 79 |
if face_emb is not None:
|
| 80 |
print("Face detected: Applying InstantID.")
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
self.mh.pipeline.set_ip_adapter_scale(ip_adapter_scale)
|
| 86 |
-
controlnet_conditioning_scale = [cn_scale_instantid, depth_strength, lineart_strength]
|
| 87 |
-
# --- End 3 ---
|
| 88 |
-
|
| 89 |
-
control_guidance_end = [0.3, 0.6, 0.6] # Stop InstantID early (preserves style)
|
| 90 |
else:
|
| 91 |
print("No face detected: Disabling InstantID.")
|
| 92 |
-
|
| 93 |
controlnet_conditioning_scale = [0.0, depth_strength, lineart_strength]
|
| 94 |
control_guidance_end = [0.3, 0.6, 0.6]
|
|
|
|
| 95 |
|
| 96 |
# --- START FIX for NoneType Error ---
|
| 97 |
# Create a dummy tensor instead of passing None
|
|
@@ -103,8 +88,6 @@ class Generator:
|
|
| 103 |
print("Running pipeline...")
|
| 104 |
result = self.mh.pipeline(
|
| 105 |
prompt=final_prompt,
|
| 106 |
-
# --- 2. Pass negative prompt ---
|
| 107 |
-
negative_prompt=negative_prompt,
|
| 108 |
image=processed_image, # Base image for Img2Img
|
| 109 |
control_image=[processed_image, depth_map, lineart_map], # ControlNet inputs
|
| 110 |
image_embeds=face_emb, # Face embedding (or dummy)
|
|
@@ -118,8 +101,7 @@ class Generator:
|
|
| 118 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 119 |
control_guidance_end=control_guidance_end,
|
| 120 |
|
| 121 |
-
|
| 122 |
-
clip_skip=Config.CLIP_SKIP,
|
| 123 |
|
| 124 |
# --- LoRA Strength REMOVED ---
|
| 125 |
# No longer needed, as LoRA is fused into the model weights
|
|
@@ -127,11 +109,4 @@ class Generator:
|
|
| 127 |
|
| 128 |
).images[0]
|
| 129 |
|
| 130 |
-
# --- 4. Add memory optimization ---
|
| 131 |
-
del face_emb
|
| 132 |
-
del depth_map
|
| 133 |
-
del lineart_map
|
| 134 |
-
del processed_image
|
| 135 |
-
gc.collect()
|
| 136 |
-
|
| 137 |
return result
|
|
|
|
| 1 |
import torch
|
|
|
|
|
|
|
| 2 |
from config import Config
|
| 3 |
from utils import resize_image_to_1mp, get_caption
|
| 4 |
from PIL import Image
|
|
|
|
| 26 |
depth_map = depth_map_raw.resize((width, height), Image.LANCZOS)
|
| 27 |
lineart_map = lineart_map_raw.resize((width, height), Image.LANCZOS)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return depth_map, lineart_map
|
| 30 |
|
| 31 |
def predict(
|
|
|
|
| 36 |
num_inference_steps=6,
|
| 37 |
img2img_strength=0.3,
|
| 38 |
depth_strength=0.3,
|
| 39 |
+
lineart_strength=0.3
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
):
|
| 41 |
# 1. Pre-process Inputs
|
| 42 |
print("Processing Input...")
|
|
|
|
| 67 |
# ControlNet order: [InstantID, Zoe, LineArt]
|
| 68 |
if face_emb is not None:
|
| 69 |
print("Face detected: Applying InstantID.")
|
| 70 |
+
# Use strengths from UI
|
| 71 |
+
controlnet_conditioning_scale = [0.6, depth_strength, lineart_strength]
|
| 72 |
+
control_guidance_end = [0.3, 0.6, 0.6] # Stop InstantID early
|
| 73 |
+
self.mh.pipeline.set_ip_adapter_scale(0.6) # Set IP-Adapter (likeness) strength
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
else:
|
| 75 |
print("No face detected: Disabling InstantID.")
|
| 76 |
+
# Use strengths from UI, but keep InstantID at 0.0
|
| 77 |
controlnet_conditioning_scale = [0.0, depth_strength, lineart_strength]
|
| 78 |
control_guidance_end = [0.3, 0.6, 0.6]
|
| 79 |
+
self.mh.pipeline.set_ip_adapter_scale(0.0)
|
| 80 |
|
| 81 |
# --- START FIX for NoneType Error ---
|
| 82 |
# Create a dummy tensor instead of passing None
|
|
|
|
| 88 |
print("Running pipeline...")
|
| 89 |
result = self.mh.pipeline(
|
| 90 |
prompt=final_prompt,
|
|
|
|
|
|
|
| 91 |
image=processed_image, # Base image for Img2Img
|
| 92 |
control_image=[processed_image, depth_map, lineart_map], # ControlNet inputs
|
| 93 |
image_embeds=face_emb, # Face embedding (or dummy)
|
|
|
|
| 101 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 102 |
control_guidance_end=control_guidance_end,
|
| 103 |
|
| 104 |
+
clip_skip=2,
|
|
|
|
| 105 |
|
| 106 |
# --- LoRA Strength REMOVED ---
|
| 107 |
# No longer needed, as LoRA is fused into the model weights
|
|
|
|
| 109 |
|
| 110 |
).images[0]
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
return result
|