Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -56,10 +56,10 @@ def save_images_with_unique_filenames(image_list, save_directory): | |
| 56 | 
             
                for image in image_list:
         | 
| 57 | 
             
                    unique_filename = f"{uuid.uuid4()}.png"
         | 
| 58 | 
             
                    file_path = os.path.join(save_directory, unique_filename)
         | 
| 59 | 
            -
             | 
| 60 | 
             
                    image.save(file_path)
         | 
| 61 | 
             
                    paths.append(file_path)
         | 
| 62 | 
            -
             | 
| 63 | 
             
                return paths
         | 
| 64 |  | 
| 65 | 
             
            def convert_to_centered_scale(num):
         | 
| @@ -68,7 +68,7 @@ def convert_to_centered_scale(num): | |
| 68 | 
             
                    end = num // 2
         | 
| 69 | 
             
                else:  # odd
         | 
| 70 | 
             
                    start = -(num // 2)
         | 
| 71 | 
            -
                    end = num // 2 | 
| 72 | 
             
                return tuple(range(start, end + 1))
         | 
| 73 |  | 
| 74 | 
             
            def translate_if_korean(text):
         | 
| @@ -84,41 +84,41 @@ def generate(prompt, | |
| 84 | 
             
                         randomize_seed=True,
         | 
| 85 | 
             
                         seed=42,
         | 
| 86 | 
             
                         recalc_directions=True,
         | 
| 87 | 
            -
                         iterations=200, | 
| 88 | 
            -
                         steps=3, | 
| 89 | 
            -
                         interm_steps=33, | 
| 90 | 
             
                         guidance_scale=3.5,
         | 
| 91 | 
            -
                         x_concept_1="", x_concept_2="", | 
| 92 | 
            -
                         avg_diff_x=None, | 
| 93 | 
             
                         total_images=[],
         | 
| 94 | 
            -
                          | 
| 95 | 
             
                ):
         | 
| 96 | 
             
                # Translate prompt and concepts if Korean
         | 
| 97 | 
             
                prompt = translate_if_korean(prompt)
         | 
| 98 | 
             
                concept_1 = translate_if_korean(concept_1)
         | 
| 99 | 
             
                concept_2 = translate_if_korean(concept_2)
         | 
| 100 | 
            -
             | 
| 101 | 
             
                print(f"Prompt: {prompt}, ← {concept_2}, {concept_1} ➡️ . scale {scale}, interm steps {interm_steps}")
         | 
| 102 | 
             
                slider_x = [concept_2, concept_1]
         | 
| 103 | 
             
                # check if avg diff for directions need to be re-calculated
         | 
| 104 | 
             
                if randomize_seed:
         | 
| 105 | 
             
                        seed = random.randint(0, MAX_SEED)
         | 
| 106 | 
            -
             | 
| 107 | 
             
                if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]) or recalc_directions:
         | 
| 108 | 
            -
                     | 
| 109 | 
             
                    avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations)
         | 
| 110 | 
             
                    x_concept_1, x_concept_2 = slider_x[0], slider_x[1]
         | 
| 111 |  | 
| 112 | 
             
                images = []
         | 
| 113 | 
             
                high_scale = scale
         | 
| 114 | 
             
                low_scale = -1 * scale
         | 
| 115 | 
            -
                for i in  | 
| 116 | 
             
                    cur_scale = low_scale + (high_scale - low_scale) * i / (interm_steps - 1)
         | 
| 117 | 
            -
                    image = clip_slider.generate(prompt, | 
| 118 | 
             
                                                 width=768,
         | 
| 119 | 
             
                                                 height=768,
         | 
| 120 | 
            -
                                                 guidance_scale=guidance_scale, | 
| 121 | 
            -
                                                 scale=cur_scale,  seed=seed, num_inference_steps=steps, avg_diff=avg_diff) | 
| 122 | 
             
                    images.append(image)
         | 
| 123 | 
             
                canvas = Image.new('RGB', (256*interm_steps, 256))
         | 
