sayakpaul HF staff commited on
Commit
bb21a7c
1 Parent(s): 237ed66

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
2
+ from diffusers import UniPCMultistepScheduler
3
+ from diffusers.utils import load_image
4
+ import gradio as gr
5
+ import torch
6
+
7
+ # Constants
8
+ low_threshold = 100
9
+ high_threshold = 200
10
+
11
+ # Models
12
+ pose_model = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
13
+ controlnet = ControlNetModel.from_pretrained(
14
+ "lllyasviel/sd-controlnet-openpose", torch_dtype=torch.float16
15
+ )
16
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
17
+ "andite/anything-v4.0", controlnet=controlnet, torch_dtype=torch.float16
18
+ )
19
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
20
+
21
+ # This command loads the individual model components on GPU on-demand. So, we don't
22
+ # need to explicitly call pipe.to("cuda").
23
+ pipe.enable_model_cpu_offload()
24
+
25
+ # Generator seed,
26
+ generator = torch.manual_seed(0)
27
+
28
+ def get_pose(image):
29
+ return pose_model(image)
30
+
31
+
32
+ def generate_images(image, prompt):
33
+ pose = get_pose(image)
34
+ output = pipe(
35
+ prompt,
36
+ pose,
37
+ generator=generator,
38
+ num_images_per_prompt=3
39
+ )
40
+ return output.images
41
+
42
+
43
+ gr.Interface(
44
+ generate_images,
45
+ inputs=[
46
+ gr.Image(type="pil"),
47
+ gr.Textbox(
48
+ label="Enter your prompt",
49
+ max_lines=1,
50
+ placeholder="best quality, extremely detailed",
51
+ ),
52
+ ],
53
+ outputs=gr.Gallery().style(grid=[2], height="auto"),
54
+ title="Generate controlled outputs with ControlNet and Stable Diffusion. ",
55
+ description="This Space uses pose estimated lines as the additional conditioning.",
56
+ examples=[["yoga1.jpeg", "best quality, extremely detailed"]],
57
+ allow_flagging=False,
58
+ ).launch(enable_queue=True)
59
+
60
+