yash commited on
Commit
545d518
·
1 Parent(s): b8b2322

first commit

Browse files
Files changed (2) hide show
  1. app.py +173 -0
  2. requirements.txt +104 -0
app.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from diffusers import StableDiffusionControlNetPipeline
4
+ from diffusers import ControlNetModel, DDIMScheduler,EulerDiscreteScheduler,EulerAncestralDiscreteScheduler
5
+ from diffusers import KDPM2DiscreteScheduler,KDPM2AncestralDiscreteScheduler,PNDMScheduler,UniPCMultistepScheduler
6
+ from diffusers import DPMSolverMultistepScheduler
7
+ import random
8
+ import numpy as np
9
+ import cv2
10
+ from PIL import Image
11
+ from diffusers.utils import load_image
12
+
13
+
14
+
15
+ def canny_image(image,th1=100,th2=200):
16
+ image = np.array(image)
17
+ image = cv2.Canny(image,th1,th2)
18
+ image = image[:, :, None]
19
+ image = np.concatenate([image, image, image], axis=2)
20
+ canny_image = Image.fromarray(image)
21
+ return canny_image
22
+
23
+
24
+ def set_pipeline(model_id_repo,scheduler):
25
+
26
+ model_ids_dict = {
27
+ "runwayml": "runwayml/stable-diffusion-v1-5",
28
+ "Realistic_Vision_V5_1_noVAE":"SG161222/Realistic_Vision_V5.1_noVAE"
29
+ }
30
+ model_id = model_id_repo
31
+ model_repo = model_ids_dict.get(model_id)
32
+ print("model_repo :",model_repo)
33
+
34
+
35
+
36
+ # load control net and stable diffusion v1-5
37
+ controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_canny",
38
+ # torch_dtype=torch.float16
39
+ )
40
+
41
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
42
+ model_repo,
43
+ controlnet=controlnet,
44
+ # torch_dtype=torch.float16,
45
+ use_safetensors = True
46
+ ).to("cpu")
47
+
48
+
49
+
50
+
51
+ scheduler_classes = {
52
+ "DDIM": DDIMScheduler,
53
+ "Euler": EulerDiscreteScheduler,
54
+ "Euler a": EulerAncestralDiscreteScheduler,
55
+ "UniPC": UniPCMultistepScheduler,
56
+ "DPM2 Karras": KDPM2DiscreteScheduler,
57
+ "DPM2 a Karras": KDPM2AncestralDiscreteScheduler,
58
+ "PNDM": PNDMScheduler,
59
+ "DPM++ 2M Karras": DPMSolverMultistepScheduler,
60
+ "DPM++ 2M SDE Karras": DPMSolverMultistepScheduler,
61
+ }
62
+
63
+ sampler_name = scheduler # Example sampler name, replace with the actual value
64
+ scheduler_class = scheduler_classes.get(sampler_name)
65
+
66
+ if scheduler_class is not None:
67
+ print("sampler_name:",sampler_name)
68
+ pipe.scheduler = scheduler_class.from_config(pipe.scheduler.config)
69
+ else:
70
+ pass
71
+
72
+ return pipe
73
+
74
+
75
+ def img_args(
76
+ prompt,
77
+ negative_prompt,
78
+ image_canny,
79
+ controlnet_conditioning_scale = 1.0,
80
+ control_guidance_start=0.0,
81
+ control_guidance_end=1.0,
82
+ clip_skip=0,
83
+ model_id_repo = "Realistic_Vision_V5_1_noVAE",
84
+ scheduler= "Euler a",
85
+ num_inference_steps = 30,
86
+ guidance_scale = 7.5,
87
+ num_images_per_prompt = 1,
88
+ seed = 0
89
+ ):
90
+
91
+ controlnet_conditioning_scale = float(controlnet_conditioning_scale)
92
+
93
+ if image_canny is None:
94
+ return
95
+
96
+ pipe = set_pipeline(model_id_repo,scheduler)
97
+
98
+ if seed == -1:
99
+ seed = random.randint(0,2564798154)
100
+ print(f"random seed :{seed}")
101
+ generator = torch.manual_seed(seed)
102
+ else:
103
+ generator = torch.manual_seed(seed)
104
+ print(f"manual seed :{seed}")
105
+
106
+ print("Prompt:",prompt)
107
+ image = pipe(prompt=prompt,
108
+ negative_prompt = negative_prompt,
109
+ image=image_canny,
110
+ control_guidance_start = control_guidance_start,
111
+ control_guidance_end = control_guidance_end,
112
+ clip_skip =clip_skip,
113
+ num_inference_steps = num_inference_steps,
114
+ guidance_scale = guidance_scale,
115
+ num_images_per_prompt = num_images_per_prompt, # default 1
116
+ generator = generator,
117
+ controlnet_conditioning_scale = controlnet_conditioning_scale
118
+ ).images
119
+ return image
120
+
121
+
122
+ block = gr.Blocks().queue()
123
+ block.title = "Inpaint Anything"
124
+ with block as image_gen:
125
+ with gr.Column():
126
+ with gr.Row():
127
+ gr.Markdown("## Image Generation With Canny Controlnet")
128
+ with gr.Row():
129
+ with gr.Column():
130
+ # with gr.Row():
131
+ input_image = gr.Image(type="pil",label="Input")
132
+ prompt = gr.Textbox(placeholder="what you want to generate",label="Positive Prompt")
133
+ negative_prompt = gr.Textbox(placeholder="what you don't want to generate",label="Negative prompt")
134
+ with gr.Column():
135
+ canny_output = gr.Image(type="pil",label="Canny Input")
136
+ canny_btn = gr.Button("Canny Image", elem_id="select_btn", variant="primary")
137
+
138
+ with gr.Accordion(label="Controlnet Advance Options",open=False):
139
+ controlnet_conditioning_scale_slider = gr.Slider(label="Control Condition Scale", minimum=0.0, maximum=2.0, value=1.0, step=0.05)
140
+ control_guidance_start_slider = gr.Slider(label="Contron Guidance Start", minimum=0.0, maximum=1.0, value=0, step=0.1)
141
+ control_guidance_end_slider = gr.Slider(label="Contron Guidance Start End", minimum=0.0, maximum=1.0, value=1, step=0.1)
142
+ canny_th1 = gr.Slider(label="Canny High Threshold",minimum=0, maximum=300, value=100, step=1)
143
+ canny_th2 = gr.Slider(label="Canny Low Threshold",minimum=0, maximum=300, value=200, step=1)
144
+ with gr.Column():
145
+ out_img = gr.Gallery(label='Output', show_label=True, elem_id="gallery", preview=True)
146
+ run_btn = gr.Button("Generation", elem_id="select_btn", variant="primary")
147
+
148
+ with gr.Accordion(label="Generation Advance Options",open=False):
149
+ with gr.Row():
150
+ model_selection = gr.Dropdown(choices=["runwayml","Realistic_Vision_V5_1_noVAE"],value="Realistic_Vision_V5_1_noVAE",label="Models")
151
+ schduler_selection = gr.Dropdown(choices=["DDIM","Euler","Euler a","UniPC","DPM2 Karras","DPM2 a Karras","PNDM","DPM++ 2M Karras","DPM++ 2M SDE Karras"],value="Euler a",label="Scheduler")
152
+ guidance_scale_slider = gr.Slider(label="Guidance Scale", minimum=0, maximum=15, value=7.5, step=0.5)
153
+ num_images_per_prompt_slider = gr.Slider(label="num_images_per_prompt", minimum=0, maximum=5, value=1, step=1)
154
+ num_inference_steps_slider = gr.Slider(label="num_inference_steps", minimum=0, maximum=150, value=30, step=1)
155
+ seed_slider = gr.Slider(label="Seed", minimum=-1, maximum=256479815, value=-1, step=1)
156
+ clip_skip_slider = gr.Slider(label="Clip Skip", minimum=0, maximum=3, value=0, step=1)
157
+
158
+ canny_btn.click(fn=canny_image,inputs=[input_image,canny_th1,canny_th2],outputs=[canny_output])
159
+ run_btn.click(fn=img_args,inputs=[prompt,
160
+ negative_prompt,
161
+ canny_output,
162
+ controlnet_conditioning_scale_slider,
163
+ control_guidance_start_slider,
164
+ control_guidance_end_slider,
165
+ clip_skip_slider,
166
+ model_selection,
167
+ schduler_selection,
168
+ num_inference_steps_slider,
169
+ guidance_scale_slider,
170
+ num_images_per_prompt_slider,
171
+ seed_slider], outputs=[out_img])
172
+ image_gen.launch()
173
+
requirements.txt ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.30.1
2
+ aiofiles==23.2.1
3
+ altair==5.3.0
4
+ annotated-types==0.6.0
5
+ anyio==4.3.0
6
+ attrs==23.2.0
7
+ certifi==2024.2.2
8
+ charset-normalizer==3.3.2
9
+ click==8.1.7
10
+ contourpy==1.2.1
11
+ cycler==0.12.1
12
+ diffusers==0.27.2
13
+ dnspython==2.6.1
14
+ email_validator==2.1.1
15
+ exceptiongroup==1.2.1
16
+ fastapi==0.111.0
17
+ fastapi-cli==0.0.3
18
+ ffmpy==0.3.2
19
+ filelock==3.14.0
20
+ fonttools==4.51.0
21
+ fsspec==2024.3.1
22
+ gradio==3.50.2
23
+ gradio_client==0.6.1
24
+ h11==0.14.0
25
+ httpcore==1.0.5
26
+ httptools==0.6.1
27
+ httpx==0.27.0
28
+ huggingface-hub==0.23.0
29
+ idna==3.7
30
+ importlib_metadata==7.1.0
31
+ importlib_resources==6.4.0
32
+ inquirerpy==0.3.4
33
+ Jinja2==3.1.4
34
+ jsonschema==4.22.0
35
+ jsonschema-specifications==2023.12.1
36
+ kiwisolver==1.4.5
37
+ markdown-it-py==3.0.0
38
+ MarkupSafe==2.1.5
39
+ matplotlib==3.8.4
40
+ mdurl==0.1.2
41
+ mpmath==1.3.0
42
+ networkx==3.3
43
+ numpy==1.26.4
44
+ nvidia-cublas-cu12==12.1.3.1
45
+ nvidia-cuda-cupti-cu12==12.1.105
46
+ nvidia-cuda-nvrtc-cu12==12.1.105
47
+ nvidia-cuda-runtime-cu12==12.1.105
48
+ nvidia-cudnn-cu12==8.9.2.26
49
+ nvidia-cufft-cu12==11.0.2.54
50
+ nvidia-curand-cu12==10.3.2.106
51
+ nvidia-cusolver-cu12==11.4.5.107
52
+ nvidia-cusparse-cu12==12.1.0.106
53
+ nvidia-nccl-cu12==2.20.5
54
+ nvidia-nvjitlink-cu12==12.4.127
55
+ nvidia-nvtx-cu12==12.1.105
56
+ opencv-python==4.9.0.80
57
+ orjson==3.10.3
58
+ packaging==24.0
59
+ pandas==2.2.2
60
+ pfzy==0.3.4
61
+ pillow==10.3.0
62
+ prompt-toolkit==3.0.43
63
+ psutil==5.9.8
64
+ pydantic==2.7.1
65
+ pydantic_core==2.18.2
66
+ pydub==0.25.1
67
+ Pygments==2.18.0
68
+ pyparsing==3.1.2
69
+ python-dateutil==2.9.0.post0
70
+ python-dotenv==1.0.1
71
+ python-multipart==0.0.9
72
+ pytz==2024.1
73
+ PyYAML==6.0.1
74
+ referencing==0.35.1
75
+ regex==2024.5.10
76
+ requests==2.31.0
77
+ rich==13.7.1
78
+ rpds-py==0.18.1
79
+ safetensors==0.4.3
80
+ semantic-version==2.10.0
81
+ shellingham==1.5.4
82
+ six==1.16.0
83
+ sniffio==1.3.1
84
+ starlette==0.37.2
85
+ sympy==1.12
86
+ tokenizers==0.19.1
87
+ toolz==0.12.1
88
+ torch==2.3.0
89
+ torchvision==0.18.0
90
+ tqdm==4.66.4
91
+ transformers==4.40.2
92
+ triton==2.3.0
93
+ typer==0.12.3
94
+ typing_extensions==4.11.0
95
+ tzdata==2024.1
96
+ ujson==5.10.0
97
+ urllib3==2.2.1
98
+ uvicorn==0.29.0
99
+ uvloop==0.19.0
100
+ watchfiles==0.21.0
101
+ wcwidth==0.2.13
102
+ websockets==11.0.3
103
+ xformers==0.0.26.post1
104
+ zipp==3.18.1