File size: 8,725 Bytes
13eeca7
 
bfdb818
 
 
 
 
 
 
 
 
13eeca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfdb818
 
13eeca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfdb818
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13eeca7
7cc1e07
13eeca7
7cc1e07
bfdb818
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
###TEST03 JUSTE CHARGER FLUX-SCHNELL
###https://huggingface.co/spaces/black-forest-labs/FLUX.1-schnell/blob/main/app.py
###
import os
import gradio as gr
from huggingface_hub import login
from diffusers import FluxPipeline
import torch
from PIL import Image
import fitz  # PyMuPDF pour la gestion des PDF
import sentencepiece

import numpy as np
import random
import spaces





#
#import gradio as gr
#import numpy as np
#import random
#import spaces
#import torch
#from diffusers import DiffusionPipeline
#
#dtype = torch.bfloat16
#device = "cuda" if torch.cuda.is_available() else "cpu"
#
#pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
#
#MAX_SEED = np.iinfo(np.int32).max
#MAX_IMAGE_SIZE = 2048
#
#@spaces.GPU()
#def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
#    if randomize_seed:
#        seed = random.randint(0, MAX_SEED)
#    generator = torch.Generator().manual_seed(seed)
#    image = pipe(
#            prompt = prompt, 
#            width = width,
#            height = height,
#            num_inference_steps = num_inference_steps, 
#            generator = generator,
#            guidance_scale=0.0
#    ).images[0] 
#    return image, seed
# 
#examples = [
#    "a tiny astronaut hatching from an egg on the moon",
#    "a cat holding a sign that says hello world",
#    "an anime illustration of a wiener schnitzel",
#]
#
#css="""
##col-container {
#    margin: 0 auto;
#    max-width: 520px;
#}
#"""
#
#with gr.Blocks(css=css) as demo:
#    
#    with gr.Column(elem_id="col-container"):
#        gr.Markdown(f"""# FLUX.1 [schnell]
#12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
#[[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]
#        """)
#        
#        with gr.Row():
#            
#            prompt = gr.Text(
#                label="Prompt",
#                show_label=False,
#                max_lines=1,
#                placeholder="Enter your prompt",
#                container=False,
#            )
#            
#            run_button = gr.Button("Run", scale=0)
#        
#        result = gr.Image(label="Result", show_label=False)
#        
#        with gr.Accordion("Advanced Settings", open=False):
#            
#            seed = gr.Slider(
#                label="Seed",
#                minimum=0,
#                maximum=MAX_SEED,
#                step=1,
#                value=0,
#            )
#            
#            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
#            
#            with gr.Row():
#                
#                width = gr.Slider(
#                    label="Width",
#                    minimum=256,
#                    maximum=MAX_IMAGE_SIZE,
#                    step=32,
#                    value=1024,
#                )
#                
#                height = gr.Slider(
#                    label="Height",
#                    minimum=256,
#                    maximum=MAX_IMAGE_SIZE,
#                    step=32,
#                    value=1024,
#                )
#            
#            with gr.Row():
#                
#  
#                num_inference_steps = gr.Slider(
#                    label="Number of inference steps",
#                    minimum=1,
#                    maximum=50,
#                    step=1,
#                    value=4,
#                )
#        
#        gr.Examples(
#            examples = examples,
#            fn = infer,
#            inputs = [prompt],
#            outputs = [result, seed],
#            cache_examples="lazy"
#        )
#
#    gr.on(
#        triggers=[run_button.click, prompt.submit],
#        fn = infer,
#        inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
#        outputs = [result, seed]
#    )
#
#demo.launch()
#
#


























# Force l'utilisation du CPU pour tout PyTorch
#torch.set_default_device("cpu")


#dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
#
#pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)












def load_pdf(pdf_path):
    """Traite le texte d'un fichier PDF"""
    if pdf_path is None:
        return None
    text = ""
    try:
        doc = fitz.open(pdf_path)
        for page in doc:
            text += page.get_text()
        doc.close()
        return text
    except Exception as e:
        print(f"Erreur lors de la lecture du PDF: {str(e)}")
        return None

class FluxGenerator:
    def __init__(self):
        self.token = os.getenv('Authentification_HF')
        if not self.token:
            raise ValueError("Token d'authentification HuggingFace non trouvé")
        login(self.token)
        self.pipeline = None
        self.device = "cpu"  # Force l'utilisation du CPU
        self.load_model()

    def load_model(self):
        """Charge le modèle FLUX avec des paramètres optimisés pour CPU"""
        try:
            print("Chargement du modèle FLUX sur CPU...")
            # Configuration spécifique pour CPU
            torch.set_grad_enabled(False)  # Désactive le calcul des gradients
            
            self.pipeline = FluxPipeline.from_pretrained(
                "black-forest-labs/FLUX.1-schnell",
                revision="refs/pr/1",
                torch_dtype=torch.float32  # Utilise float32 au lieu de bfloat16 pour meilleure compatibilité CPU
            )
#                device_map={"cpu": self.device}  # Force tous les composants sur CPU
#            )device
            
            # Désactive les optimisations GPU
            self.pipeline.to(self.device)
            print(f"Utilisation forcée du CPU")
            print("Modèle FLUX chargé avec succès!")
            
        except Exception as e:
            print(f"Erreur lors du chargement du modèle: {str(e)}")
            raise

    def generate_image(self, prompt, reference_image=None, pdf_file=None):
        """Génère une image à partir d'un prompt et optionnellement une référence"""
        try:
            # Si un PDF est fourni, ajoute son contenu au prompt
            if pdf_file is not None:
                pdf_text = load_pdf(pdf_file)
                if pdf_text:
                    prompt = f"{prompt}\nContexte du PDF:\n{pdf_text}"

            # Configuration pour génération sur CPU
            with torch.no_grad():  # Désactive le calcul des gradients pendant la génération
                image = self.pipeline(
                    prompt=prompt,
                    num_inference_steps=20,  # Réduit le nombre d'étapes pour accélérer sur CPU
                    guidance_scale=0.0,
                    max_sequence_length=256,
                    generator=torch.Generator(device=self.device).manual_seed(0)
                ).images[0]

            return image

        except Exception as e:
            print(f"Erreur lors de la génération de l'image: {str(e)}")
            return None

# Instance globale du générateur
generator = FluxGenerator()

def generate(prompt, reference_file):
    """Fonction de génération pour l'interface Gradio"""
    try:
        # Gestion du fichier de référence
        if reference_file is not None:
            if isinstance(reference_file, dict):  # Si le fichier est fourni par Gradio
                file_path = reference_file.name
            else:  # Si c'est un chemin direct
                file_path = reference_file
                
            file_type = file_path.split('.')[-1].lower()
            if file_type in ['pdf']:
                return generator.generate_image(prompt, pdf_file=file_path)
            elif file_type in ['png', 'jpg', 'jpeg']:
                return generator.generate_image(prompt, reference_image=file_path)

        # Génération sans référence
        return generator.generate_image(prompt)

    except Exception as e:
        print(f"Erreur détaillée: {str(e)}")
        return None

# Interface Gradio simple
demo = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Décrivez l'image que vous souhaitez générer..."),
        gr.File(label="Image ou PDF de référence (optionnel)", type="file")
    ],
    outputs=gr.Image(label="Image générée"),
    title="Test du modèle FLUX (CPU)",
    description="Interface simple pour tester la génération d'images avec FLUX (optimisé pour CPU)"
)

if __name__ == "__main__":
    demo.launch()