Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	added zero decorators
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -9,6 +9,7 @@ import os 
     | 
|
| 9 | 
         
             
            import sys
         
     | 
| 10 | 
         
             
            import json # Added import
         
     | 
| 11 | 
         
             
            import copy
         
     | 
| 
         | 
|
| 12 | 
         
             
            # Add project root to sys.path to allow direct import of var_post_samp
         
     | 
| 13 | 
         
             
            project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "."))
         
     | 
| 14 | 
         
             
            if project_root not in sys.path:
         
     | 
| 
         @@ -92,6 +93,7 @@ def save_configuration(image_editor_data, image_input, prompt, seed_val, task, r 
     | 
|
| 92 | 
         
             
                except Exception as e:
         
     | 
| 93 | 
         
             
                    return gr.Markdown(f"""<p style='color:red;'>Error saving configuration: {str(e)}</p>""")
         
     | 
| 94 | 
         | 
| 
         | 
|
| 95 | 
         
             
            def embed_prompt(prompt, device):
         
     | 
| 96 | 
         
             
                print(f"Generating prompt embeddings for: {prompt}")
         
     | 
| 97 | 
         
             
                with torch.no_grad(): # Add torch.no_grad() here
         
     | 
| 
         @@ -264,6 +266,7 @@ def postprocess_image(image_tensor): 
     | 
|
| 264 | 
         
             
                pil_image = TF.to_pil_image(image_tensor)
         
     | 
| 265 | 
         
             
                return pil_image
         
     | 
| 266 | 
         | 
| 
         | 
|
| 267 | 
         
             
            def inpaint_image(image_editor_output, prompt_text, fixed_seed_value, use_random_seed, guidance_scale, num_steps): # MODIFIED: seed_input changed to fixed_seed_value, use_random_seed
         
     | 
| 268 | 
         
             
                try:
         
     | 
| 269 | 
         
             
                    if image_editor_output is None:
         
     | 
| 
         @@ -364,6 +367,7 @@ def inpaint_image(image_editor_output, prompt_text, fixed_seed_value, use_random 
     | 
|
| 364 | 
         
             
                    # Return a more user-friendly error message to Gradio
         
     | 
| 365 | 
         
             
                    raise gr.Error(f"An error occurred: {str(e)}. Check console for details.")
         
     | 
| 366 | 
         | 
| 
         | 
|
| 367 | 
         
             
            def super_resolution_image(lr_image, prompt_text, fixed_seed_value, use_random_seed, guidance_scale, num_steps, sr_scale_factor, downscale_input):
         
     | 
| 368 | 
         
             
                try:
         
     | 
| 369 | 
         
             
                    if lr_image is None:
         
     | 
| 
         @@ -728,55 +732,20 @@ Use the slider to compare the low resolution input image with the super-resolved 
     | 
|
| 728 | 
         | 
| 729 | 
         
             
                    gr.Markdown("---") # Separator
         
     | 
| 730 | 
         
             
                    gr.Markdown("### Click an example to load:")
         
     | 
| 731 | 
         
            -
             
     | 
| 732 | 
         
            -
                        for ex in example_list_inp:
         
     | 
| 733 | 
         
            -
                            if ex[0]["background"] == input_data:
         
     | 
| 734 | 
         
            -
                                prompt_value, task, num_steps = ex[1], ex[2], ex[3]
         
     | 
| 735 | 
         
            -
                        # Load inpainting example into ImageEditor
         
     | 
| 736 | 
         
            -
                        image_editor.clear()
         
     | 
| 737 | 
         
            -
                        if input_data and input_data.get("background"):  
         
     | 
| 738 | 
         
            -
                            image_editor.upload_image(input_data["background"])
         
     | 
| 739 | 
         
            -
                        if input_data and input_data.get("layers"):
         
     | 
| 740 | 
         
            -
                            for layer in input_data["layers"]:
         
     | 
| 741 | 
         
            -
                                image_editor.upload_mask(layer)
         
     | 
| 742 | 
         
            -
                        # Update other UI controls
         
     | 
| 743 | 
         
            -
                        prompt_text.value = prompt_value
         
     | 
| 744 | 
         
            -
                        task_selector.value = task
         
     | 
| 745 | 
         
            -
                        num_steps_slider.value = num_steps
         
     | 
| 746 | 
         
            -
                        seed_slider.value = random.randint(0, 2**32 - 1)
         
     | 
| 747 | 
         
            -
                        guidance_scale_slider.value = default_guidance_scale
         
     | 
| 748 | 
         
            -
                        update_visibility(task)
         
     | 
| 749 | 
         
            -
             
     | 
| 750 | 
         
            -
                    def load_sr_example(input_data):
         
     | 
| 751 | 
         
            -
                        for ex in example_list_sr:
         
     | 
| 752 | 
         
            -
                            if ex[0] == input_data:
         
     | 
| 753 | 
         
            -
                                prompt_value, task, num_steps = ex[1], ex[2], ex[3]
         
     | 
| 754 | 
         
            -
                        # Load super-resolution example into Image component
         
     | 
| 755 | 
         
            -
                        image_input.clear()
         
     | 
| 756 | 
         
            -
                        image_input.upload_image(input_data)
         
     | 
| 757 | 
         
            -
                        # Update other UI controls
         
     | 
| 758 | 
         
            -
                        prompt_text.value = prompt_value
         
     | 
| 759 | 
         
            -
                        task_selector.value = task
         
     | 
| 760 | 
         
            -
                        num_steps_slider.value = num_steps
         
     | 
| 761 | 
         
            -
                        seed_slider.value = random.randint(0, 2**32 - 1)
         
     | 
| 762 | 
         
            -
                        guidance_scale_slider.value = default_guidance_scale
         
     | 
| 763 | 
         
            -
                        update_visibility(task)
         
     | 
| 764 | 
         
            -
                    # Examples for both tasks, loading via load_example (mutates components directly)
         
     | 
| 765 | 
         
             
                    with gr.Row():
         
     | 
| 766 | 
         
             
                        gr.Examples(
         
     | 
| 767 | 
         
             
                            examples=example_list_sr,
         
     | 
| 768 | 
         
             
                            inputs=[image_input, prompt_text, task_selector, num_steps_slider],
         
     | 
| 769 | 
         
            -
                            fn=load_sr_example,
         
     | 
| 770 | 
         
             
                            label="Super Resolution Examples",
         
     | 
| 771 | 
         
            -
                            cache_examples= 
     | 
| 772 | 
         
             
                        )
         
     | 
| 773 | 
         
             
                    with gr.Row():
         
     | 
| 774 | 
         
             
                        gr.Examples(
         
     | 
| 775 | 
         
             
                            examples=example_list_inp,
         
     | 
| 776 | 
         
             
                            inputs=[image_editor, prompt_text, task_selector, num_steps_slider],
         
     | 
| 777 | 
         
            -
                            fn=load_inp_example,
         
     | 
| 778 | 
         
             
                            label="Inpainting Examples",
         
     | 
| 779 | 
         
            -
                            cache_examples= 
     | 
| 780 | 
         
             
                        )
         
     | 
| 781 | 
         | 
| 782 | 
         | 
| 
         | 
|
| 9 | 
         
             
            import sys
         
     | 
| 10 | 
         
             
            import json # Added import
         
     | 
| 11 | 
         
             
            import copy
         
     | 
| 12 | 
         
            +
            import spaces
         
     | 
| 13 | 
         
             
            # Add project root to sys.path to allow direct import of var_post_samp
         
     | 
| 14 | 
         
             
            project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "."))
         
     | 
