jamesliu1217 commited on
Commit
5a48378
Β·
verified Β·
1 Parent(s): 2bed018

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import os
3
+ import json
4
+ import time
5
+ import torch
6
+ from PIL import Image
7
+ from tqdm import tqdm
8
+ import gradio as gr
9
+
10
+ from safetensors.torch import save_file
11
+ from src.pipeline import FluxPipeline
12
+ from src.transformer_flux import FluxTransformer2DModel
13
+ from src.lora_helper import set_single_lora, set_multi_lora, unset_lora
14
+
15
+ # Initialize the image processor
16
+ base_path = "black-forest-labs/FLUX.1-dev"
17
+ lora_base_path = "./models"
18
+
19
+
20
+ pipe = FluxPipeline.from_pretrained(base_path, torch_dtype=torch.bfloat16)
21
+ transformer = FluxTransformer2DModel.from_pretrained(base_path, subfolder="transformer", torch_dtype=torch.bfloat16)
22
+ pipe.transformer = transformer
23
+ pipe.to("cuda")
24
+
25
+ def clear_cache(transformer):
26
+ for name, attn_processor in transformer.attn_processors.items():
27
+ attn_processor.bank_kv.clear()
28
+
29
+ # Define the Gradio interface
30
+ @spaces.GPU()
31
+ def single_condition_generate_image(prompt, subject_img, spatial_img, height, width, seed, control_type):
32
+ # Set the control type
33
+ if control_type == "Ghibli":
34
+ lora_path = os.path.join(lora_base_path, "Ghibli.safetensors")
35
+ set_single_lora(pipe.transformer, lora_path, lora_weights=[1], cond_size=512)
36
+
37
+ # Process the image
38
+ subject_imgs = [subject_img] if subject_img else []
39
+ spatial_imgs = [spatial_img] if spatial_img else []
40
+ image = pipe(
41
+ prompt,
42
+ height=int(height),
43
+ width=int(width),
44
+ guidance_scale=3.5,
45
+ num_inference_steps=25,
46
+ max_sequence_length=512,
47
+ generator=torch.Generator("cpu").manual_seed(seed),
48
+ subject_images=subject_imgs,
49
+ spatial_images=spatial_imgs,
50
+ cond_size=512,
51
+ ).images[0]
52
+ clear_cache(pipe.transformer)
53
+ return image
54
+
55
+ # Define the Gradio interface components
56
+ control_types = ["Ghibli"]
57
+
58
+ # Example data
59
+ single_examples = [
60
+ ["Ghibli Studio style, Charming hand-drawn anime-style illustration", None, Image.open("./test_imgs/00.png"), 768, 768, 5, "Ghibli"],
61
+ ["Ghibli Studio style, Charming hand-drawn anime-style illustration", None, Image.open("./test_imgs/02.png"), 768, 768, 42, "Ghibli"],
62
+ ["Ghibli Studio style, Charming hand-drawn anime-style illustration", None, Image.open("./test_imgs/03.png"), 768, 768, 1, "Ghibli"],
63
+ ]
64
+
65
+
66
+ # Create the Gradio Blocks interface
67
+ with gr.Blocks() as demo:
68
+ gr.Markdown("# Ghibli Studio Control Image Generation with EasyControl")
69
+ gr.Markdown("Generate images using EasyControl with Ghibli control LoRAs.(Due to hardware constraints, only low-resolution images can be generated. For high-resolution (1024+), please set up your own environment.οΌ‰")
70
+
71
+ with gr.Tab("Ghibli Condition Generation"):
72
+ with gr.Row():
73
+ with gr.Column():
74
+ prompt = gr.Textbox(label="Prompt")
75
+ spatial_img = gr.Image(label="Ghibli Image", type="pil") # δΈŠδΌ ε›Ύεƒζ–‡δ»Ά
76
+ height = gr.Slider(minimum=256, maximum=1024, step=64, label="Height", value=768)
77
+ width = gr.Slider(minimum=256, maximum=1024, step=64, label="Width", value=768)
78
+ seed = gr.Number(label="Seed", value=42)
79
+ control_type = gr.Dropdown(choices=control_types, label="Control Type")
80
+ single_generate_btn = gr.Button("Generate Image")
81
+ with gr.Column():
82
+ single_output_image = gr.Image(label="Generated Image")
83
+
84
+ # Add examples for Single Condition Generation
85
+ gr.Examples(
86
+ examples=single_examples,
87
+ inputs=[prompt, None, spatial_img, height, width, seed, control_type],
88
+ outputs=single_output_image,
89
+ fn=single_condition_generate_image,
90
+ cache_examples=False, # ηΌ“ε­˜η€ΊδΎ‹η»“ζžœδ»₯εŠ εΏ«εŠ θ½½ι€ŸεΊ¦
91
+ label="Single Condition Examples"
92
+ )
93
+
94
+ # Link the buttons to the functions
95
+ single_generate_btn.click(
96
+ single_condition_generate_image,
97
+ inputs=[prompt, None, spatial_img, height, width, seed, control_type],
98
+ outputs=single_output_image
99
+ )
100
+
101
+ # Launch the Gradio app
102
+ demo.queue().launch()