codermert commited on
Commit
82cfdb8
·
verified ·
1 Parent(s): b2aae7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -38
app.py CHANGED
@@ -1,72 +1,75 @@
1
  import gradio as gr
2
- import replicate
 
 
3
  import random
4
- import os
5
 
6
- def query(prompt, aspect_ratio="1:1", steps=28, cfg_scale=3.5, seed=-1, strength=0.95):
 
 
 
 
 
 
 
 
7
  if seed == -1:
8
  seed = random.randint(1, 1000000000)
9
 
10
- input = {
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
- output = replicate.run(
23
- "lucataco/flux-dev-lora:a22c463f11808638ad5e2ebd582e07a469031f48dd567366fb4c6fdab91d614d",
24
- input=input
25
- )
26
-
27
- print(output)
28
- return output[0], seed
 
 
 
29
 
30
  css = """
31
  #app-container {
32
- max-width: 600px;
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 Image Generator</h1></center>")
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.Accordion("Advanced Settings", open=False):
51
- aspect_ratio = gr.Radio(label="Aspect ratio", value="1:1", choices=["1:1", "4:5", "2:3", "3:4","9:16", "4:3", "16:9"])
52
- steps = gr.Slider(label="Sampling steps", value=28, minimum=1, maximum=100, step=1)
53
- cfg = gr.Slider(label="CFG Scale", value=3.5, minimum=1, maximum=20, step=0.5)
54
- strength = gr.Slider(label="Strength", value=0.95, minimum=0, maximum=1, step=0.001)
55
  seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
56
 
57
  with gr.Row():
58
- text_button = gr.Button("Generate", variant='primary')
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.Textbox(label="Seed Used", show_copy_button=True)
63
 
64
- gr.Examples(examples=examples, inputs=[text_prompt])
65
 
66
- text_button.click(
67
- query,
68
- inputs=[text_prompt, aspect_ratio, steps, cfg, seed, strength],
69
  outputs=[image_output, seed_output]
70
  )
71
 
72
- app.launch(show_api=False)
 
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()