测试动漫模型
Browse files- app.py +78 -72
 - requirements.txt +3 -5
 
    	
        app.py
    CHANGED
    
    | 
         @@ -1,74 +1,85 @@ 
     | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 
         | 
|
| 2 | 
         
             
            import numpy as np
         
     | 
| 3 | 
         
             
            import random
         
     | 
| 4 | 
         
            -
             
     | 
| 5 | 
         
            -
            # import spaces #[uncomment to use ZeroGPU]
         
     | 
| 6 | 
         
            -
            from diffusers import DiffusionPipeline
         
     | 
| 7 | 
         
             
            import torch
         
     | 
| 8 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 9 | 
         
             
            device = "cuda" if torch.cuda.is_available() else "cpu"
         
     | 
| 10 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 11 | 
         | 
| 12 | 
         
            -
             
     | 
| 13 | 
         
            -
                 
     | 
| 14 | 
         
            -
             
     | 
| 15 | 
         
            -
                torch_dtype 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 16 | 
         | 
| 17 | 
         
            -
            pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
         
     | 
| 18 | 
         
            -
            pipe = pipe.to(device)
         
     | 
| 19 | 
         | 
| 20 | 
         
             
            MAX_SEED = np.iinfo(np.int32).max
         
     | 
| 21 | 
         
            -
            MAX_IMAGE_SIZE =  
     | 
| 22 | 
         | 
| 
         | 
|
| 
         | 
|
| 23 | 
         | 
| 24 | 
         
            -
            # @spaces.GPU #[uncomment to use ZeroGPU]
         
     | 
| 25 | 
         
            -
            def infer(
         
     | 
| 26 | 
         
            -
                prompt,
         
     | 
| 27 | 
         
            -
                negative_prompt,
         
     | 
| 28 | 
         
            -
                seed,
         
     | 
| 29 | 
         
            -
                randomize_seed,
         
     | 
| 30 | 
         
            -
                width,
         
     | 
| 31 | 
         
            -
                height,
         
     | 
| 32 | 
         
            -
                guidance_scale,
         
     | 
| 33 | 
         
            -
                num_inference_steps,
         
     | 
| 34 | 
         
            -
                progress=gr.Progress(track_tqdm=True),
         
     | 
| 35 | 
         
            -
            ):
         
     | 
| 36 | 
         
             
                if randomize_seed:
         
     | 
| 37 | 
         
             
                    seed = random.randint(0, MAX_SEED)
         
     | 
| 38 | 
         
            -
             
     | 
| 39 | 
         
             
                generator = torch.Generator().manual_seed(seed)
         
     | 
| 40 | 
         
            -
             
     | 
| 41 | 
         
             
                image = pipe(
         
     | 
| 42 | 
         
            -
                    prompt=prompt,
         
     | 
| 43 | 
         
            -
                    negative_prompt=negative_prompt,
         
     | 
| 44 | 
         
            -
                    guidance_scale=guidance_scale,
         
     | 
| 45 | 
         
            -
                    num_inference_steps=num_inference_steps,
         
     | 
| 46 | 
         
            -
                    width=width,
         
     | 
| 47 | 
         
            -
                    height=height,
         
     | 
| 48 | 
         
            -
                    generator=generator 
     | 
| 49 | 
         
            -
                ).images[0]
         
     | 
| 50 | 
         
            -
             
     | 
| 51 | 
         
             
                return image, seed
         
     | 
| 52 | 
         | 
| 53 | 
         
            -
             
     | 
| 54 | 
         
             
            examples = [
         
     | 
| 55 | 
         
             
                "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
         
     | 
| 56 | 
         
             
                "An astronaut riding a green horse",
         
     | 
| 57 | 
         
             
                "A delicious ceviche cheesecake slice",
         
     | 
| 58 | 
         
             
            ]
         
     | 
| 59 | 
         | 
| 60 | 
         
            -
            css 
     | 
| 61 | 
         
             
            #col-container {
         
     | 
| 62 | 
         
             
                margin: 0 auto;
         
     | 
| 63 | 
         
            -
                max-width:  
     | 
| 64 | 
         
             
            }
         
     | 
