File size: 10,200 Bytes
101b95f
3541f6b
 
9f2d35d
3541f6b
7a245d0
9f2d35d
 
 
 
 
 
 
101b95f
9f2d35d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a245d0
9f2d35d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3541f6b
9f2d35d
 
 
 
 
 
 
 
3541f6b
9f2d35d
 
 
 
 
 
 
 
 
 
3541f6b
9f2d35d
 
 
 
 
 
 
 
 
 
 
 
3541f6b
 
 
9f2d35d
 
 
 
 
 
 
 
 
 
 
 
 
7a245d0
9f2d35d
 
 
 
 
 
 
 
 
 
 
7a245d0
9f2d35d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a245d0
9f2d35d
 
 
 
 
7a245d0
 
9f2d35d
 
 
 
 
 
 
 
 
 
 
 
101b95f
 
9f2d35d
 
101b95f
 
 
 
9f2d35d
 
 
101b95f
 
9f2d35d
 
101b95f
9f2d35d
 
101b95f
9f2d35d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a245d0
9f2d35d
 
 
 
 
 
 
 
 
7a245d0
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#142

import os
import gradio as gr
from huggingface_hub import hf_hub_download, login
from transformers import AutoModelForCausalLM, AutoTokenizer
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.text import PP_ALIGN
import torch
from llama_cpp import Llama
import time

# [Configuration des modèles et PREPROMPT restent identiques à C141]
TEXT_MODELS = {
    "Mistral Nemo 2407 (GGUF)": "MisterAI/Bartowski_MistralAI_Mistral-Nemo-Instruct-2407-IQ4_XS.gguf",
    "Mixtral 8x7B": "mistralai/Mixtral-8x7B-v0.1",
    "Lucie 7B": "OpenLLM-France/Lucie-7B"
}

PREPROMPT = """Vous êtes un assistant IA expert en création de présentations PowerPoint professionnelles.
Générez une présentation structurée et détaillée en suivant ce format EXACT:

TITRE: [Titre principal de la présentation]

DIAPO 1:
Titre: [Titre de la diapo]
Points:
- Point 1
- Point 2
- Point 3

DIAPO 2:
Titre: [Titre de la diapo]
Points:
- Point 1
- Point 2
- Point 3

[Continuez avec ce format pour chaque diapositive]

Analysez le texte suivant et créez une présentation professionnelle :"""

class PresentationGenerator:
    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.text_model = None
        self.text_tokenizer = None

    def load_text_model(self, model_name):
        """Charge le modèle de génération de texte"""
        model_id = TEXT_MODELS[model_name]
        if model_id.endswith('.gguf'):
            model_path = hf_hub_download(
                repo_id=model_id.split('/')[0] + '/' + model_id.split('/')[1],
                filename=model_id.split('/')[-1],
                token=self.token
            )
            self.text_model = Llama(
                model_path=model_path,
                n_ctx=4096,
                n_batch=512,
                verbose=False
            )
            print(f"Modèle GGUF {model_id} chargé avec succès!")
        else:
            self.text_tokenizer = AutoTokenizer.from_pretrained(model_id, token=self.token)
            self.text_model = AutoModelForCausalLM.from_pretrained(
                model_id,
                torch_dtype=torch.bfloat16,
                device_map="auto",
                token=self.token
            )
            print(f"Modèle Transformers {model_id} chargé avec succès!")

    def generate_text(self, prompt, temperature=0.7, max_tokens=4096):
        """Génère le texte de la présentation"""
        if isinstance(self.text_model, Llama):
            response = self.text_model(
                prompt,
                max_tokens=max_tokens,
                temperature=temperature,
                echo=False
            )
            print("Texte généré par Llama :", response['choices'][0]['text'])
            return response['choices'][0]['text']
        else:
            inputs = self.text_tokenizer.apply_chat_template(
                [{"role": "user", "content": prompt}],
                return_tensors="pt",
                return_dict=True
            )
            outputs = self.text_model.generate(
                **inputs,
                max_new_tokens=max_tokens,
                temperature=temperature
            )
            generated_text = self.text_tokenizer.decode(outputs[0], skip_special_tokens=True)
            print("Texte généré par Transformers :", generated_text)
            return generated_text

    def parse_presentation_content(self, content):
        """Parse le contenu généré en sections pour les diapositives"""
        slides = []
        current_slide = None

        for line in content.split('\n'):
            line = line.strip()
            if line.startswith('TITRE:'):
                slides.append({'type': 'title', 'title': line[6:].strip()})
            elif line.startswith('DIAPO'):
                if current_slide:
                    slides.append(current_slide)
                current_slide = {'type': 'content', 'title': '', 'points': []}
            elif line.startswith('Titre:') and current_slide:
                current_slide['title'] = line[6:].strip()
            elif line.startswith('- ') and current_slide:
                current_slide['points'].append(line[2:].strip())

        if current_slide:
            slides.append(current_slide)

        return slides

    def create_presentation(self, slides):
        """Crée la présentation PowerPoint avec texte uniquement"""
        prs = Presentation()

        # Première diapo (titre)
        title_slide = prs.slides.add_slide(prs.slide_layouts[0])
        title_slide.shapes.title.text = slides[0]['title']

        # Autres diapos
        for slide in slides[1:]:
            content_slide = prs.slides.add_slide(prs.slide_layouts[1])
            content_slide.shapes.title.text = slide['title']

            # Ajout du texte
            if slide['points']:
                body = content_slide.shapes.placeholders[1].text_frame
                body.clear()
                for point in slide['points']:
                    p = body.add_paragraph()
                    p.text = point
                    p.level = 0

        return prs

