ilanser commited on
Commit
4396e7f
1 Parent(s): e1e77a3

Update app.py

Browse files

update the interface to use Blocks for more customized GUI with examples

Files changed (1) hide show
  1. app.py +24 -9
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from PIL import Image
3
  import base64
4
  import io
 
5
  import cv2
6
  import numpy as np
7
  import torch
@@ -20,24 +21,38 @@ def predict(sketch, description):
20
  controlnet_id = "lllyasviel/sd-controlnet-scribble"
21
 
22
  # Load ControlNet model
23
- controlnet = ControlNetModel.from_pretrained(controlnet_id, torch_dtype=torch.float16)
24
 
25
  # Create pipeline with ControlNet model
26
- pipe = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=controlnet, torch_dtype=torch.float16)
27
 
28
  # Use improved scheduler
29
  pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
30
 
31
  # Enable smart CPU offloading and memory efficient attention
32
  # pipe.enable_model_cpu_offload()
33
- pipe.enable_xformers_memory_efficient_attention()
34
 
 
 
35
 
36
- result = pipe(description, image, num_inference_steps=20).images[0]
37
 
38
  return result
39
- # Define sketchpad with custom size and stroke width
40
- sketchpad = gr.Sketchpad(shape=(1024, 1024), brush_radius=5)
41
-
42
- iface = gr.Interface(fn=predict, inputs=[sketchpad, "text"], outputs="image", live=False)
43
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from PIL import Image
3
  import base64
4
  import io
5
+ import glob
6
  import cv2
7
  import numpy as np
8
  import torch
 
21
  controlnet_id = "lllyasviel/sd-controlnet-scribble"
22
 
23
  # Load ControlNet model
24
+ controlnet = ControlNetModel.from_pretrained(controlnet_id)
25
 
26
  # Create pipeline with ControlNet model
27
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=controlnet)
28
 
29
  # Use improved scheduler
30
  pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
31
 
32
  # Enable smart CPU offloading and memory efficient attention
33
  # pipe.enable_model_cpu_offload()
34
+ # pipe.enable_xformers_memory_efficient_attention()
35
 
36
+ # Move pipeline to GPU
37
+ pipe = pipe.to("cuda")
38
 
39
+ result = pipe(description, image, num_inference_steps=10).images[0]
40
 
41
  return result
42
+ with gr.Blocks() as iface:
43
+ # Define sketchpad with custom size and stroke width
44
+ sketchpad = gr.Sketchpad(shape=(400, 300), brush_radius=5, label="Sketchpad- Draw something")
45
+ txt= gr.Textbox(lines=3, label="Description - Describe your sketch with style")
46
+ im = gr.Image(label="Output Image", interactive=False)
47
+ button = gr.Button(value="Submit")
48
+ button.click(predict, inputs=[sketchpad, txt], outputs=im)
49
+ flag= gr.CSVLogger()
50
+ flag.setup([sketchpad, txt, im], "flagged_data_points")
51
+ button_flag = gr.Button(value="Flag")
52
+ button_flag.click(lambda *args: flag.flag(args), [sketchpad, txt, im], None, preprocess=False)
53
+
54
+ # iface = gr.Interface(fn=predict, inputs=[sketchpad, "text"], outputs=im, live=False, title="Sketch2Image")
55
+ ## get all the file path from flagged/sketch folder into a list
56
+ sketch_path = glob.glob("flagged/sketch/*.png")
57
+ gr.Examples(examples = list(map(lambda x: [x ,"draw in the style of crayon by kids"], sketch_path)), inputs=[sketchpad,txt], outputs=im, fn=predict, cache_examples=True)
58
+ iface.launch()