| 15 | 
         
             
            if project_root not in sys.path:
         
     | 
| 
         | 
|
| 93 | 
         
             
                except Exception as e:
         
     | 
| 94 | 
         
             
                    return gr.Markdown(f"""<p style='color:red;'>Error saving configuration: {str(e)}</p>""")
         
     | 
| 95 | 
         | 
| 96 | 
         
            +
            @spaces.GPU
         
     | 
| 97 | 
         
             
            def embed_prompt(prompt, device):
         
     | 
| 98 | 
         
             
                print(f"Generating prompt embeddings for: {prompt}")
         
     | 
| 99 | 
         
             
                with torch.no_grad(): # Add torch.no_grad() here
         
     | 
| 
         | 
|
| 266 | 
         
             
                pil_image = TF.to_pil_image(image_tensor)
         
     | 
| 267 | 
         
             
                return pil_image
         
     | 
| 268 | 
         | 
| 269 | 
         
            +
            @spaces.GPU
         
     | 
| 270 | 
         
             
            def inpaint_image(image_editor_output, prompt_text, fixed_seed_value, use_random_seed, guidance_scale, num_steps): # MODIFIED: seed_input changed to fixed_seed_value, use_random_seed
         
     | 
| 271 | 
         
             
                try:
         
     | 
| 272 | 
         
             
                    if image_editor_output is None:
         
     | 
| 
         | 
|
| 367 | 
         
             
                    # Return a more user-friendly error message to Gradio
         
     | 
| 368 | 
         
             
                    raise gr.Error(f"An error occurred: {str(e)}. Check console for details.")
         
     | 
| 369 | 
         | 
| 370 | 
         
            +
            @spaces.GPU
         
     | 
| 371 | 
         
             
            def super_resolution_image(lr_image, prompt_text, fixed_seed_value, use_random_seed, guidance_scale, num_steps, sr_scale_factor, downscale_input):
         
     | 
| 372 | 
         
             
                try:
         
     | 
| 373 | 
         
             
                    if lr_image is None:
         
     | 
| 
         | 
|
| 732 | 
         | 
| 733 | 
         
             
                    gr.Markdown("---") # Separator
         
     | 
| 734 | 
         
             
                    gr.Markdown("### Click an example to load:")
         
     | 
| 735 | 
         
            +
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 736 | 
         
             
                    with gr.Row():
         
     | 
| 737 | 
         
             
                        gr.Examples(
         
     | 
| 738 | 
         
             
                            examples=example_list_sr,
         
     | 
| 739 | 
         
             
                            inputs=[image_input, prompt_text, task_selector, num_steps_slider],
         
     | 
| 
         | 
|
| 740 | 
         
             
                            label="Super Resolution Examples",
         
     | 
| 741 | 
         
            +
                            cache_examples=False
         
     | 
| 742 | 
         
             
                        )
         
     | 
| 743 | 
         
             
                    with gr.Row():
         
     | 
| 744 | 
         
             
                        gr.Examples(
         
     | 
| 745 | 
         
             
                            examples=example_list_inp,
         
     | 
| 746 | 
         
             
                            inputs=[image_editor, prompt_text, task_selector, num_steps_slider],
         
     | 
| 
         | 
|
| 747 | 
         
             
                            label="Inpainting Examples",
         
     | 
| 748 | 
         
            +
                            cache_examples=False
         
     | 
| 749 | 
         
             
                        )
         
     | 
| 750 | 
         | 
| 751 | 
         | 
    	
        demo_images/{demo_1_image.png β demo_4_image.png}
    RENAMED
    
    | 
										 
											File without changes
										 
									 | 
								
    	
        demo_images/{demo_1_mask.png β demo_4_mask.png}
    RENAMED
    
    | 
										 
											File without changes
										 
									 | 
								
    	
        demo_images/{demo_1_meta.json β demo_4_meta.json}
    RENAMED
    
    | 
         
            File without changes
         
     |