Spaces:
Sleeping
Sleeping
| 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) |