Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,10 +1,10 @@ | |
| 1 | 
            -
             | 
|  | |
|  | |
| 2 | 
             
            from huggingface_hub import hf_hub_download
         | 
| 3 | 
             
            from safetensors.torch import load_file
         | 
| 4 | 
             
            import spaces
         | 
| 5 | 
            -
             | 
| 6 | 
            -
            import torch
         | 
| 7 | 
            -
            import PIL
         | 
| 8 |  | 
| 9 | 
             
            # Constants
         | 
| 10 | 
             
            base = "stabilityai/stable-diffusion-xl-base-1.0"
         | 
| @@ -23,8 +23,7 @@ CSS = """ | |
| 23 |  | 
| 24 | 
             
            # Ensure model and scheduler are initialized in GPU-enabled function
         | 
| 25 | 
             
            if torch.cuda.is_available():
         | 
| 26 | 
            -
                 | 
| 27 | 
            -
                pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
         | 
| 28 |  | 
| 29 |  | 
| 30 | 
             
            # Function 
         | 
| @@ -37,12 +36,13 @@ def generate_image(prompt, ckpt): | |
| 37 | 
             
                num_inference_steps = checkpoints[ckpt][1]
         | 
| 38 |  | 
| 39 | 
             
                if loaded != num_inference_steps:
         | 
| 40 | 
            -
                    pipe.unet.load_state_dict(torch.load(hf_hub_download(repo, checkpoint), map_location="cuda"))
         | 
| 41 | 
             
                    pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample" if num_inference_steps==1 else "epsilon")
         | 
|  | |
| 42 | 
             
                    loaded = num_inference_steps
         | 
| 43 |  | 
| 44 | 
             
                results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0)
         | 
| 45 |  | 
|  | |
| 46 | 
             
                return results.images[0]
         | 
| 47 |  | 
| 48 |  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            import torch
         | 
| 3 | 
            +
            from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
         | 
| 4 | 
             
            from huggingface_hub import hf_hub_download
         | 
| 5 | 
             
            from safetensors.torch import load_file
         | 
| 6 | 
             
            import spaces
         | 
| 7 | 
            +
            from PIL import Image
         | 
|  | |
|  | |
| 8 |  | 
| 9 | 
             
            # Constants
         | 
| 10 | 
             
            base = "stabilityai/stable-diffusion-xl-base-1.0"
         | 
|  | |
| 23 |  | 
| 24 | 
             
            # Ensure model and scheduler are initialized in GPU-enabled function
         | 
| 25 | 
             
            if torch.cuda.is_available():
         | 
| 26 | 
            +
                pipe = StableDiffusionXLPipeline.from_pretrained(base, torch_dtype=torch.float16, variant="fp16").to("cuda")
         | 
|  | |
| 27 |  | 
| 28 |  | 
| 29 | 
             
            # Function 
         | 
|  | |
| 36 | 
             
                num_inference_steps = checkpoints[ckpt][1]
         | 
| 37 |  | 
| 38 | 
             
                if loaded != num_inference_steps:
         | 
|  | |
| 39 | 
             
                    pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample" if num_inference_steps==1 else "epsilon")
         | 
| 40 | 
            +
                    pipe.unet.load_state_dict(load_file(hf_hub_download(repo, checkpoint), device="cuda"))
         | 
| 41 | 
             
                    loaded = num_inference_steps
         | 
| 42 |  | 
| 43 | 
             
                results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0)
         | 
| 44 |  | 
| 45 | 
            +
             | 
| 46 | 
             
                return results.images[0]
         | 
| 47 |  | 
| 48 |  | 

