John Leo commited on
Commit
36fda04
1 Parent(s): cc51d2f

Added dark mode and image size change option

Browse files
Files changed (1) hide show
  1. app.py +53 -20
app.py CHANGED
@@ -9,28 +9,61 @@ pipe = StableDiffusionXLPipeline.from_pretrained(model_id, torch_dtype=torch.flo
9
  pipe = pipe.to("cuda")
10
 
11
  @spaces.GPU()
12
- def text_to_image(prompt, negative_prompt, steps, guidance_scale, add_4k_masterpiece, progress=gr.Progress(track_tqdm=True)):
13
  if add_4k_masterpiece:
14
  prompt += ", 4k, (masterpiece)"
15
- image = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=steps, guidance_scale=guidance_scale).images[0]
16
  return image
 
17
  duplicate_button = gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
18
 
19
- gradio_interface = Interface(
20
- fn=text_to_image,
21
- allow_duplication=True,
22
- inputs=[
23
- gr.Textbox(label="Prompt", lines=2, placeholder="Enter your prompt here..."),
24
- gr.Textbox(label="Negative Prompt", lines=2, placeholder="What to exclude from the image..."),
25
- gr.Slider(minimum=1, maximum=65, value=50, label="Steps", step=1),
26
- gr.Slider(minimum=1, maximum=20, value=7.5, label="Guidance Scale", step=0.1),
27
- gr.Checkbox(label="Add recommended prompt items (4k, masterpiece)", value=False)
28
- ],
29
- outputs=Image(type="pil", show_download_button=True),
30
- examples=[
31
- ["magical kitten, 4k, high quality, (masterpiece)"],
32
- ],
33
- cache_examples=False,
34
- theme=gr.themes.Soft()
35
- )
36
- gradio_interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  pipe = pipe.to("cuda")
10
 
11
  @spaces.GPU()
12
+ def text_to_image(prompt, negative_prompt, steps, guidance_scale, add_4k_masterpiece, width, height, progress=gr.Progress(track_tqdm=True)):
13
  if add_4k_masterpiece:
14
  prompt += ", 4k, (masterpiece)"
15
+ image = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=steps, guidance_scale=guidance_scale, width=width, height=height).images[0]
16
  return image
17
+
18
  duplicate_button = gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
19
 
20
+ def set_aspect_ratio(ratio):
21
+ ratios = {
22
+ "1:1": (1024, 1024),
23
+ "4:3": (1366, 1024),
24
+ "16:9": (1920, 1080),
25
+ "3:2": (2048, 1365),
26
+ "5:4": (1280, 1024)
27
+ }
28
+ return ratios[ratio]
29
+
30
+ def update_aspect_ratio(ratio, width_slider, height_slider):
31
+ width, height = set_aspect_ratio(ratio)
32
+ width_slider.update(value=width)
33
+ height_slider.update(value=height)
34
+
35
+ with gr.Blocks() as gradio_interface:
36
+ gr.Markdown("## Stable Diffusion XL Interface")
37
+
38
+ dark_mode = gr.Checkbox(label="Dark Mode")
39
+ with gr.Row():
40
+ prompt = gr.Textbox(label="Prompt", lines=2, placeholder="Enter your prompt here...")
41
+ negative_prompt = gr.Textbox(label="Negative Prompt", lines=2, placeholder="What to exclude from the image...")
42
+
43
+ with gr.Row():
44
+ steps = gr.Slider(minimum=1, maximum=65, value=50, label="Steps", step=1)
45
+ guidance_scale = gr.Slider(minimum=1, maximum=20, value=7.5, label="Guidance Scale", step=0.1)
46
+
47
+ add_4k_masterpiece = gr.Checkbox(label="Add recommended prompt items (4k, masterpiece)", value=False)
48
+
49
+ with gr.Row():
50
+ width = gr.Slider(minimum=0, maximum=2048, value=1024, label="Width", step=1)
51
+ height = gr.Slider(minimum=0, maximum=2048, value=1024, label="Height", step=1)
52
+
53
+ aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["1:1", "4:3", "16:9", "3:2", "5:4"])
54
+
55
+ aspect_ratio.change(update_aspect_ratio, inputs=[aspect_ratio, width, height], outputs=[width, height])
56
+
57
+ generate_button = gr.Button("Generate Image")
58
+ output_image = gr.Image(type="pil", show_download_button=True)
59
+
60
+ generate_button.click(
61
+ fn=text_to_image,
62
+ inputs=[prompt, negative_prompt, steps, guidance_scale, add_4k_masterpiece, width, height],
63
+ outputs=output_image
64
+ )
65
+
66
+ if dark_mode:
67
+ gradio_interface.theme = gr.themes.Dark()
68
+
69
+ gradio_interface.launch()