Spaces:
Running
on
Zero
Running
on
Zero
import gradio as gr | |
import numpy as np | |
import random | |
import torch | |
import spaces | |
import re | |
from diffusers import ( | |
DiffusionPipeline, | |
AutoencoderTiny, | |
) | |
from huggingface_hub import hf_hub_download | |
def feifeimodload(): | |
dtype = torch.bfloat16 | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
pipe = DiffusionPipeline.from_pretrained( | |
"aifeifei798/DarkIdol-flux-v1", torch_dtype=dtype | |
).to(device) | |
pipe.load_lora_weights( | |
hf_hub_download("aifeifei798/feifei-flux-lora-v1.1", "feifei-v1.1.safetensors"), | |
adapter_name="feifei", | |
) | |
pipe.vae.enable_slicing() | |
pipe.vae.enable_tiling() | |
torch.cuda.empty_cache() | |
return pipe | |
pipe = feifeimodload() | |
MAX_SEED = np.iinfo(np.int32).max | |
MAX_IMAGE_SIZE = 2048 | |
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidancescale=3.5, num_feifei=0.35, progress=gr.Progress(track_tqdm=True)): | |
if randomize_seed: | |
seed = random.randint(0, MAX_SEED) | |
generator = torch.Generator().manual_seed(seed) | |
prompt = re.sub("young woman", " feifei " ,prompt) | |
prompt = re.sub("woman", " feifei " ,prompt) | |
prompt = re.sub("girl", " feifei " ,prompt) | |
prompt = re.sub("model", " feifei " ,prompt) | |
pipe.set_adapters( | |
["feifei"], | |
adapter_weights=[num_feifei], | |
) | |
pipe.fuse_lora( | |
adapter_name=["feifei"], | |
lora_scale=1.0, | |
) | |
image = pipe( | |
prompt = "", | |
prompt_2 = prompt, | |
width = width, | |
height = height, | |
num_inference_steps = num_inference_steps, | |
generator = generator, | |
guidance_scale=guidancescale | |
).images[0] | |
return image, seed | |
examples = [ | |
"flux,this photo is kpop idol girl" | |
] | |
css=""" | |
#col-container { | |
margin: 0 auto; | |
max-width: 520px; | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
with gr.Column(elem_id="col-container"): | |
gr.Markdown(f"""# FLUX.1 + feifei-flux-lora """) | |
with gr.Row(): | |
prompt = gr.Text( | |
label="Prompt", | |
show_label=False, | |
max_lines=12, | |
placeholder="Enter your prompt", | |
value="flux", | |
container=False, | |
) | |
run_button = gr.Button("Run") | |
result = gr.Image(label="Result", show_label=False,height=520) | |
with gr.Accordion("Advanced Settings", open=False): | |
seed = gr.Slider( | |
label="Seed", | |
minimum=0, | |
maximum=MAX_SEED, | |
step=1, | |
value=0, | |
) | |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True) | |
with gr.Row(): | |
width = gr.Slider( | |
label="Width", | |
minimum=256, | |
maximum=MAX_IMAGE_SIZE, | |
step=64, | |
value=896, | |
) | |
height = gr.Slider( | |
label="Height", | |
minimum=256, | |
maximum=MAX_IMAGE_SIZE, | |
step=64, | |
value=1152, | |
) | |
with gr.Row(): | |
num_inference_steps = gr.Slider( | |
label="Number of inference steps", | |
minimum=1, | |
maximum=50, | |
step=1, | |
value=8, | |
) | |
with gr.Row(): | |
guidancescale = gr.Slider( | |
label="Guidance scale", | |
minimum=0, | |
maximum=10, | |
step=0.1, | |
value=3.5, | |
) | |
with gr.Row(): | |
num_feifei = gr.Slider( | |
label="FeiFei", | |
minimum=0, | |
maximum=2, | |
step=0.05, | |
value=0.55, | |
) | |
gr.Examples( | |
examples = examples, | |
fn = infer, | |
inputs = [prompt], | |
outputs = [result, seed], | |
cache_examples=False | |
) | |
gr.on( | |
triggers=[run_button.click, prompt.submit], | |
fn = infer, | |
inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps, guidancescale, num_feifei], | |
outputs = [result, seed] | |
) | |
demo.launch() |