Spaces:
Running
Running
Delete app-backup.py
Browse files- app-backup.py +0 -287
app-backup.py
DELETED
|
@@ -1,287 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
-
import torch
|
| 4 |
-
from diffusers import FluxKontextPipeline
|
| 5 |
-
from diffusers.utils import load_image
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import os
|
| 8 |
-
|
| 9 |
-
# Style dictionary
|
| 10 |
-
style_type_lora_dict = {
|
| 11 |
-
"3D_Chibi": "3D_Chibi_lora_weights.safetensors",
|
| 12 |
-
"American_Cartoon": "American_Cartoon_lora_weights.safetensors",
|
| 13 |
-
"Chinese_Ink": "Chinese_Ink_lora_weights.safetensors",
|
| 14 |
-
"Clay_Toy": "Clay_Toy_lora_weights.safetensors",
|
| 15 |
-
"Fabric": "Fabric_lora_weights.safetensors",
|
| 16 |
-
"Ghibli": "Ghibli_lora_weights.safetensors",
|
| 17 |
-
"Irasutoya": "Irasutoya_lora_weights.safetensors",
|
| 18 |
-
"Jojo": "Jojo_lora_weights.safetensors",
|
| 19 |
-
"Oil_Painting": "Oil_Painting_lora_weights.safetensors",
|
| 20 |
-
"Pixel": "Pixel_lora_weights.safetensors",
|
| 21 |
-
"Snoopy": "Snoopy_lora_weights.safetensors",
|
| 22 |
-
"Poly": "Poly_lora_weights.safetensors",
|
| 23 |
-
"LEGO": "LEGO_lora_weights.safetensors",
|
| 24 |
-
"Origami": "Origami_lora_weights.safetensors",
|
| 25 |
-
"Pop_Art": "Pop_Art_lora_weights.safetensors",
|
| 26 |
-
"Van_Gogh": "Van_Gogh_lora_weights.safetensors",
|
| 27 |
-
"Paper_Cutting": "Paper_Cutting_lora_weights.safetensors",
|
| 28 |
-
"Line": "Line_lora_weights.safetensors",
|
| 29 |
-
"Vector": "Vector_lora_weights.safetensors",
|
| 30 |
-
"Picasso": "Picasso_lora_weights.safetensors",
|
| 31 |
-
"Macaron": "Macaron_lora_weights.safetensors",
|
| 32 |
-
"Rick_Morty": "Rick_Morty_lora_weights.safetensors"
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
# Style descriptions
|
| 36 |
-
style_descriptions = {
|
| 37 |
-
"3D_Chibi": "Cute, miniature 3D character style with big heads",
|
| 38 |
-
"American_Cartoon": "Classic American animation style",
|
| 39 |
-
"Chinese_Ink": "Traditional Chinese ink painting aesthetic",
|
| 40 |
-
"Clay_Toy": "Playful clay/plasticine toy appearance",
|
| 41 |
-
"Fabric": "Soft, textile-like rendering",
|
| 42 |
-
"Ghibli": "Studio Ghibli's distinctive anime style",
|
| 43 |
-
"Irasutoya": "Simple, flat Japanese illustration style",
|
| 44 |
-
"Jojo": "JoJo's Bizarre Adventure manga style",
|
| 45 |
-
"Oil_Painting": "Classic oil painting texture and strokes",
|
| 46 |
-
"Pixel": "Retro pixel art style",
|
| 47 |
-
"Snoopy": "Peanuts comic strip style",
|
| 48 |
-
"Poly": "Low-poly 3D geometric style",
|
| 49 |
-
"LEGO": "LEGO brick construction style",
|
| 50 |
-
"Origami": "Paper folding art style",
|
| 51 |
-
"Pop_Art": "Bold, colorful pop art style",
|
| 52 |
-
"Van_Gogh": "Van Gogh's expressive brushstroke style",
|
| 53 |
-
"Paper_Cutting": "Paper cut-out art style",
|
| 54 |
-
"Line": "Clean line art/sketch style",
|
| 55 |
-
"Vector": "Clean vector graphics style",
|
| 56 |
-
"Picasso": "Cubist art style inspired by Picasso",
|
| 57 |
-
"Macaron": "Soft, pastel macaron-like style",
|
| 58 |
-
"Rick_Morty": "Rick and Morty cartoon style"
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
# Initialize pipeline globally
|
| 62 |
-
pipeline = None
|
| 63 |
-
pipeline_loaded = False
|
| 64 |
-
|
| 65 |
-
def load_pipeline():
|
| 66 |
-
global pipeline, pipeline_loaded
|
| 67 |
-
if pipeline is None:
|
| 68 |
-
print("Loading FLUX.1-Kontext-dev model...")
|
| 69 |
-
# HF_TOKEN 자동 감지
|
| 70 |
-
token = os.getenv("HF_TOKEN", True)
|
| 71 |
-
|
| 72 |
-
pipeline = FluxKontextPipeline.from_pretrained(
|
| 73 |
-
"black-forest-labs/FLUX.1-Kontext-dev",
|
| 74 |
-
torch_dtype=torch.bfloat16,
|
| 75 |
-
use_auth_token=token
|
| 76 |
-
)
|
| 77 |
-
pipeline_loaded = True
|
| 78 |
-
return pipeline
|
| 79 |
-
|
| 80 |
-
@spaces.GPU(duration=120)
|
| 81 |
-
def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps, guidance_scale, seed):
|
| 82 |
-
"""
|
| 83 |
-
Apply style transfer to the input image using selected style
|
| 84 |
-
"""
|
| 85 |
-
if input_image is None:
|
| 86 |
-
gr.Warning("Please upload an image first!")
|
| 87 |
-
return None
|
| 88 |
-
|
| 89 |
-
try:
|
| 90 |
-
# Load pipeline and move to GPU
|
| 91 |
-
pipe = load_pipeline()
|
| 92 |
-
pipe = pipe.to('cuda')
|
| 93 |
-
|
| 94 |
-
# Enable memory efficient settings
|
| 95 |
-
pipe.enable_model_cpu_offload()
|
| 96 |
-
|
| 97 |
-
# Set seed for reproducibility
|
| 98 |
-
generator = None
|
| 99 |
-
if seed > 0:
|
| 100 |
-
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 101 |
-
|
| 102 |
-
# Process input image
|
| 103 |
-
if isinstance(input_image, str):
|
| 104 |
-
image = load_image(input_image)
|
| 105 |
-
else:
|
| 106 |
-
image = input_image
|
| 107 |
-
|
| 108 |
-
# Ensure RGB and resize to 1024x1024
|
| 109 |
-
image = image.convert("RGB").resize((1024, 1024), Image.Resampling.LANCZOS)
|
| 110 |
-
|
| 111 |
-
# Load the selected LoRA
|
| 112 |
-
lora_filename = style_type_lora_dict[style_name]
|
| 113 |
-
|
| 114 |
-
# Clear any previously loaded LoRA
|
| 115 |
-
try:
|
| 116 |
-
pipe.unload_lora_weights()
|
| 117 |
-
except:
|
| 118 |
-
pass
|
| 119 |
-
|
| 120 |
-
# Load LoRA weights
|
| 121 |
-
pipe.load_lora_weights(
|
| 122 |
-
"Owen777/Kontext-Style-Loras",
|
| 123 |
-
weight_name=lora_filename,
|
| 124 |
-
adapter_name="style"
|
| 125 |
-
)
|
| 126 |
-
pipe.set_adapters(["style"], adapter_weights=[1.0])
|
| 127 |
-
|
| 128 |
-
# Create prompt for style transformation
|
| 129 |
-
style_name_readable = style_name.replace('_', ' ')
|
| 130 |
-
prompt = f"Turn this image into the {style_name_readable} style."
|
| 131 |
-
if prompt_suffix and prompt_suffix.strip():
|
| 132 |
-
prompt += f" {prompt_suffix.strip()}"
|
| 133 |
-
|
| 134 |
-
print(f"Generating with prompt: {prompt}")
|
| 135 |
-
|
| 136 |
-
# Generate the styled image
|
| 137 |
-
result = pipe(
|
| 138 |
-
image=image,
|
| 139 |
-
prompt=prompt,
|
| 140 |
-
guidance_scale=guidance_scale,
|
| 141 |
-
num_inference_steps=num_inference_steps,
|
| 142 |
-
generator=generator,
|
| 143 |
-
height=1024,
|
| 144 |
-
width=1024
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
# Clear GPU memory
|
| 148 |
-
torch.cuda.empty_cache()
|
| 149 |
-
|
| 150 |
-
return result.images[0]
|
| 151 |
-
|
| 152 |
-
except Exception as e:
|
| 153 |
-
print(f"Error: {str(e)}")
|
| 154 |
-
gr.Error(f"Error during style transfer: {str(e)}")
|
| 155 |
-
torch.cuda.empty_cache()
|
| 156 |
-
return None
|
| 157 |
-
|
| 158 |
-
# Create Gradio interface
|
| 159 |
-
with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as demo:
|
| 160 |
-
gr.Markdown("""
|
| 161 |
-
# 🎨 FLUX.1 Kontext Style Transfer
|
| 162 |
-
|
| 163 |
-
Transform your images into various artistic styles using FLUX.1-Kontext-dev and high-quality style LoRAs.
|
| 164 |
-
|
| 165 |
-
This demo uses the official Owen777/Kontext-Style-Loras collection with 22 different artistic styles!
|
| 166 |
-
""")
|
| 167 |
-
|
| 168 |
-
with gr.Row():
|
| 169 |
-
with gr.Column(scale=1):
|
| 170 |
-
input_image = gr.Image(
|
| 171 |
-
label="Upload Image",
|
| 172 |
-
type="pil",
|
| 173 |
-
height=400
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
style_dropdown = gr.Dropdown(
|
| 177 |
-
choices=list(style_type_lora_dict.keys()),
|
| 178 |
-
value="Ghibli",
|
| 179 |
-
label="Select Style",
|
| 180 |
-
info="Choose from 22 different artistic styles"
|
| 181 |
-
)
|
| 182 |
-
|
| 183 |
-
style_info = gr.Textbox(
|
| 184 |
-
label="Style Description",
|
| 185 |
-
value=style_descriptions["Ghibli"],
|
| 186 |
-
interactive=False,
|
| 187 |
-
lines=2
|
| 188 |
-
)
|
| 189 |
-
|
| 190 |
-
prompt_suffix = gr.Textbox(
|
| 191 |
-
label="Additional Instructions (Optional)",
|
| 192 |
-
placeholder="Add extra details like 'make it more colorful' or 'add dramatic lighting'...",
|
| 193 |
-
lines=2
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 197 |
-
num_steps = gr.Slider(
|
| 198 |
-
minimum=10,
|
| 199 |
-
maximum=50,
|
| 200 |
-
value=24,
|
| 201 |
-
step=1,
|
| 202 |
-
label="Inference Steps",
|
| 203 |
-
info="More steps = better quality but slower"
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
guidance = gr.Slider(
|
| 207 |
-
minimum=1.0,
|
| 208 |
-
maximum=5.0,
|
| 209 |
-
value=2.5,
|
| 210 |
-
step=0.1,
|
| 211 |
-
label="Guidance Scale",
|
| 212 |
-
info="How closely to follow the prompt (2.5 recommended)"
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
seed = gr.Number(
|
| 216 |
-
label="Seed",
|
| 217 |
-
value=42,
|
| 218 |
-
precision=0,
|
| 219 |
-
info="Set to 0 for random results"
|
| 220 |
-
)
|
| 221 |
-
|
| 222 |
-
generate_btn = gr.Button("🎨 Transform Image", variant="primary", size="lg")
|
| 223 |
-
|
| 224 |
-
with gr.Column(scale=1):
|
| 225 |
-
output_image = gr.Image(
|
| 226 |
-
label="Styled Result",
|
| 227 |
-
type="pil",
|
| 228 |
-
height=400
|
| 229 |
-
)
|
| 230 |
-
|
| 231 |
-
gr.Markdown("""
|
| 232 |
-
### 💡 Tips:
|
| 233 |
-
- All images are resized to 1024x1024
|
| 234 |
-
- First run downloads the model (~12GB)
|
| 235 |
-
- Each style transformation takes ~30-60 seconds
|
| 236 |
-
- Try different styles to find the best match!
|
| 237 |
-
- Use additional instructions for fine control
|
| 238 |
-
""")
|
| 239 |
-
|
| 240 |
-
# Update style description when style changes
|
| 241 |
-
def update_description(style):
|
| 242 |
-
return style_descriptions.get(style, "")
|
| 243 |
-
|
| 244 |
-
style_dropdown.change(
|
| 245 |
-
fn=update_description,
|
| 246 |
-
inputs=[style_dropdown],
|
| 247 |
-
outputs=[style_info]
|
| 248 |
-
)
|
| 249 |
-
|
| 250 |
-
# Examples
|
| 251 |
-
gr.Examples(
|
| 252 |
-
examples=[
|
| 253 |
-
["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "Ghibli", ""],
|
| 254 |
-
["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "3D_Chibi", "make it extra cute"],
|
| 255 |
-
["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "Van_Gogh", "with swirling sky"],
|
| 256 |
-
["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "Pixel", "8-bit retro game style"],
|
| 257 |
-
],
|
| 258 |
-
inputs=[input_image, style_dropdown, prompt_suffix],
|
| 259 |
-
outputs=output_image,
|
| 260 |
-
fn=style_transfer,
|
| 261 |
-
cache_examples=False
|
| 262 |
-
)
|
| 263 |
-
|
| 264 |
-
# Connect the generate button
|
| 265 |
-
generate_btn.click(
|
| 266 |
-
fn=style_transfer,
|
| 267 |
-
inputs=[input_image, style_dropdown, prompt_suffix, num_steps, guidance, seed],
|
| 268 |
-
outputs=output_image
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
-
gr.Markdown("""
|
| 272 |
-
---
|
| 273 |
-
### 📚 Available Styles:
|
| 274 |
-
|
| 275 |
-
**Anime/Cartoon**: Ghibli, American Cartoon, Jojo, Snoopy, Rick & Morty, Irasutoya
|
| 276 |
-
**3D/Geometric**: 3D Chibi, Poly, LEGO, Clay Toy
|
| 277 |
-
**Traditional Art**: Chinese Ink, Oil Painting, Van Gogh, Picasso, Pop Art
|
| 278 |
-
**Craft/Material**: Fabric, Origami, Paper Cutting, Macaron
|
| 279 |
-
**Digital/Modern**: Pixel, Line, Vector
|
| 280 |
-
|
| 281 |
-
---
|
| 282 |
-
|
| 283 |
-
Created with ❤️ using [Owen777/Kontext-Style-Loras](https://huggingface.co/Owen777/Kontext-Style-Loras)
|
| 284 |
-
""")
|
| 285 |
-
|
| 286 |
-
if __name__ == "__main__":
|
| 287 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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