| 65 | 
         
             
            """
         
     | 
| 66 | 
         | 
| 67 | 
         
             
            with gr.Blocks(css=css) as demo:
         
     | 
| 
         | 
|
| 68 | 
         
             
                with gr.Column(elem_id="col-container"):
         
     | 
| 69 | 
         
            -
                    gr.Markdown(" 
     | 
| 70 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 71 | 
         
             
                    with gr.Row():
         
     | 
| 
         | 
|
| 72 | 
         
             
                        prompt = gr.Text(
         
     | 
| 73 | 
         
             
                            label="Prompt",
         
     | 
| 74 | 
         
             
                            show_label=False,
         
     | 
| 
         @@ -76,19 +87,19 @@ with gr.Blocks(css=css) as demo: 
     | 
|
| 76 | 
         
             
                            placeholder="Enter your prompt",
         
     | 
| 77 | 
         
             
                            container=False,
         
     | 
| 78 | 
         
             
                        )
         
     | 
| 79 | 
         
            -
             
     | 
| 80 | 
         
            -
                        run_button = gr.Button("Run", scale=0 
     | 
| 81 | 
         
            -
             
     | 
| 82 | 
         
             
                    result = gr.Image(label="Result", show_label=False)
         
     | 
| 83 | 
         | 
| 84 | 
         
             
                    with gr.Accordion("Advanced Settings", open=False):
         
     | 
| 
         | 
|
| 85 | 
         
             
                        negative_prompt = gr.Text(
         
     | 
| 86 | 
         
             
                            label="Negative prompt",
         
     | 
| 87 | 
         
             
                            max_lines=1,
         
     | 
| 88 | 
         
             
                            placeholder="Enter a negative prompt",
         
     | 
| 89 | 
         
            -
                            visible=False,
         
     | 
| 90 | 
         
             
                        )
         
     | 
| 91 | 
         
            -
             
     | 
| 92 | 
         
             
                        seed = gr.Slider(
         
     | 
| 93 | 
         
             
                            label="Seed",
         
     | 
| 94 | 
         
             
                            minimum=0,
         
     | 
| 
         @@ -96,59 +107,54 @@ with gr.Blocks(css=css) as demo: 
     | 
|
| 96 | 
         
             
                            step=1,
         
     | 
| 97 | 
         
             
                            value=0,
         
     | 
| 98 | 
         
             
                        )
         
     | 
| 99 | 
         
            -
             
     | 
| 100 | 
         
             
                        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
         
     | 
| 101 | 
         
            -
             
     | 
| 102 | 
         
             
                        with gr.Row():
         
     | 
| 
         | 
|
| 103 | 
         
             
                            width = gr.Slider(
         
     | 
| 104 | 
         
             
                                label="Width",
         
     | 
| 105 | 
         
             
                                minimum=256,
         
     | 
| 106 | 
         
             
                                maximum=MAX_IMAGE_SIZE,
         
     | 
| 107 | 
         
            -
                                step= 
     | 
| 108 | 
         
            -
                                value=1024, 
     | 
| 109 | 
         
             
                            )
         
     | 
| 110 | 
         
            -
             
     | 
| 111 | 
         
             
                            height = gr.Slider(
         
     | 
| 112 | 
         
             
                                label="Height",
         
     | 
| 113 | 
         
             
                                minimum=256,
         
     | 
| 114 | 
         
             
                                maximum=MAX_IMAGE_SIZE,
         
     | 
| 115 | 
         
            -
                                step= 
     | 
| 116 | 
         
            -
                                value=1024, 
     | 
| 117 | 
         
             
                            )
         
     | 
| 118 | 
         
            -
             
     | 
| 119 | 
         
             
                        with gr.Row():
         
     | 
| 
         | 
|
| 120 | 
         
             
                            guidance_scale = gr.Slider(
         
     | 
| 121 | 
         
             
                                label="Guidance scale",
         
     | 
| 122 | 
         
             
                                minimum=0.0,
         
     | 
| 123 | 
         
             
                                maximum=10.0,
         
     | 
| 124 | 
         
             
                                step=0.1,
         
     | 
| 125 | 
         
            -
                                value= 
     | 
| 126 | 
         
             
                            )
         
     | 
| 127 | 
         
            -
             
     | 
| 128 | 
         
             
                            num_inference_steps = gr.Slider(
         
     | 
| 129 | 
         
             
                                label="Number of inference steps",
         
     | 
| 130 | 
         
             
                                minimum=1,
         
     | 
| 131 | 
         
             
                                maximum=50,
         
     | 
| 132 | 
         
             
                                step=1,
         
     | 
| 133 | 
         
            -
                                value= 
     | 
| 134 | 
         
             
                            )
         
     | 
| 135 | 
         
            -
             
     | 
| 136 | 
         
            -
                    gr.Examples( 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 137 | 
         
             
                gr.on(
         
     | 
| 138 | 
         
            -
                    triggers=[run_button.click, prompt.submit],
         
     | 
| 139 | 
         
            -
                    fn=infer,
         
     | 
| 140 | 
         
            -
                    inputs=[
         
     | 
| 141 | 
         
            -
             
     | 
| 142 | 
         
            -
                        negative_prompt,
         
     | 
| 143 | 
         
            -
                        seed,
         
     | 
| 144 | 
         
            -
                        randomize_seed,
         
     | 
| 145 | 
         
            -
                        width,
         
     | 
| 146 | 
         
            -
                        height,
         
     | 
| 147 | 
         
            -
                        guidance_scale,
         
     | 
| 148 | 
         
            -
                        num_inference_steps,
         
     | 
| 149 | 
         
            -
                    ],
         
     | 
| 150 | 
         
            -
                    outputs=[result, seed],
         
     | 
| 151 | 
         
             
                )
         
     | 
| 152 | 
         | 
| 153 | 
         
            -
             
     | 
| 154 | 
         
            -
                demo.launch()
         
     | 
| 
         | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 2 | 
         
            +
            import spaces
         
     | 
| 3 | 
         
             
            import numpy as np
         
     | 
| 4 | 
         
             
            import random
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 5 | 
         
             
            import torch
         
     | 
| 6 | 
         | 
| 7 | 
         
            +
            from diffusers.models.autoencoders.autoencoder_kl import AutoencoderKL
         
     | 
| 8 | 
         
            +
            from diffusers.pipelines.stable_diffusion_xl.pipeline_stable_diffusion_xl import (
         
     | 
| 9 | 
         
            +
                StableDiffusionXLPipeline,
         
     | 
| 10 | 
         
            +
            )
         
     | 
| 11 | 
         
            +
            from diffusers.schedulers.scheduling_euler_ancestral_discrete import (
         
     | 
| 12 | 
         
            +
                EulerAncestralDiscreteScheduler,
         
     | 
| 13 | 
         
            +
            )
         
     | 
| 14 | 
         
            +
             
     | 
| 15 | 
         
             
            device = "cuda" if torch.cuda.is_available() else "cpu"
         
     | 
| 16 | 
         
            +
            dtype = torch.float16
         
     | 
| 17 | 
         
            +
             
     | 
| 18 | 
         
            +
            repo = "OnomaAIResearch/Illustrious-xl-early-release-v0"
         
     | 
| 19 | 
         
            +
             
     | 
| 20 | 
         
            +
            vae = AutoencoderKL.from_pretrained(
         
     | 
| 21 | 
         
            +
                    "madebyollin/sdxl-vae-fp16-fix",
         
     | 
| 22 | 
         
            +
                    torch_dtype=torch.float16,
         
     | 
| 23 | 
         
            +
                )
         
     | 
| 24 | 
         | 
| 25 | 
         
            +
            pipe = StableDiffusionXLPipeline.from_pretrained(
         
     | 
| 26 | 
         
            +
                repo,
         
     | 
| 27 | 
         
            +
                vae=vae,
         
     | 
| 28 | 
         
            +
                torch_dtype=torch.float16,
         
     | 
| 29 | 
         
            +
                use_safetensors=True,
         
     | 
| 30 | 
         
            +
                add_watermarker=False,
         
     | 
| 31 | 
         
            +
                custom_pipeline="lpw_stable_diffusion_xl",
         
     | 
| 32 | 
         
            +
            ).to(device)
         
     | 
| 33 | 
         
            +
             
     | 
| 34 | 
         
            +
            pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
         
     | 
| 35 | 
         | 
| 
         | 
|
| 
         | 
|
| 36 | 
         | 
| 37 | 
         
             
            MAX_SEED = np.iinfo(np.int32).max
         
     | 
| 38 | 
         
            +
            MAX_IMAGE_SIZE = 1344
         
     | 
| 39 | 
         | 
| 40 | 
         
            +
            @spaces.GPU
         
     | 
| 41 | 
         
            +
            def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
         
     | 
| 42 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 43 | 
         
             
                if randomize_seed:
         
     | 
| 44 | 
         
             
                    seed = random.randint(0, MAX_SEED)
         
     | 
| 45 | 
         
            +
                    
         
     | 
| 46 | 
         
             
                generator = torch.Generator().manual_seed(seed)
         
     | 
| 47 | 
         
            +
                
         
     | 
| 48 | 
         
             
                image = pipe(
         
     | 
| 49 | 
         
            +
                    prompt = prompt, 
         
     | 
| 50 | 
         
            +
                    negative_prompt = negative_prompt,
         
     | 
| 51 | 
         
            +
                    guidance_scale = guidance_scale, 
         
     | 
| 52 | 
         
            +
                    num_inference_steps = num_inference_steps, 
         
     | 
| 53 | 
         
            +
                    width = width, 
         
     | 
| 54 | 
         
            +
                    height = height,
         
     | 
| 55 | 
         
            +
                    generator = generator
         
     | 
| 56 | 
         
            +
                ).images[0] 
         
     | 
| 57 | 
         
            +
                
         
     | 
| 58 | 
         
             
                return image, seed
         
     | 
| 59 | 
         | 
| 
         | 
|
| 60 | 
         
             
            examples = [
         
     | 
| 61 | 
         
             
                "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
         
     | 
| 62 | 
         
             
                "An astronaut riding a green horse",
         
     | 
| 63 | 
         
             
                "A delicious ceviche cheesecake slice",
         
     | 
| 64 | 
         
             
            ]
         
     | 
| 65 | 
         | 
| 66 | 
         
            +
            css="""
         
     | 
