Spaces:
Sleeping
Sleeping
File size: 6,534 Bytes
e547b24 1b9717a e547b24 1b9717a e547b24 1b9717a e547b24 1b9717a e547b24 eac94ad e547b24 eac94ad 6f5a32e e547b24 1b9717a e547b24 1b9717a e547b24 1b9717a 753d150 1b9717a eac94ad 753d150 e547b24 1b9717a e547b24 1b9717a e547b24 02f8cfa 4d6cbec eac94ad 9608c70 eac94ad 9608c70 eac94ad 9608c70 eac94ad 9608c70 73f7edc e547b24 9608c70 1b9717a 4d6cbec 02f8cfa 753d150 eac94ad 753d150 4d6cbec eac94ad e547b24 eac94ad 4d6cbec 02f8cfa eac94ad 1b9717a eac94ad e547b24 eac94ad 753d150 e547b24 eac94ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
import gradio as gr
import torch
import random
import os
import time
from PIL import Image
from deep_translator import GoogleTranslator
from diffusers import DiffusionPipeline
from huggingface_hub import hf_hub_download
# Project by Nymbo with LoRA integration
# Model and LoRA configuration
BASE_MODEL = "black-forest-labs/FLUX.1-dev"
LORA_REPO = "burhansyam/uncen"
LORA_WEIGHTS_NAME = "uncen.safetensors" # Adjust if different
torch_dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
# Initialize the pipeline with LoRA
def init_pipeline():
pipe = DiffusionPipeline.from_pretrained(
BASE_MODEL,
torch_dtype=torch_dtype
)
# Load LoRA weights
pipe.load_lora_weights(
hf_hub_download(repo_id=LORA_REPO, filename=LORA_WEIGHTS_NAME),
adapter_name="uncen"
)
# Enable model offloading if needed
if torch.cuda.is_available():
pipe.to("cuda")
pipe.enable_xformers_memory_efficient_attention()
return pipe
pipe = init_pipeline()
def convert_to_png(image):
"""Convert any image format to true PNG format"""
png_buffer = io.BytesIO()
if image.mode == 'RGBA':
image.save(png_buffer, format='PNG', optimize=True)
else:
if image.mode != 'RGB':
image = image.convert('RGB')
image.save(png_buffer, format='PNG', optimize=True)
png_buffer.seek(0)
return Image.open(png_buffer)
def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras",
seed=-1, strength=0.7, width=1024, height=1024):
if not prompt:
return None
key = random.randint(0, 999)
# Translate prompt
try:
prompt = GoogleTranslator(source='id', target='en').translate(prompt)
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
except Exception as e:
print(f"Translation error: {e}")
print(f'\033[1mGeneration {key}:\033[0m {prompt}')
# Set random seed if not specified
generator = None
if seed != -1:
generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu").manual_seed(seed)
else:
seed = random.randint(1, 1000000000)
generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu").manual_seed(seed)
# Map sampler names to Diffusers scheduler names
sampler_map = {
"DPM++ 2M Karras": "dpmsolver++",
"DPM++ SDE Karras": "dpmsolver++",
"Euler": "euler",
"Euler a": "euler_a",
"Heun": "heun",
"DDIM": "ddim"
}
try:
# Generate image with LoRA
image = pipe(
prompt=prompt,
negative_prompt=is_negative if is_negative else None,
num_inference_steps=steps,
guidance_scale=cfg_scale,
generator=generator,
strength=strength,
width=width,
height=height,
cross_attention_kwargs={"scale": 0.8}, # LoRA strength adjustment
).images[0]
png_img = convert_to_png(image)
print(f'\033[1mGeneration {key} completed as PNG!\033[0m')
return png_img
except Exception as e:
print(f"Generation error: {e}")
raise gr.Error(f"Image generation failed: {str(e)}")
# Light theme CSS (same as before)
css = """
#app-container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #ffffff;
}
#prompt-text-input, #negative-prompt-text-input {
font-size: 14px;
background: #f9f9f9;
}
#gallery {
min-height: 512px;
background: #ffffff;
border: 1px solid #e0e0e0;
}
#gen-button {
margin: 10px 0;
background: #4CAF50;
color: white;
}
.accordion {
background: #f5f5f5;
border: 1px solid #e0e0e0;
}
h1 {
color: #333333;
}
"""
with gr.Blocks(theme=gr.themes.Default(primary_hue="green"), css=css) as app:
gr.HTML("<center><h1>FLUX.1-Dev with LoRA (PNG Output)</h1></center>")
with gr.Column(elem_id="app-container"):
with gr.Row():
with gr.Column(elem_id="prompt-container"):
with gr.Row():
text_prompt = gr.Textbox(
label="Prompt",
placeholder="Masukkan prompt dalam Bahasa Indonesia",
lines=2,
elem_id="prompt-text-input"
)
with gr.Accordion("Advanced Settings", open=False):
negative_prompt = gr.Textbox(
label="Negative Prompt",
value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
lines=3
)
with gr.Row():
width = gr.Slider(1024, label="Width", minimum=512, maximum=1536, step=64)
height = gr.Slider(1024, label="Height", minimum=512, maximum=1536, step=64)
with gr.Row():
steps = gr.Slider(35, label="Steps", minimum=10, maximum=100, step=1)
cfg = gr.Slider(7.0, label="CFG Scale", minimum=1.0, maximum=20.0, step=0.5)
with gr.Row():
strength = gr.Slider(0.7, label="Strength", minimum=0.1, maximum=1.0, step=0.01)
seed = gr.Number(-1, label="Seed (-1 for random)")
method = gr.Radio(
["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"],
value="DPM++ 2M Karras",
label="Sampling Method"
)
generate_btn = gr.Button("Generate Image", variant="primary")
with gr.Row():
output_image = gr.Image(
type="pil",
label="Generated PNG Image",
format="png",
elem_id="gallery"
)
generate_btn.click(
fn=query,
inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height],
outputs=output_image
)
app.launch(server_name="0.0.0.0", server_port=7860, share=True) |