| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  | import math | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | import gradio as gr | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  | import modules.scripts as scripts | 
					
						
							|  |  |  | from modules import deepbooru, images, processing, shared | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | from modules.processing import Processed | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  | from modules.shared import opts, state | 
					
						
							| 
									
										
										
										
											2023-01-05 09:29:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Script(scripts.Script): | 
					
						
							|  |  |  |     def title(self): | 
					
						
							|  |  |  |         return "Loopback" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def show(self, is_img2img): | 
					
						
							|  |  |  |         return is_img2img | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-11 18:28:15 +03:00
										 |  |  |     def ui(self, is_img2img): | 
					
						
							| 
									
										
										
										
											2023-01-05 09:29:07 +01:00
										 |  |  |         loops = gr.Slider(minimum=1, maximum=32, step=1, label='Loops', value=4, elem_id=self.elem_id("loops")) | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |         final_denoising_strength = gr.Slider(minimum=0, maximum=1, step=0.01, label='Final denoising strength', value=0.5, elem_id=self.elem_id("final_denoising_strength")) | 
					
						
							|  |  |  |         denoising_curve = gr.Dropdown(label="Denoising strength curve", choices=["Aggressive", "Linear", "Lazy"], value="Linear") | 
					
						
							| 
									
										
										
										
											2023-02-06 00:28:31 +03:00
										 |  |  |         append_interrogation = gr.Dropdown(label="Append interrogated prompt at each iteration", choices=["None", "CLIP", "DeepBooru"], value="None") | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |         return [loops, final_denoising_strength, denoising_curve, append_interrogation] | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |     def run(self, p, loops, final_denoising_strength, denoising_curve, append_interrogation): | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  |         processing.fix_seed(p) | 
					
						
							|  |  |  |         batch_count = p.n_iter | 
					
						
							|  |  |  |         p.extra_generation_params = { | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |             "Final denoising strength": final_denoising_strength, | 
					
						
							|  |  |  |             "Denoising curve": denoising_curve | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         p.batch_size = 1 | 
					
						
							|  |  |  |         p.n_iter = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |         info = None | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  |         initial_seed = None | 
					
						
							|  |  |  |         initial_info = None | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |         initial_denoising_strength = p.denoising_strength | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         grids = [] | 
					
						
							|  |  |  |         all_images = [] | 
					
						
							| 
									
										
										
										
											2022-10-11 11:23:47 +01:00
										 |  |  |         original_init_image = p.init_images | 
					
						
							| 
									
										
										
										
											2023-02-06 00:28:31 +03:00
										 |  |  |         original_prompt = p.prompt | 
					
						
							| 
									
										
										
										
											2023-03-23 10:44:25 -04:00
										 |  |  |         original_inpainting_fill = p.inpainting_fill | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  |         state.job_count = loops * batch_count | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-17 18:18:30 -04:00
										 |  |  |         initial_color_corrections = [processing.setup_color_correction(p.init_images[0])] | 
					
						
							| 
									
										
										
										
											2022-09-16 08:33:47 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |         def calculate_denoising_strength(loop): | 
					
						
							|  |  |  |             strength = initial_denoising_strength | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if loops == 1: | 
					
						
							|  |  |  |                 return strength | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             progress = loop / (loops - 1) | 
					
						
							| 
									
										
										
										
											2023-03-25 14:48:22 -03:00
										 |  |  |             if denoising_curve == "Aggressive": | 
					
						
							|  |  |  |                 strength = math.sin((progress) * math.pi * 0.5) | 
					
						
							|  |  |  |             elif denoising_curve == "Lazy": | 
					
						
							|  |  |  |                 strength = 1 - math.cos((progress) * math.pi * 0.5) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 strength = progress | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             change = (final_denoising_strength - initial_denoising_strength) * strength | 
					
						
							|  |  |  |             return initial_denoising_strength + change | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |         history = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for n in range(batch_count): | 
					
						
							| 
									
										
										
										
											2022-10-11 11:23:47 +01:00
										 |  |  |             # Reset to original init image at the start of each batch | 
					
						
							|  |  |  |             p.init_images = original_init_image | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |             # Reset to original denoising strength | 
					
						
							|  |  |  |             p.denoising_strength = initial_denoising_strength | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             last_image = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  |             for i in range(loops): | 
					
						
							|  |  |  |                 p.n_iter = 1 | 
					
						
							|  |  |  |                 p.batch_size = 1 | 
					
						
							|  |  |  |                 p.do_not_save_grid = True | 
					
						
							| 
									
										
										
										
											2022-09-17 18:20:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if opts.img2img_color_correction: | 
					
						
							|  |  |  |                     p.color_corrections = initial_color_corrections | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 00:28:31 +03:00
										 |  |  |                 if append_interrogation != "None": | 
					
						
							| 
									
										
										
										
											2023-05-09 22:17:58 +03:00
										 |  |  |                     p.prompt = f"{original_prompt}, " if original_prompt else "" | 
					
						
							| 
									
										
										
										
											2023-02-06 00:28:31 +03:00
										 |  |  |                     if append_interrogation == "CLIP": | 
					
						
							|  |  |  |                         p.prompt += shared.interrogator.interrogate(p.init_images[0]) | 
					
						
							|  |  |  |                     elif append_interrogation == "DeepBooru": | 
					
						
							|  |  |  |                         p.prompt += deepbooru.model.tag(p.init_images[0]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  |                 state.job = f"Iteration {i + 1}/{loops}, batch {n + 1}/{batch_count}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 processed = processing.process_images(p) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |                 # Generation cancelled. | 
					
						
							| 
									
										
										
										
											2024-01-01 16:50:59 +03:00
										 |  |  |                 if state.interrupted or state.stopping_generation: | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |                     break | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  |                 if initial_seed is None: | 
					
						
							|  |  |  |                     initial_seed = processed.seed | 
					
						
							|  |  |  |                     initial_info = processed.info | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 p.seed = processed.seed + 1 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |                 p.denoising_strength = calculate_denoising_strength(i + 1) | 
					
						
							| 
									
										
										
										
											2023-05-11 18:28:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |                 if state.skipped: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 last_image = processed.images[0] | 
					
						
							|  |  |  |                 p.init_images = [last_image] | 
					
						
							| 
									
										
										
										
											2023-03-23 10:44:25 -04:00
										 |  |  |                 p.inpainting_fill = 1 # Set "masked content" to "original" for next loop. | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if batch_count == 1: | 
					
						
							|  |  |  |                     history.append(last_image) | 
					
						
							|  |  |  |                     all_images.append(last_image) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if batch_count > 1 and not state.skipped and not state.interrupted: | 
					
						
							|  |  |  |                 history.append(last_image) | 
					
						
							|  |  |  |                 all_images.append(last_image) | 
					
						
							| 
									
										
										
										
											2023-03-23 10:44:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             p.inpainting_fill = original_inpainting_fill | 
					
						
							| 
									
										
										
										
											2023-05-11 18:28:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 16:50:59 +03:00
										 |  |  |             if state.interrupted or state.stopping_generation: | 
					
						
							| 
									
										
										
										
											2023-10-16 14:12:18 +08:00
										 |  |  |                 break | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if len(history) > 1: | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  |             grid = images.image_grid(history, rows=1) | 
					
						
							|  |  |  |             if opts.grid_save: | 
					
						
							|  |  |  |                 images.save_image(grid, p.outpath_grids, "grid", initial_seed, p.prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename, grid=True, p=p) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |             if opts.return_grid: | 
					
						
							|  |  |  |                 grids.append(grid) | 
					
						
							| 
									
										
										
										
											2023-05-11 18:28:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 21:07:33 -04:00
										 |  |  |         all_images = grids + all_images | 
					
						
							| 
									
										
										
										
											2022-09-14 14:47:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         processed = Processed(p, all_images, initial_seed, initial_info) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return processed |