| 67 | 
         
             
            #col-container {
         
     | 
| 68 | 
         
             
                margin: 0 auto;
         
     | 
| 69 | 
         
            +
                max-width: 580px;
         
     | 
| 70 | 
         
             
            }
         
     | 
| 71 | 
         
             
            """
         
     | 
| 72 | 
         | 
| 73 | 
         
             
            with gr.Blocks(css=css) as demo:
         
     | 
| 74 | 
         
            +
                
         
     | 
| 75 | 
         
             
                with gr.Column(elem_id="col-container"):
         
     | 
| 76 | 
         
            +
                    gr.Markdown(f"""
         
     | 
| 77 | 
         
            +
                    # Demo [Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)
         
     | 
| 78 | 
         
            +
                    Learn more about the [Stable Diffusion 3 series](https://stability.ai/news/stable-diffusion-3). Try on [Stability AI API](https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post), [Stable Assistant](https://stability.ai/stable-assistant), or on Discord via [Stable Artisan](https://stability.ai/stable-artisan). Run locally with [ComfyUI](https://github.com/comfyanonymous/ComfyUI) or [diffusers](https://github.com/huggingface/diffusers)
         
     | 
| 79 | 
         
            +
                    """)
         
     | 
| 80 | 
         
            +
                    
         
     | 
| 81 | 
         
             
                    with gr.Row():
         
     | 
| 82 | 
         
            +
                        
         
     | 
| 83 | 
         
             
                        prompt = gr.Text(
         
     | 
| 84 | 
         
             
                            label="Prompt",
         
     | 
| 85 | 
         
             
                            show_label=False,
         
     | 
| 
         | 
|
| 87 | 
         
             
                            placeholder="Enter your prompt",
         
     | 
| 88 | 
         
             
                            container=False,
         
     | 
| 89 | 
         
             
                        )
         
     | 
| 90 | 
         
            +
                        
         
     | 
| 91 | 
         
            +
                        run_button = gr.Button("Run", scale=0)
         
     | 
| 92 | 
         
            +
                    
         
     | 
| 93 | 
         
             
                    result = gr.Image(label="Result", show_label=False)
         
     | 
| 94 | 
         | 
| 95 | 
         
             
                    with gr.Accordion("Advanced Settings", open=False):
         
     | 
| 96 | 
         
            +
                        
         
     | 
| 97 | 
         
             
                        negative_prompt = gr.Text(
         
     | 
| 98 | 
         
             
                            label="Negative prompt",
         
     | 
| 99 | 
         
             
                            max_lines=1,
         
     | 
| 100 | 
         
             
                            placeholder="Enter a negative prompt",
         
     | 
| 
         | 
|
| 101 | 
         
             
                        )
         
     | 
| 102 | 
         
            +
                        
         
     | 
| 103 | 
         
             
                        seed = gr.Slider(
         
     | 
| 104 | 
         
             
                            label="Seed",
         
     | 
| 105 | 
         
             
                            minimum=0,
         
     | 
| 
         | 
|
| 107 | 
         
             
                            step=1,
         
     | 
| 108 | 
         
             
                            value=0,
         
     | 
| 109 | 
         
             
                        )
         
     | 
| 110 | 
         
            +
                        
         
     | 
| 111 | 
         
             
                        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
         
     | 
| 112 | 
         
            +
                        
         
     | 
| 113 | 
         
             
                        with gr.Row():
         
     | 
| 114 | 
         
            +
                            
         
     | 
| 115 | 
         
             
                            width = gr.Slider(
         
     | 
| 116 | 
         
             
                                label="Width",
         
     | 
| 117 | 
         
             
                                minimum=256,
         
     | 
| 118 | 
         
             
                                maximum=MAX_IMAGE_SIZE,
         
     | 
| 119 | 
         
            +
                                step=64,
         
     | 
| 120 | 
         
            +
                                value=1024,
         
     | 
| 121 | 
         
             
                            )
         
     | 
| 122 | 
         
            +
                            
         
     | 
| 123 | 
         
             
                            height = gr.Slider(
         
     | 
| 124 | 
         
             
                                label="Height",
         
     | 
| 125 | 
         
             
                                minimum=256,
         
     | 
| 126 | 
         
             
                                maximum=MAX_IMAGE_SIZE,
         
     | 
| 127 | 
         
            +
                                step=64,
         
     | 
| 128 | 
         
            +
                                value=1024,
         
     | 
| 129 | 
         
             
                            )
         
     | 
| 130 | 
         
            +
                        
         
     | 
| 131 | 
         
             
                        with gr.Row():
         
     | 
| 132 | 
         
            +
                            
         
     | 
| 133 | 
         
             
                            guidance_scale = gr.Slider(
         
     | 
| 134 | 
         
             
                                label="Guidance scale",
         
     | 
| 135 | 
         
             
                                minimum=0.0,
         
     | 
| 136 | 
         
             
                                maximum=10.0,
         
     | 
| 137 | 
         
             
                                step=0.1,
         
     | 
| 138 | 
         
            +
                                value=5.0,
         
     | 
| 139 | 
         
             
                            )
         
     | 
| 140 | 
         
            +
                            
         
     | 
| 141 | 
         
             
                            num_inference_steps = gr.Slider(
         
     | 
| 142 | 
         
             
                                label="Number of inference steps",
         
     | 
| 143 | 
         
             
                                minimum=1,
         
     | 
| 144 | 
         
             
                                maximum=50,
         
     | 
| 145 | 
         
             
                                step=1,
         
     | 
| 146 | 
         
            +
                                value=28,
         
     | 
| 147 | 
         
             
                            )
         
     | 
| 148 | 
         
            +
                    
         
     | 
| 149 | 
         
            +
                    gr.Examples(
         
     | 
| 150 | 
         
            +
                        examples = examples,
         
     | 
| 151 | 
         
            +
                        inputs = [prompt]
         
     | 
| 152 | 
         
            +
                    )
         
     | 
| 153 | 
         
             
                gr.on(
         
     | 
| 154 | 
         
            +
                    triggers=[run_button.click, prompt.submit, negative_prompt.submit],
         
     | 
| 155 | 
         
            +
                    fn = infer,
         
     | 
| 156 | 
         
            +
                    inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
         
     | 
| 157 | 
         
            +
                    outputs = [result, seed]
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 158 | 
         
             
                )
         
     | 
| 159 | 
         | 
| 160 | 
         
            +
            demo.launch()
         
     | 
| 
         | 
    	
        requirements.txt
    CHANGED
    
    | 
         @@ -1,6 +1,4 @@ 
     | 
|
| 1 | 
         
            -
             
     | 
| 2 | 
         
            -
            diffusers
         
     | 
| 3 | 
         
            -
            invisible_watermark
         
     | 
| 4 | 
         
            -
            torch
         
     | 
| 5 | 
         
             
            transformers
         
     | 
| 6 | 
         
            -
             
     | 
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            git+https://github.com/huggingface/diffusers.git
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 2 | 
         
             
            transformers
         
     | 
| 3 | 
         
            +
            accelerate
         
     | 
| 4 | 
         
            +
            sentencepiece
         
     |