Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,5 +1,5 @@ | |
| 1 | 
            -
            ### | 
| 2 | 
            -
            ###
         | 
| 3 | 
             
            ###
         | 
| 4 | 
             
            import os
         | 
| 5 | 
             
            import gradio as gr
         | 
| @@ -9,9 +9,185 @@ import torch | |
| 9 | 
             
            from PIL import Image
         | 
| 10 | 
             
            import fitz  # PyMuPDF pour la gestion des PDF
         | 
| 11 | 
             
            import sentencepiece
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 12 |  | 
| 13 | 
             
            # Force l'utilisation du CPU pour tout PyTorch
         | 
| 14 | 
            -
            torch.set_default_device("cpu")
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 15 |  | 
| 16 | 
             
            def load_pdf(pdf_path):
         | 
| 17 | 
             
                """Traite le texte d'un fichier PDF"""
         | 
| @@ -48,9 +224,9 @@ class FluxGenerator: | |
| 48 | 
             
                        self.pipeline = FluxPipeline.from_pretrained(
         | 
| 49 | 
             
                            "black-forest-labs/FLUX.1-schnell",
         | 
| 50 | 
             
                            revision="refs/pr/1",
         | 
| 51 | 
            -
                            torch_dtype=torch.float32 | 
| 52 | 
            -
                            device_map={" | 
| 53 | 
            -
                        )
         | 
| 54 |  | 
| 55 | 
             
                        # Désactive les optimisations GPU
         | 
| 56 | 
             
                        self.pipeline.to(self.device)
         | 
|  | |
| 1 | 
            +
            ###TEST03 JUSTE CHARGER FLUX-SCHNELL
         | 
| 2 | 
            +
            ###https://huggingface.co/spaces/black-forest-labs/FLUX.1-schnell/blob/main/app.py
         | 
| 3 | 
             
            ###
         | 
| 4 | 
             
            import os
         | 
| 5 | 
             
            import gradio as gr
         | 
|  | |
| 9 | 
             
            from PIL import Image
         | 
| 10 | 
             
            import fitz  # PyMuPDF pour la gestion des PDF
         | 
| 11 | 
             
            import sentencepiece
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            import numpy as np
         | 
| 14 | 
            +
            import random
         | 
| 15 | 
            +
            import spaces
         | 
| 16 | 
            +
             | 
| 17 | 
            +
             | 
| 18 | 
            +
             | 
| 19 | 
            +
             | 
| 20 | 
            +
             | 
| 21 | 
            +
            #
         | 
| 22 | 
            +
            #import gradio as gr
         | 
| 23 | 
            +
            #import numpy as np
         | 
| 24 | 
            +
            #import random
         | 
| 25 | 
            +
            #import spaces
         | 
| 26 | 
            +
            #import torch
         | 
| 27 | 
            +
            #from diffusers import DiffusionPipeline
         | 
| 28 | 
            +
            #
         | 
| 29 | 
            +
            #dtype = torch.bfloat16
         | 
| 30 | 
            +
            #device = "cuda" if torch.cuda.is_available() else "cpu"
         | 
| 31 | 
            +
            #
         | 
| 32 | 
            +
            #pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
         | 
| 33 | 
            +
            #
         | 
| 34 | 
            +
            #MAX_SEED = np.iinfo(np.int32).max
         | 
| 35 | 
            +
            #MAX_IMAGE_SIZE = 2048
         | 
| 36 | 
            +
            #
         | 
| 37 | 
            +
            #@spaces.GPU()
         | 
| 38 | 
            +
            #def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
         | 
| 39 | 
            +
            #    if randomize_seed:
         | 
| 40 | 
            +
            #        seed = random.randint(0, MAX_SEED)
         | 
| 41 | 
            +
            #    generator = torch.Generator().manual_seed(seed)
         | 
| 42 | 
            +
            #    image = pipe(
         | 
| 43 | 
            +
            #            prompt = prompt, 
         | 
| 44 | 
            +
            #            width = width,
         | 
| 45 | 
            +
            #            height = height,
         | 
| 46 | 
            +
            #            num_inference_steps = num_inference_steps, 
         | 
| 47 | 
            +
            #            generator = generator,
         | 
| 48 | 
            +
            #            guidance_scale=0.0
         | 
| 49 | 
            +
            #    ).images[0] 
         | 
| 50 | 
            +
            #    return image, seed
         | 
| 51 | 
            +
            # 
         | 
| 52 | 
            +
            #examples = [
         | 
| 53 | 
            +
            #    "a tiny astronaut hatching from an egg on the moon",
         | 
| 54 | 
            +
            #    "a cat holding a sign that says hello world",
         | 
| 55 | 
            +
            #    "an anime illustration of a wiener schnitzel",
         | 
| 56 | 
            +
            #]
         | 
| 57 | 
            +
            #
         | 
| 58 | 
            +
            #css="""
         | 
| 59 | 
            +
            ##col-container {
         | 
| 60 | 
            +
            #    margin: 0 auto;
         | 
| 61 | 
            +
            #    max-width: 520px;
         | 
| 62 | 
            +
            #}
         | 
| 63 | 
            +
            #"""
         | 
| 64 | 
            +
            #
         | 
| 65 | 
            +
            #with gr.Blocks(css=css) as demo:
         | 
| 66 | 
            +
            #    
         | 
| 67 | 
            +
            #    with gr.Column(elem_id="col-container"):
         | 
| 68 | 
            +
            #        gr.Markdown(f"""# FLUX.1 [schnell]
         | 
| 69 | 
            +
            #12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
         | 
| 70 | 
            +
            #[[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]
         | 
| 71 | 
            +
            #        """)
         | 
| 72 | 
            +
            #        
         | 
| 73 | 
            +
            #        with gr.Row():
         | 
| 74 | 
            +
            #            
         | 
| 75 | 
            +
            #            prompt = gr.Text(
         | 
| 76 | 
            +
            #                label="Prompt",
         | 
| 77 | 
            +
            #                show_label=False,
         | 
| 78 | 
            +
            #                max_lines=1,
         | 
| 79 | 
            +
            #                placeholder="Enter your prompt",
         | 
| 80 | 
            +
            #                container=False,
         | 
| 81 | 
            +
            #            )
         | 
| 82 | 
            +
            #            
         | 
| 83 | 
            +
            #            run_button = gr.Button("Run", scale=0)
         | 
| 84 | 
            +
            #        
         | 
| 85 | 
            +
            #        result = gr.Image(label="Result", show_label=False)
         | 
| 86 | 
            +
            #        
         | 
| 87 | 
            +
            #        with gr.Accordion("Advanced Settings", open=False):
         | 
| 88 | 
            +
            #            
         | 
| 89 | 
            +
            #            seed = gr.Slider(
         | 
| 90 | 
            +
            #                label="Seed",
         | 
| 91 | 
            +
            #                minimum=0,
         | 
| 92 | 
            +
            #                maximum=MAX_SEED,
         | 
| 93 | 
            +
            #                step=1,
         | 
| 94 | 
            +
            #                value=0,
         | 
| 95 | 
            +
            #            )
         | 
| 96 | 
            +
            #            
         | 
| 97 | 
            +
            #            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
         | 
| 98 | 
            +
            #            
         | 
| 99 | 
            +
            #            with gr.Row():
         | 
| 100 | 
            +
            #                
         | 
| 101 | 
            +
            #                width = gr.Slider(
         | 
| 102 | 
            +
            #                    label="Width",
         | 
| 103 | 
            +
            #                    minimum=256,
         | 
| 104 | 
            +
            #                    maximum=MAX_IMAGE_SIZE,
         | 
| 105 | 
            +
            #                    step=32,
         | 
| 106 | 
            +
            #                    value=1024,
         | 
| 107 | 
            +
            #                )
         | 
| 108 | 
            +
            #                
         | 
| 109 | 
            +
            #                height = gr.Slider(
         | 
| 110 | 
            +
            #                    label="Height",
         | 
| 111 | 
            +
            #                    minimum=256,
         | 
| 112 | 
            +
            #                    maximum=MAX_IMAGE_SIZE,
         | 
| 113 | 
            +
            #                    step=32,
         | 
| 114 | 
            +
            #                    value=1024,
         | 
| 115 | 
            +
            #                )
         | 
| 116 | 
            +
            #            
         | 
| 117 | 
            +
            #            with gr.Row():
         | 
| 118 | 
            +
            #                
         | 
| 119 | 
            +
            #  
         | 
| 120 | 
            +
            #                num_inference_steps = gr.Slider(
         | 
| 121 | 
            +
            #                    label="Number of inference steps",
         | 
| 122 | 
            +
            #                    minimum=1,
         | 
| 123 | 
            +
            #                    maximum=50,
         | 
| 124 | 
            +
            #                    step=1,
         | 
| 125 | 
            +
            #                    value=4,
         | 
| 126 | 
            +
            #                )
         | 
| 127 | 
            +
            #        
         | 
| 128 | 
            +
            #        gr.Examples(
         | 
| 129 | 
            +
            #            examples = examples,
         | 
| 130 | 
            +
            #            fn = infer,
         | 
| 131 | 
            +
            #            inputs = [prompt],
         | 
| 132 | 
            +
            #            outputs = [result, seed],
         | 
| 133 | 
            +
            #            cache_examples="lazy"
         | 
| 134 | 
            +
            #        )
         | 
| 135 | 
            +
            #
         | 
| 136 | 
            +
            #    gr.on(
         | 
| 137 | 
            +
            #        triggers=[run_button.click, prompt.submit],
         | 
| 138 | 
            +
            #        fn = infer,
         | 
| 139 | 
            +
            #        inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
         | 
| 140 | 
            +
            #        outputs = [result, seed]
         | 
| 141 | 
            +
            #    )
         | 
| 142 | 
            +
            #
         | 
| 143 | 
            +
            #demo.launch()
         | 
| 144 | 
            +
            #
         | 
| 145 | 
            +
            #
         | 
| 146 | 
            +
             | 
| 147 | 
            +
             | 
| 148 | 
            +
             | 
| 149 | 
            +
             | 
| 150 | 
            +
             | 
| 151 | 
            +
             | 
| 152 | 
            +
             | 
| 153 | 
            +
             | 
| 154 | 
            +
             | 
| 155 | 
            +
             | 
| 156 | 
            +
             | 
| 157 | 
            +
             | 
| 158 | 
            +
             | 
| 159 | 
            +
             | 
| 160 | 
            +
             | 
| 161 | 
            +
             | 
| 162 | 
            +
             | 
| 163 | 
            +
             | 
| 164 | 
            +
             | 
| 165 | 
            +
             | 
| 166 | 
            +
             | 
| 167 | 
            +
             | 
| 168 | 
            +
             | 
| 169 | 
            +
             | 
| 170 | 
            +
             | 
| 171 |  | 
| 172 | 
             
            # Force l'utilisation du CPU pour tout PyTorch
         | 
| 173 | 
            +
            #torch.set_default_device("cpu")
         | 
| 174 | 
            +
             | 
| 175 | 
            +
             | 
| 176 | 
            +
            #dtype = torch.bfloat16
         | 
| 177 | 
            +
            device = "cuda" if torch.cuda.is_available() else "cpu"
         | 
| 178 | 
            +
            #
         | 
| 179 | 
            +
            #pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
         | 
| 180 | 
            +
             | 
| 181 | 
            +
             | 
| 182 | 
            +
             | 
| 183 | 
            +
             | 
| 184 | 
            +
             | 
| 185 | 
            +
             | 
| 186 | 
            +
             | 
| 187 | 
            +
             | 
| 188 | 
            +
             | 
| 189 | 
            +
             | 
| 190 | 
            +
             | 
| 191 |  | 
| 192 | 
             
            def load_pdf(pdf_path):
         | 
| 193 | 
             
                """Traite le texte d'un fichier PDF"""
         | 
|  | |
| 224 | 
             
                        self.pipeline = FluxPipeline.from_pretrained(
         | 
| 225 | 
             
                            "black-forest-labs/FLUX.1-schnell",
         | 
| 226 | 
             
                            revision="refs/pr/1",
         | 
| 227 | 
            +
                            torch_dtype=torch.float32  # Utilise float32 au lieu de bfloat16 pour meilleure compatibilité CPU
         | 
| 228 | 
            +
            #                device_map={"cpu": self.device}  # Force tous les composants sur CPU
         | 
| 229 | 
            +
                        )device
         | 
| 230 |  | 
| 231 | 
             
                        # Désactive les optimisations GPU
         | 
| 232 | 
             
                        self.pipeline.to(self.device)
         |