Update app.py
Browse files
app.py
CHANGED
|
@@ -35,27 +35,42 @@ def single_condition_generate_image(prompt, spatial_img, height, width, seed, co
|
|
| 35 |
if API_TOKEN and api_token != API_TOKEN:
|
| 36 |
return "ERROR: Invalid API token. Please provide a valid token to generate images."
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Define the Gradio interface components
|
| 61 |
control_types = ["Ghibli"]
|
|
@@ -76,14 +91,15 @@ with gr.Blocks() as demo:
|
|
| 76 |
|
| 77 |
gr.Markdown("**[Attention!!]**οΌThe recommended prompts for using Ghibli Control LoRA should include the trigger words: `Ghibli Studio style, Charming hand-drawn anime-style illustration`")
|
| 78 |
gr.Markdown("ππIf you like this demo, please give us a star (github: [EasyControl](https://github.com/Xiaojiu-z/EasyControl))")
|
|
|
|
| 79 |
|
| 80 |
with gr.Tab("Ghibli Condition Generation"):
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column():
|
| 83 |
prompt = gr.Textbox(label="Prompt", value="Ghibli Studio style, Charming hand-drawn anime-style illustration")
|
| 84 |
spatial_img = gr.Image(label="Ghibli Image", type="pil")
|
| 85 |
-
height = gr.Slider(minimum=256, maximum=1024, step=
|
| 86 |
-
width = gr.Slider(minimum=256, maximum=1024, step=
|
| 87 |
seed = gr.Number(label="Seed", value=42)
|
| 88 |
control_type = gr.Dropdown(choices=control_types, label="Control Type", value="Ghibli")
|
| 89 |
single_generate_btn = gr.Button("Generate Image")
|
|
|
|
| 35 |
if API_TOKEN and api_token != API_TOKEN:
|
| 36 |
return "ERROR: Invalid API token. Please provide a valid token to generate images."
|
| 37 |
|
| 38 |
+
try:
|
| 39 |
+
# Ensure height and width are divisible by 8
|
| 40 |
+
height = int(height)
|
| 41 |
+
width = int(width)
|
| 42 |
+
|
| 43 |
+
if height % 8 != 0 or width % 8 != 0:
|
| 44 |
+
# Adjust to nearest multiple of 8
|
| 45 |
+
height = (height // 8) * 8
|
| 46 |
+
width = (width // 8) * 8
|
| 47 |
+
print(f"Dimensions adjusted to be divisible by 8: {height}x{width}")
|
| 48 |
+
|
| 49 |
+
# Set the control type
|
| 50 |
+
if control_type == "Ghibli":
|
| 51 |
+
lora_path = os.path.join(lora_base_path, "Ghibli.safetensors")
|
| 52 |
+
set_single_lora(pipe.transformer, lora_path, lora_weights=[1], cond_size=512)
|
| 53 |
+
|
| 54 |
+
# Process the image
|
| 55 |
+
spatial_imgs = [spatial_img] if spatial_img else []
|
| 56 |
+
image = pipe(
|
| 57 |
+
prompt,
|
| 58 |
+
height=height,
|
| 59 |
+
width=width,
|
| 60 |
+
guidance_scale=3.5,
|
| 61 |
+
num_inference_steps=25,
|
| 62 |
+
max_sequence_length=512,
|
| 63 |
+
generator=torch.Generator("cpu").manual_seed(seed),
|
| 64 |
+
subject_images=[],
|
| 65 |
+
spatial_images=spatial_imgs,
|
| 66 |
+
cond_size=512,
|
| 67 |
+
).images[0]
|
| 68 |
+
clear_cache(pipe.transformer)
|
| 69 |
+
return image
|
| 70 |
+
except Exception as e:
|
| 71 |
+
error_message = f"Error during generation: {str(e)}"
|
| 72 |
+
print(error_message)
|
| 73 |
+
return f"ERROR: {error_message}"
|
| 74 |
|
| 75 |
# Define the Gradio interface components
|
| 76 |
control_types = ["Ghibli"]
|
|
|
|
| 91 |
|
| 92 |
gr.Markdown("**[Attention!!]**οΌThe recommended prompts for using Ghibli Control LoRA should include the trigger words: `Ghibli Studio style, Charming hand-drawn anime-style illustration`")
|
| 93 |
gr.Markdown("ππIf you like this demo, please give us a star (github: [EasyControl](https://github.com/Xiaojiu-z/EasyControl))")
|
| 94 |
+
gr.Markdown("**NOTE**: Both height and width must be divisible by 8. Values will be automatically adjusted if needed.")
|
| 95 |
|
| 96 |
with gr.Tab("Ghibli Condition Generation"):
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column():
|
| 99 |
prompt = gr.Textbox(label="Prompt", value="Ghibli Studio style, Charming hand-drawn anime-style illustration")
|
| 100 |
spatial_img = gr.Image(label="Ghibli Image", type="pil")
|
| 101 |
+
height = gr.Slider(minimum=256, maximum=1024, step=8, label="Height", value=768)
|
| 102 |
+
width = gr.Slider(minimum=256, maximum=1024, step=8, label="Width", value=768)
|
| 103 |
seed = gr.Number(label="Seed", value=42)
|
| 104 |
control_type = gr.Dropdown(choices=control_types, label="Control Type", value="Ghibli")
|
| 105 |
single_generate_btn = gr.Button("Generate Image")
|