Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,75 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
import random
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
if seed == -1:
|
| 8 |
seed = random.randint(1, 1000000000)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
"prompt": prompt,
|
| 12 |
-
"hf_lora": "codermert/mert_flux",
|
| 13 |
-
"output_format": "jpg",
|
| 14 |
-
"aspect_ratio": aspect_ratio,
|
| 15 |
-
"num_inference_steps": steps,
|
| 16 |
-
"guidance_scale": cfg_scale,
|
| 17 |
-
"lora_scale": strength,
|
| 18 |
-
"seed": seed,
|
| 19 |
-
"disable_safety_checker": True
|
| 20 |
-
}
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
css = """
|
| 31 |
#app-container {
|
| 32 |
-
max-width:
|
| 33 |
margin-left: auto;
|
| 34 |
margin-right: auto;
|
| 35 |
}
|
| 36 |
"""
|
| 37 |
|
| 38 |
examples = [
|
| 39 |
-
"A beautiful landscape with mountains and a lake",
|
| 40 |
-
"A futuristic cityscape at night",
|
| 41 |
-
"A portrait of a smiling person in a colorful outfit",
|
| 42 |
]
|
| 43 |
|
| 44 |
with gr.Blocks(theme='default', css=css) as app:
|
| 45 |
-
gr.HTML("<center><h1>Mert Flux
|
| 46 |
with gr.Column(elem_id="app-container"):
|
| 47 |
with gr.Row():
|
| 48 |
text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2)
|
|
|
|
| 49 |
with gr.Row():
|
| 50 |
-
with gr.
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
strength = gr.Slider(label="Strength", value=0.
|
| 55 |
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
-
|
| 59 |
with gr.Row():
|
| 60 |
image_output = gr.Image(type="pil", label="Generated Image", show_download_button=True)
|
| 61 |
with gr.Row():
|
| 62 |
-
seed_output = gr.
|
| 63 |
|
| 64 |
-
gr.Examples(examples=examples, inputs=[text_prompt])
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
inputs=[text_prompt,
|
| 69 |
outputs=[image_output, seed_output]
|
| 70 |
)
|
| 71 |
|
| 72 |
-
app.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 8 |
+
lora_model_id = "codermert/tugce2-lora" # Your LoRA model
|
| 9 |
+
|
| 10 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 11 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 12 |
+
pipe = pipe.to("cuda")
|
| 13 |
+
pipe.load_lora_weights(lora_model_id)
|
| 14 |
+
|
| 15 |
+
def generate_image(prompt, negative_prompt, steps, cfg_scale, seed, strength):
|
| 16 |
if seed == -1:
|
| 17 |
seed = random.randint(1, 1000000000)
|
| 18 |
|
| 19 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
image = pipe(
|
| 22 |
+
prompt=prompt,
|
| 23 |
+
negative_prompt=negative_prompt,
|
| 24 |
+
num_inference_steps=steps,
|
| 25 |
+
guidance_scale=cfg_scale,
|
| 26 |
+
generator=generator,
|
| 27 |
+
cross_attention_kwargs={"scale": strength},
|
| 28 |
+
).images[0]
|
| 29 |
+
|
| 30 |
+
return image, seed
|
| 31 |
|
| 32 |
css = """
|
| 33 |
#app-container {
|
| 34 |
+
max-width: 800px;
|
| 35 |
margin-left: auto;
|
| 36 |
margin-right: auto;
|
| 37 |
}
|
| 38 |
"""
|
| 39 |
|
| 40 |
examples = [
|
| 41 |
+
["A beautiful landscape with mountains and a lake", "ugly, deformed"],
|
| 42 |
+
["A futuristic cityscape at night", "daytime, rural"],
|
| 43 |
+
["A portrait of a smiling person in a colorful outfit", "monochrome, frowning"],
|
| 44 |
]
|
| 45 |
|
| 46 |
with gr.Blocks(theme='default', css=css) as app:
|
| 47 |
+
gr.HTML("<center><h1>Mert Flux LoRA Explorer</h1></center>")
|
| 48 |
with gr.Column(elem_id="app-container"):
|
| 49 |
with gr.Row():
|
| 50 |
text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2)
|
| 51 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What to avoid in the image", lines=2)
|
| 52 |
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
steps = gr.Slider(label="Sampling steps", value=30, minimum=10, maximum=100, step=1)
|
| 55 |
+
cfg_scale = gr.Slider(label="CFG Scale", value=7.5, minimum=1, maximum=20, step=0.5)
|
| 56 |
+
with gr.Column():
|
| 57 |
+
strength = gr.Slider(label="LoRA Strength", value=0.75, minimum=0, maximum=1, step=0.01)
|
| 58 |
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
+
generate_button = gr.Button("Generate", variant='primary')
|
| 62 |
with gr.Row():
|
| 63 |
image_output = gr.Image(type="pil", label="Generated Image", show_download_button=True)
|
| 64 |
with gr.Row():
|
| 65 |
+
seed_output = gr.Number(label="Seed Used")
|
| 66 |
|
| 67 |
+
gr.Examples(examples=examples, inputs=[text_prompt, negative_prompt])
|
| 68 |
|
| 69 |
+
generate_button.click(
|
| 70 |
+
generate_image,
|
| 71 |
+
inputs=[text_prompt, negative_prompt, steps, cfg_scale, seed, strength],
|
| 72 |
outputs=[image_output, seed_output]
|
| 73 |
)
|
| 74 |
|
| 75 |
+
app.launch()
|