File size: 11,591 Bytes
5500e59
 
 
 
 
 
 
 
 
 
84ddde8
 
 
 
 
 
 
 
 
5500e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84ddde8
5500e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84ddde8
 
5500e59
84ddde8
 
 
 
 
5500e59
84ddde8
 
 
 
 
 
 
 
5500e59
84ddde8
5500e59
84ddde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5500e59
84ddde8
 
 
 
 
5500e59
84ddde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5500e59
84ddde8
 
 
 
 
 
 
 
c321787
84ddde8
c321787
 
84ddde8
 
c321787
 
 
84ddde8
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
import gradio as gr
import os
import tempfile
from datetime import datetime
import logging
import json

from ai_enhance import AIEnhance
from presentation_generator import PresentationGenerator

# Configuration logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialisation des services
ai_service = AIEnhance()
presentation_generator = PresentationGenerator()

# Code secret
CODE_SECRET = "32015"  # code à cinq chiffres

def verifier_code(code):
    """Vérifie si le code entré est correct"""
    if code == CODE_SECRET:
        return True, "🔓 Accès autorisé !"
    else:
        return False, "❌ Code incorrect. Veuillez réessayer."

def create_presentation_structure(texte):
    """Crée la structure de présentation avec analyse IA"""
    try:
        # ESSAYER DIFFÉRENTES MÉTHODES POUR TROUVER CELLE QUI FONCTIONNE
        if hasattr(ai_service, '_basic_analysis'):
            analysis = ai_service._basic_analysis(texte)
        elif hasattr(ai_service, 'analyze_text_advanced'):
            analysis = ai_service.analyze_text_advanced(texte)
        else:
            # Créer une analyse de base manuellement
            analysis = {
                "statistics": {"word_count": len(texte.split()), "sentence_count": texte.count('.')},
                "keywords": ["texte", "analyse", "présentation"],
                "recommended_structure": {
                    "slides": [
                        {"title": "Introduction", "content": "Présentation du sujet"},
                        {"title": "Développement", "content": "Points principaux"},
                        {"title": "Conclusion", "content": "Synthèse"}
                    ]
                },
                "content_analysis": "général"
            }
        
        summary = ai_service.smart_summarize(texte)
        structure = {
            "title": f"Présentation: {analysis['content_analysis'].capitalize()}",
            "slides": analysis["recommended_structure"]["slides"],
            "key_points": analysis["keywords"][:8],
            "style_recommendation": analysis["recommended_structure"]["recommended_style"],
            "analysis_metadata": analysis
        }
        return structure, summary
        
    except Exception as e:
        logger.error(f"Erreur dans create_presentation_structure: {e}")
        # Retourner une structure par défaut en cas d'erreur
        default_structure = {
            "title": "Présentation Générée par IA",
            "slides": [
                {"title": "Introduction", "content": "Votre texte a été analysé avec succès"},
                {"title": "Points Principaux", "content": "Les insights clés ont été extraits"},
                {"title": "Conclusion", "content": "Synthèse des éléments importants"}
            ],
            "key_points": ["analyse", "texte", "présentation"],
            "analysis_metadata": {"statistics": {"word_count": len(texte.split())}}
        }
        return default_structure, texte[:200] + "..."

def generate_presentation_gradio(texte, style="professionnel"):
    """Version Gradio améliorée avec prévisualisation"""
    try:
        if not texte or len(texte.strip()) < 50:
            return None, None, "❌ Veuillez entrer au moins 50 caractères."

        logger.info(f"🚀 Génération IA pour {len(texte)} caractères")
        
        # Générer la structure
        structure, summary = create_presentation_structure(texte)
        filename = presentation_generator.generate_presentation(structure, style)

        # Créer une belle prévisualisation
        preview_html = create_preview_html(structure, summary)
        
        logger.info("✅ Présentation générée avec succès!")
        return filename, preview_html, f"🎉 Présentation générée! ({len(structure['slides'])} slides)"

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

def create_preview_html(structure, summary):
    """Crée une belle prévisualisation HTML des slides"""
    html_content = f"""

    <div style="font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto;">

        <h2 style="color: #2E86AB; border-bottom: 2px solid #2E86AB; padding-bottom: 10px;">

            📊 Aperçu de votre Présentation

        </h2>

        

        <div style="background: #f8f9fa; padding: 15px; border-radius: 10px; margin-bottom: 20px;">

            <h3 style="color: #2E86AB;">🎯 Titre Principal</h3>

            <p style="font-size: 18px; font-weight: bold;">{structure['title']}</p>

        </div>

        

        <div style="background: #e8f4f8; padding: 15px; border-radius: 10px; margin-bottom: 20px;">

            <h3 style="color: #2E86AB;">📝 Résumé IA</h3>

            <p>{summary}</p>

        </div>

        

        <h3 style="color: #2E86AB;">🔄 Structure des Slides</h3>

    """
    
    # Ajouter chaque slide
    for i, slide in enumerate(structure['slides']):
        html_content += f"""

        <div style="background: white; border: 2px solid #2E86AB; border-radius: 10px; padding: 15px; margin: 10px 0;">

            <div style="display: flex; align-items: center; margin-bottom: 10px;">

                <span style="background: #2E86AB; color: white; width: 30px; height: 30px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold;">

                    {i+1}

                </span>

                <h4 style="margin: 0 0 0 10px; color: #2E86AB;">{slide['title']}</h4>

            </div>

            <p style="margin: 0; color: #555;">{slide['content']}</p>

        </div>

        """
    
    # Ajouter les points clés
    html_content += f"""

        <div style="background: #fff3cd; padding: 15px; border-radius: 10px; margin-top: 20px;">

            <h3 style="color: #856404;">🎯 Points Clés Identifiés</h3>

            <div style="display: flex; flex-wrap: wrap; gap: 8px;">

    """
    
    for point in structure['key_points']:
        html_content += f'<span style="background: #856404; color: white; padding: 5px 10px; border-radius: 15px; font-size: 12px;">{point}</span>'
    
    html_content += """

            </div>

        </div>

    </div>

    """
    
    return html_content