def generate_presentation_with_progress(text, text_model_name, temperature, max_tokens):
    """Fonction principale de génération avec suivi de progression"""
    try:
        start_time = time.time()
        generator = PresentationGenerator()

        # Chargement du modèle de texte uniquement
        yield "Chargement du modèle...", None, None
        generator.load_text_model(text_model_name)

        # Génération du contenu
        yield "Génération du contenu de la présentation...", None, None
        full_prompt = PREPROMPT + "\n\n" + text
        generated_content = generator.generate_text(full_prompt, temperature, max_tokens)

        # Création de la présentation
        yield "Création de la présentation PowerPoint...", generated_content, None
        slides = generator.parse_presentation_content(generated_content)
        prs = generator.create_presentation(slides)

        # Sauvegarde avec chemin absolu
        output_path = os.path.abspath("presentation.pptx")
        prs.save(output_path)

        # Vérification que le fichier existe
        if not os.path.exists(output_path):
            raise FileNotFoundError(f"Le fichier {output_path} n'a pas été créé correctement")

        execution_time = time.time() - start_time
        status = f"Présentation générée avec succès en {execution_time:.2f} secondes!"

        # Retourne le statut, le contenu généré et le fichier
        return status, generated_content, (output_path, "presentation.pptx")

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

# [Le reste du code (CSS et interface Gradio) reste identique à C141]
css = """
/* Thème sombre personnalisé */
.gradio-container {
    background-color: #000000 !important;
    color: #ffffff !important;
}

.gr-form, .gr-box, .gr-panel {
    border-radius: 8px !important;
    background-color: #1a1a1a !important;
    border: 1px solid #333333 !important;
    color: #ffffff !important;
}

.gr-input, .gr-textarea, .gr-dropdown {
    background-color: #2d2d2d !important;
    color: #ffffff !important;
    border: 1px solid #404040 !important;
}

.gr-button {
    background-color: #2d2d2d !important;
    color: #ffffff !important;
    border: 1px solid #404040 !important;
    transition: all 0.3s ease !important;
}

.gr-button:hover {
    background-color: #404040 !important;
    transform: translateY(-2px) !important;
}

/* Textes et labels */
h1, h2, h3, p, label, .gr-text {
    color: #ffffff !important;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: #1a1a1a;
}

::-webkit-scrollbar-thumb {
    background: #404040;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #4a4a4a;
}
"""

with gr.Blocks(theme=gr.themes.Default(), css=css) as demo:
    gr.Markdown(
        """
        # 🎯 Générateur de Présentations PowerPoint IA

        Créez des présentations professionnelles automatiquement avec l'aide de l'IA.
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            text_model_choice = gr.Dropdown(
                choices=list(TEXT_MODELS.keys()),
                value=list(TEXT_MODELS.keys())[0],
                label="Modèle de génération de texte"
            )
            temperature = gr.Slider(
                minimum=0.1,
                maximum=1.0,
                value=0.7,
                step=0.1,
                label="Température"
            )
            max_tokens = gr.Slider(
                minimum=1000,
                maximum=4096,
                value=2048,
                step=256,
                label="Tokens maximum"
            )

    with gr.Row():
        with gr.Column(scale=2):
            input_text = gr.Textbox(
                lines=10,
                label="Votre texte",
                placeholder="Décrivez le contenu que vous souhaitez pour votre présentation..."
            )

    with gr.Row():
        generate_btn = gr.Button("🚀 Générer la présentation", variant="primary")

    with gr.Row():
        with gr.Column():
            status_output = gr.Textbox(
                label="Statut",
                lines=2
            )
            generated_content = gr.Textbox(
                label="Contenu généré",
                lines=10,
                show_copy_button=True
            )
            output_file = gr.File(
                label="Présentation PowerPoint"
            )

    generate_btn.click(
        fn=generate_presentation_with_progress,
        inputs=[
            input_text,
            text_model_choice,
            temperature,
            max_tokens
        ],
        outputs=[
            status_output,
            generated_content,
            output_file
        ]
    )

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