Spaces:
Sleeping
Sleeping
Anurag Bhardwaj
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,220 +1,29 @@
|
|
1 |
-
import sys
|
2 |
-
import subprocess
|
3 |
-
import importlib.util
|
4 |
-
import os
|
5 |
-
|
6 |
-
# List of required packages.
|
7 |
-
required_packages = {
|
8 |
-
"gradio": "gradio",
|
9 |
-
"numpy": "numpy",
|
10 |
-
"torch": "torch",
|
11 |
-
"diffusers": "diffusers",
|
12 |
-
"PIL": "pillow",
|
13 |
-
"spaces": "spaces", # if this is a package available in your environment
|
14 |
-
"transformers": "transformers",
|
15 |
-
}
|
16 |
-
|
17 |
-
def install_package(package_name):
|
18 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])
|
19 |
-
|
20 |
-
# Auto-install any missing packages.
|
21 |
-
for mod, pkg in required_packages.items():
|
22 |
-
if importlib.util.find_spec(mod) is None:
|
23 |
-
print(f"Module {mod} not found, installing {pkg}...")
|
24 |
-
install_package(pkg)
|
25 |
-
|
26 |
-
# Explicitly import transformers to ensure it's loaded.
|
27 |
-
import transformers
|
28 |
-
|
29 |
-
import random
|
30 |
import gradio as gr
|
31 |
-
|
32 |
-
import spaces
|
33 |
import torch
|
34 |
-
from diffusers import DiffusionPipeline
|
35 |
from PIL import Image
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
)
|
52 |
-
pipeline.load_lora_weights(adapter_id)
|
53 |
-
pipeline = pipeline.to(device)
|
54 |
-
|
55 |
-
MAX_SEED = np.iinfo(np.int32).max
|
56 |
-
MAX_IMAGE_SIZE = 1024
|
57 |
-
|
58 |
-
@spaces.GPU(duration=80)
|
59 |
-
def inference(
|
60 |
-
prompt: str,
|
61 |
-
seed: int,
|
62 |
-
randomize_seed: bool,
|
63 |
-
width: int,
|
64 |
-
height: int,
|
65 |
-
guidance_scale: float,
|
66 |
-
num_inference_steps: int,
|
67 |
-
lora_scale: float,
|
68 |
-
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
69 |
-
):
|
70 |
-
if randomize_seed:
|
71 |
-
seed = random.randint(0, MAX_SEED)
|
72 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
73 |
-
|
74 |
-
image = pipeline(
|
75 |
-
prompt=prompt,
|
76 |
-
guidance_scale=guidance_scale,
|
77 |
-
num_inference_steps=num_inference_steps,
|
78 |
-
width=width,
|
79 |
-
height=height,
|
80 |
-
generator=generator,
|
81 |
-
joint_attention_kwargs={"scale": lora_scale},
|
82 |
-
).images[0]
|
83 |
-
|
84 |
-
return image, seed
|
85 |
-
|
86 |
-
examples = [
|
87 |
-
(
|
88 |
-
"Ghibli style futuristic stormtrooper with glossy white armor and a sleek helmet,"
|
89 |
-
" standing heroically on a lush alien planet, vibrant flowers blooming around, soft"
|
90 |
-
" sunlight illuminating the scene, a gentle breeze rustling the leaves"
|
91 |
-
),
|
92 |
-
]
|
93 |
-
|
94 |
-
css = """
|
95 |
-
#col-container {
|
96 |
-
margin: 0 auto;
|
97 |
-
max-width: 640px;
|
98 |
-
}
|
99 |
-
"""
|
100 |
-
|
101 |
-
with gr.Blocks(css=css) as demo:
|
102 |
-
with gr.Column(elem_id="col-container"):
|
103 |
-
gr.Markdown("# FLUX.1 Studio Ghibli LoRA")
|
104 |
-
gr.Markdown(
|
105 |
-
"[alvarobartt/ghibli-characters-flux-lora](https://huggingface.co/alvarobartt/ghibli-characters-flux-lora)"
|
106 |
-
" is a LoRA fine-tune of [FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev)"
|
107 |
-
" with [alvarobartt/ghibli-characters](https://huggingface.co/datasets/alvarobartt/ghibli-characters)."
|
108 |
-
)
|
109 |
-
|
110 |
-
with gr.Accordion("How to generate nice prompts?", open=False):
|
111 |
-
gr.Markdown(
|
112 |
-
"What worked best for me to generate high-quality prompts of well-known characters,"
|
113 |
-
" was to prompt either [Claude 3 Haiku](https://claude.ai), [GPT4-o](https://chatgpt.com/),"
|
114 |
-
" or [Perplexity](https://www.perplexity.ai/) with:\n\nYou are an"
|
115 |
-
" expert prompt writer for diffusion text to image models, and you've been provided"
|
116 |
-
" the following prompt template:\n\n\"Ghibli style [character description] with"
|
117 |
-
" [distinctive features], [action or pose], [environment or background],"
|
118 |
-
" [lighting or atmosphere], [additional details].\"\n\nCould you create a prompt"
|
119 |
-
" to generate [CHARACTER NAME] as a Studio Ghibli character following that template? "
|
120 |
-
"[MORE DETAILS IF NEEDED]\n"
|
121 |
-
)
|
122 |
-
|
123 |
-
with gr.Row():
|
124 |
-
prompt = gr.Text(
|
125 |
-
label="Prompt",
|
126 |
-
show_label=False,
|
127 |
-
max_lines=1,
|
128 |
-
placeholder="Enter your prompt",
|
129 |
-
container=False,
|
130 |
-
)
|
131 |
-
|
132 |
-
run_button = gr.Button("Run", scale=0)
|
133 |
-
|
134 |
-
result = gr.Image(label="Result", show_label=False)
|
135 |
-
|
136 |
-
with gr.Accordion("Advanced Settings", open=False):
|
137 |
-
seed = gr.Slider(
|
138 |
-
label="Seed",
|
139 |
-
minimum=0,
|
140 |
-
maximum=MAX_SEED,
|
141 |
-
step=1,
|
142 |
-
value=42,
|
143 |
-
)
|
144 |
-
|
145 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
146 |
-
|
147 |
-
with gr.Row():
|
148 |
-
width = gr.Slider(
|
149 |
-
label="Width",
|
150 |
-
minimum=256,
|
151 |
-
maximum=MAX_IMAGE_SIZE,
|
152 |
-
step=32,
|
153 |
-
value=1024,
|
154 |
-
)
|
155 |
-
|
156 |
-
height = gr.Slider(
|
157 |
-
label="Height",
|
158 |
-
minimum=256,
|
159 |
-
maximum=MAX_IMAGE_SIZE,
|
160 |
-
step=32,
|
161 |
-
value=768,
|
162 |
-
)
|
163 |
-
|
164 |
-
with gr.Row():
|
165 |
-
guidance_scale = gr.Slider(
|
166 |
-
label="Guidance scale",
|
167 |
-
minimum=0.0,
|
168 |
-
maximum=10.0,
|
169 |
-
step=0.1,
|
170 |
-
value=3.5,
|
171 |
-
)
|
172 |
-
|
173 |
-
num_inference_steps = gr.Slider(
|
174 |
-
label="Number of inference steps",
|
175 |
-
minimum=1,
|
176 |
-
maximum=50,
|
177 |
-
step=1,
|
178 |
-
value=30,
|
179 |
-
)
|
180 |
-
|
181 |
-
lora_scale = gr.Slider(
|
182 |
-
label="LoRA scale",
|
183 |
-
minimum=0.0,
|
184 |
-
maximum=1.0,
|
185 |
-
step=0.1,
|
186 |
-
value=1.0,
|
187 |
-
)
|
188 |
-
|
189 |
-
gr.Examples(
|
190 |
-
examples=examples,
|
191 |
-
fn=lambda x: (Image.open("./example.jpg"), 42),
|
192 |
-
inputs=[prompt],
|
193 |
-
outputs=[result, seed],
|
194 |
-
run_on_click=True,
|
195 |
-
)
|
196 |
-
|
197 |
-
gr.Markdown(
|
198 |
-
"### Disclaimer\n\n"
|
199 |
-
"License is non-commercial for both FLUX.1-dev and the Studio Ghibli dataset; "
|
200 |
-
"but free to use for personal and non-commercial purposes."
|
201 |
-
)
|
202 |
-
|
203 |
-
gr.on(
|
204 |
-
triggers=[run_button.click, prompt.submit],
|
205 |
-
fn=inference,
|
206 |
-
inputs=[
|
207 |
-
prompt,
|
208 |
-
seed,
|
209 |
-
randomize_seed,
|
210 |
-
width,
|
211 |
-
height,
|
212 |
-
guidance_scale,
|
213 |
-
num_inference_steps,
|
214 |
-
lora_scale,
|
215 |
-
],
|
216 |
-
outputs=[result, seed],
|
217 |
-
)
|
218 |
|
219 |
-
|
220 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
|
|
3 |
import torch
|
|
|
4 |
from PIL import Image
|
5 |
|
6 |
+
# Load the fine-tuned Studio Ghibli style model
|
7 |
+
model_id = "nitrosocke/Ghibli-Diffusion" # This model is fine-tuned to mimic the Studio Ghibli aesthetic
|
8 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
9 |
+
pipe.to("cuda") # Ensure you have GPU support in your Space; if not, remove or change accordingly
|
10 |
+
|
11 |
+
def transform_image(input_image: Image.Image) -> Image.Image:
|
12 |
+
# Resize input to a standard size (e.g., 512x512)
|
13 |
+
input_image = input_image.resize((512, 512))
|
14 |
+
prompt = "ghibli style"
|
15 |
+
# 'strength' controls how much the input image is modified (0.0 to 1.0)
|
16 |
+
output = pipe(prompt=prompt, image=input_image, strength=0.75, guidance_scale=7.5)
|
17 |
+
return output.images[0]
|
18 |
+
|
19 |
+
# Create a Gradio interface: input is an image and output is the transformed image.
|
20 |
+
demo = gr.Interface(
|
21 |
+
fn=transform_image,
|
22 |
+
inputs=gr.Image(type="pil", label="Upload your portrait/photo"),
|
23 |
+
outputs=gr.Image(type="pil", label="Studio Ghibli Style Output"),
|
24 |
+
title="Studio Ghibli Style Converter",
|
25 |
+
description="Upload a portrait or photo and transform it into a Studio Ghibli-style image using a fine-tuned Stable Diffusion model."
|
26 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
if __name__ == "__main__":
|
29 |
+
demo.launch()
|