| 124 | 
             
                for i, im in enumerate(images):
         | 
| @@ -138,25 +138,24 @@ def generate(prompt, | |
| 138 | 
             
                return x_concept_1,x_concept_2, avg_diff_x, export_to_video(images, video_path, fps=5), canvas, images, images[scale_middle], post_generation_slider_update, seed
         | 
| 139 |  | 
| 140 | 
             
            def update_pre_generated_images(slider_value, total_images):
         | 
| 141 | 
            -
                number_images = len(total_images)
         | 
| 142 | 
             
                if(number_images > 0):
         | 
| 143 | 
             
                    scale_tuple = convert_to_centered_scale(number_images)
         | 
| 144 | 
             
                    return total_images[scale_tuple.index(slider_value)][0]
         | 
| 145 | 
             
                else:
         | 
| 146 | 
             
                    return None
         | 
| 147 | 
            -
             | 
| 148 | 
             
            def reset_recalc_directions():
         | 
| 149 | 
             
                return True
         | 
| 150 |  | 
| 151 | 
             
            # Updated examples with English text
         | 
| 152 | 
             
            examples = [
         | 
| 153 | 
             
                ["flower in mountain", "spring", "winter", 1.5],
         | 
| 154 | 
            -
                ["a tomato", "super fresh", "rotten", 2.5], | 
| 155 | 
             
                ["여자", "아기", "노인", 2.5]
         | 
| 156 | 
             
            ]
         | 
| 157 |  | 
| 158 |  | 
| 159 | 
            -
             | 
| 160 | 
             
            css = """
         | 
| 161 | 
             
            footer {
         | 
| 162 | 
             
                visibility: hidden;
         | 
| @@ -201,7 +200,7 @@ footer { | |
| 201 | 
             
            with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
         | 
| 202 | 
             
                x_concept_1 = gr.State("")
         | 
| 203 | 
             
                x_concept_2 = gr.State("")
         | 
| 204 | 
            -
                total_images = gr. | 
| 205 | 
             
                avg_diff_x = gr.State()
         | 
| 206 | 
             
                recalc_directions = gr.State(False)
         | 
| 207 |  | 
| @@ -230,7 +229,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo: | |
| 230 | 
             
                                            info="Final state",
         | 
| 231 | 
             
                                            placeholder="summer"
         | 
| 232 | 
             
                                        )
         | 
| 233 | 
            -
             | 
| 234 | 
             
                                with gr.Row(elem_classes="slider-container"):
         | 
| 235 | 
             
                                    x = gr.Slider(
         | 
| 236 | 
             
                                        minimum=0,
         | 
| @@ -240,7 +239,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo: | |
| 240 | 
             
                                        label=english_labels["Strength"],
         | 
| 241 | 
             
                                        info="Maximum strength for each direction (above 2.5 may be unstable)"
         | 
| 242 | 
             
                                    )
         | 
| 243 | 
            -
             | 
| 244 | 
             
                                submit = gr.Button(english_labels["Generate directions"], size="lg", variant="primary")
         | 
| 245 |  | 
| 246 | 
             
                        # Advanced Options Panel
         | 
| @@ -262,7 +261,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo: | |
| 262 | 
             
                                        step=0.1,
         | 
| 263 | 
             
                                        value=3.5
         | 
| 264 | 
             
                                    )
         | 
| 265 | 
            -
             | 
| 266 | 
             
                            with gr.Row():
         | 
| 267 | 
             
                                with gr.Column(scale=1):
         | 
| 268 | 
             
                                    iterations = gr.Slider(
         | 
| @@ -280,7 +279,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo: | |
| 280 | 
             
                                        maximum=4,
         | 
| 281 | 
             
                                        step=1
         | 
| 282 | 
             
                                    )
         | 
| 283 | 
            -
             | 
| 284 | 
             
                            with gr.Row():
         | 
| 285 | 
             
                                with gr.Column(scale=1):
         | 
| 286 | 
             
                                    randomize_seed = gr.Checkbox(
         | 
|  | |
| 56 | 
             
                for image in image_list:
         | 
| 57 | 
             
                    unique_filename = f"{uuid.uuid4()}.png"
         | 
| 58 | 
             
                    file_path = os.path.join(save_directory, unique_filename)
         | 
| 59 | 
            +
             | 
| 60 | 
             
                    image.save(file_path)
         | 
| 61 | 
             
                    paths.append(file_path)
         | 
| 62 | 
            +
             | 
| 63 | 
             
                return paths
         | 
| 64 |  | 
| 65 | 
             
            def convert_to_centered_scale(num):
         | 
|  | |
| 68 | 
             
                    end = num // 2
         | 
| 69 | 
             
                else:  # odd
         | 
| 70 | 
             
                    start = -(num // 2)
         | 
| 71 | 
            +
                    end = num // 2
         | 
| 72 | 
             
                return tuple(range(start, end + 1))
         | 
| 73 |  | 
| 74 | 
             
            def translate_if_korean(text):
         | 
|  | |
| 84 | 
             
                         randomize_seed=True,
         | 
| 85 | 
             
                         seed=42,
         | 
| 86 | 
             
                         recalc_directions=True,
         | 
| 87 | 
            +
                         iterations=200,
         | 
| 88 | 
            +
                         steps=3,
         | 
| 89 | 
            +
                         interm_steps=33,
         | 
| 90 | 
             
                         guidance_scale=3.5,
         | 
| 91 | 
            +
                         x_concept_1="", x_concept_2="",
         | 
| 92 | 
            +
                         avg_diff_x=None,
         | 
| 93 | 
             
                         total_images=[],
         | 
| 94 | 
            +
                         gradio_progress=gr.Progress() # Changed progress to gradio_progress
         | 
| 95 | 
             
                ):
         | 
| 96 | 
             
                # Translate prompt and concepts if Korean
         | 
| 97 | 
             
                prompt = translate_if_korean(prompt)
         | 
| 98 | 
             
                concept_1 = translate_if_korean(concept_1)
         | 
| 99 | 
             
                concept_2 = translate_if_korean(concept_2)
         | 
| 100 | 
            +
             | 
| 101 | 
             
                print(f"Prompt: {prompt}, ← {concept_2}, {concept_1} ➡️ . scale {scale}, interm steps {interm_steps}")
         | 
| 102 | 
             
                slider_x = [concept_2, concept_1]
         | 
| 103 | 
             
                # check if avg diff for directions need to be re-calculated
         | 
| 104 | 
             
                if randomize_seed:
         | 
| 105 | 
             
                        seed = random.randint(0, MAX_SEED)
         | 
| 106 | 
            +
             | 
| 107 | 
             
                if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]) or recalc_directions:
         | 
| 108 | 
            +
                    gradio_progress(0, desc="Calculating directions...") # Changed progress to gradio_progress
         | 
| 109 | 
             
                    avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations)
         | 
| 110 | 
             
                    x_concept_1, x_concept_2 = slider_x[0], slider_x[1]
         | 
| 111 |  | 
| 112 | 
             
                images = []
         | 
| 113 | 
             
                high_scale = scale
         | 
| 114 | 
             
                low_scale = -1 * scale
         | 
| 115 | 
            +
                for i in gradio_progress.tqdm(range(interm_steps), desc="Generating images"): # Changed progress to gradio_progress
         | 
| 116 | 
             
                    cur_scale = low_scale + (high_scale - low_scale) * i / (interm_steps - 1)
         | 
| 117 | 
            +
                    image = clip_slider.generate(prompt,
         | 
| 118 | 
             
                                                 width=768,
         | 
| 119 | 
             
                                                 height=768,
         | 
| 120 | 
            +
                                                 guidance_scale=guidance_scale,
         | 
| 121 | 
            +
                                                 scale=cur_scale,  seed=seed, num_inference_steps=steps, avg_diff=avg_diff)
         | 
| 122 | 
             
                    images.append(image)
         | 
| 123 | 
             
                canvas = Image.new('RGB', (256*interm_steps, 256))
         | 
| 124 | 
             
                for i, im in enumerate(images):
         | 
|  | |
| 138 | 
             
                return x_concept_1,x_concept_2, avg_diff_x, export_to_video(images, video_path, fps=5), canvas, images, images[scale_middle], post_generation_slider_update, seed
         | 
| 139 |  | 
| 140 | 
             
            def update_pre_generated_images(slider_value, total_images):
         | 
| 141 | 
            +
                number_images = len(total_images) # This line was causing error when total_images was None.
         | 
| 142 | 
             
                if(number_images > 0):
         | 
| 143 | 
             
                    scale_tuple = convert_to_centered_scale(number_images)
         | 
| 144 | 
             
                    return total_images[scale_tuple.index(slider_value)][0]
         | 
| 145 | 
             
                else:
         | 
| 146 | 
             
                    return None
         | 
| 147 | 
            +
             | 
| 148 | 
             
            def reset_recalc_directions():
         | 
| 149 | 
             
                return True
         | 
| 150 |  | 
| 151 | 
             
            # Updated examples with English text
         | 
| 152 | 
             
            examples = [
         | 
| 153 | 
             
                ["flower in mountain", "spring", "winter", 1.5],
         | 
| 154 | 
            +
                ["a tomato", "super fresh", "rotten", 2.5],
         | 
| 155 | 
             
                ["여자", "아기", "노인", 2.5]
         | 
| 156 | 
             
            ]
         | 
| 157 |  | 
| 158 |  | 
|  | |
| 159 | 
             
            css = """
         | 
| 160 | 
             
            footer {
         | 
| 161 | 
             
                visibility: hidden;
         | 
|  | |
| 200 | 
             
            with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
         | 
| 201 | 
             
                x_concept_1 = gr.State("")
         | 
| 202 | 
             
                x_concept_2 = gr.State("")
         | 
| 203 | 
            +
                total_images = gr.State([]) # Initialize total_images as an empty list state
         | 
| 204 | 
             
                avg_diff_x = gr.State()
         | 
| 205 | 
             
                recalc_directions = gr.State(False)
         | 
| 206 |  | 
|  | |
| 229 | 
             
                                            info="Final state",
         | 
| 230 | 
             
                                            placeholder="summer"
         | 
| 231 | 
             
                                        )
         | 
| 232 | 
            +
             | 
| 233 | 
             
                                with gr.Row(elem_classes="slider-container"):
         | 
| 234 | 
             
                                    x = gr.Slider(
         | 
| 235 | 
             
                                        minimum=0,
         | 
|  | |
| 239 | 
             
                                        label=english_labels["Strength"],
         | 
| 240 | 
             
                                        info="Maximum strength for each direction (above 2.5 may be unstable)"
         | 
| 241 | 
             
                                    )
         | 
| 242 | 
            +
             | 
| 243 | 
             
                                submit = gr.Button(english_labels["Generate directions"], size="lg", variant="primary")
         | 
| 244 |  | 
| 245 | 
             
                        # Advanced Options Panel
         | 
|  | |
| 261 | 
             
                                        step=0.1,
         | 
| 262 | 
             
                                        value=3.5
         | 
| 263 | 
             
                                    )
         | 
| 264 | 
            +
             | 
| 265 | 
             
                            with gr.Row():
         | 
| 266 | 
             
                                with gr.Column(scale=1):
         | 
| 267 | 
             
                                    iterations = gr.Slider(
         | 
|  | |
| 279 | 
             
                                        maximum=4,
         | 
| 280 | 
             
                                        step=1
         | 
| 281 | 
             
                                    )
         | 
| 282 | 
            +
             | 
| 283 | 
             
                            with gr.Row():
         | 
| 284 | 
             
                                with gr.Column(scale=1):
         | 
| 285 | 
             
                                    randomize_seed = gr.Checkbox(
         | 