def analyze_text_gradio(texte):
    """Version Gradio de votre api_analyze()"""
    try:
        if not texte or len(texte.strip()) < 10:
            return "❌ Texte trop court. Minimum 10 caractères."

        structure, summary = create_presentation_structure(texte)
        
        # Formatage pour l'interface Gradio
        result = f"""

## 📊 Analyse IA du Texte



**Statistiques:**

- {structure['analysis_metadata']['statistics']['word_count']} mots

- {structure['analysis_metadata']['statistics']['sentence_count']} phrases  

- {structure['analysis_metadata']['statistics']['paragraph_count']} paragraphes



**🎯 Thèmes identifiés:**

{', '.join(structure['key_points'][:8])}



**📝 Résumé:**

{summary}



**🏗️ Structure proposée:**

"""
        for i, slide in enumerate(structure['slides']):
            result += f"\n{i+1}. **{slide['title']}** - {slide['content'][:100]}..."

        return result

    except Exception as e:
        return f"❌ Erreur d'analyse: {str(e)}"

# INTERFACE PRINCIPALE AVEC AUTHENTIFICATION
with gr.Blocks(theme=gr.themes.Soft(), title="Générateur de Présentation IA") as demo:
    
    # Écran d'authentification
    with gr.Column(visible=True) as auth_screen:
        gr.Markdown("# 🔒 Accès Sécurisé")
        gr.Markdown("Veuillez entrer le code d'accès à 5 chiffres")
        
        with gr.Row():
            code_input = gr.Textbox(
                label="Code d'accès",
                type="text",
                max_lines=1,
                placeholder="Entrez les 5 chiffres...",
                elem_id="code_input"
            )
            verifier_btn = gr.Button("Vérifier", variant="primary")
        
        message_sortie = gr.Textbox(label="Statut", interactive=False)
    
    # Application principale (cachée au début)
    with gr.Column(visible=False) as app_screen:
        gr.Markdown("""

        # 🧠 Générateur de Présentation IA Intelligente

        **Powered by Lab_Math_and Labhp & CIE Label_Bertoua**

        

        Transformez votre texte en présentation PowerPoint professionnelle en quelques secondes !

        """)
        
        with gr.Tab("🚀 Générer Présentation"):
            with gr.Row():
                with gr.Column():
                    text_input = gr.Textbox(
                        label="📝 Collez votre texte ici",
                        placeholder="Collez ou tapez votre texte, article, rapport... (minimum 50 caractères)",
                        lines=12,
                        max_lines=20
                    )
                    style_dropdown = gr.Dropdown(
                        choices=["professionnel", "moderne", "creatif"],
                        label="🎨 Style de présentation",
                        value="professionnel",
                        info="Choisissez le style visuel de votre présentation"
                    )
                    generate_btn = gr.Button("🚀 Générer la Présentation", variant="primary", size="lg")
                
                with gr.Column():
                    output_file = gr.File(label="📥 Télécharger la Présentation", file_types=[".pptx"])
                    preview_output = gr.HTML(label="👁️ Aperçu de la Structure")
                    output_message = gr.Textbox(label="📋 Statut", interactive=False)
            
            generate_btn.click(
                fn=generate_presentation_gradio,
                inputs=[text_input, style_dropdown],
                outputs=[output_file, preview_output, output_message]
            )
        
        with gr.Tab("🔍 Analyser le Texte"):
            with gr.Row():
                with gr.Column():
                    analyze_text_input = gr.Textbox(
                        label="📝 Texte à analyser",
                        placeholder="Collez votre texte pour l'analyse IA...",
                        lines=8
                    )
                    analyze_btn = gr.Button("🔍 Analyser avec IA", variant="secondary")
                
                with gr.Column():
                    analysis_output = gr.Markdown(label="📊 Résultats de l'analyse")
            
            analyze_btn.click(
                fn=analyze_text_gradio,
                inputs=[analyze_text_input],
                outputs=[analysis_output]
            )

    def gerer_acces(code):
        """Gère l'authentification et affiche l'application si le code est correct"""
        est_valide, message = verifier_code(code)
        return (
            message, 
            gr.update(visible=not est_valide),  # Cacher l'écran d'auth
            gr.update(visible=est_valide)       # Afficher l'application
        )
    
    # Lier le bouton de vérification
    verifier_btn.click(
        fn=gerer_acces,
        inputs=[code_input],
        outputs=[message_sortie, auth_screen, app_screen]
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)