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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import time
|
| 5 |
+
import nltk
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from pptx import Presentation
|
| 8 |
+
from pptx.util import Inches, Pt
|
| 9 |
+
from pptx.dml.color import RGBColor
|
| 10 |
+
from pptx.enum.shapes import MSO_SHAPE
|
| 11 |
+
import PyPDF2
|
| 12 |
+
from docx import Document
|
| 13 |
+
|
| 14 |
+
# ==========================================
|
| 15 |
+
# PARTIE 1 : INTELLIGENCE & ANALYSE (AI)
|
| 16 |
+
# ==========================================
|
| 17 |
+
class AIEnhance:
|
| 18 |
+
def __init__(self):
|
| 19 |
+
try:
|
| 20 |
+
nltk.download('punkt', quiet=True)
|
| 21 |
+
nltk.download('punkt_tab', quiet=True)
|
| 22 |
+
except:
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
def create_intelligent_outline(self, text: str):
|
| 26 |
+
text = text.replace('\n', ' ').strip()
|
| 27 |
+
try:
|
| 28 |
+
sentences = nltk.sent_tokenize(text)
|
| 29 |
+
except:
|
| 30 |
+
sentences = [s.strip() for s in text.split('.') if len(s) > 10]
|
| 31 |
+
|
| 32 |
+
doc_type = self._detect_type(text)
|
| 33 |
+
data_points = [s for s in sentences if re.search(r'\d+%|\d+\s?millions|FCFA', s)]
|
| 34 |
+
|
| 35 |
+
outline = {
|
| 36 |
+
"title": sentences[0][:80] if sentences else "Analyse de Document",
|
| 37 |
+
"slides": []
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Slide de Titre
|
| 41 |
+
outline["slides"].append({
|
| 42 |
+
"type": "title",
|
| 43 |
+
"title": outline["title"].upper(),
|
| 44 |
+
"subtitle": f"DIVISION PROSPECTIVE | Lab_Math & Labhp\nType : {doc_type.upper()}"
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
# Slide de Plan (Sommaire)
|
| 48 |
+
plan = ["Contexte", "Analyse détaillée", "Résultats & Chiffres", "Recommandations", "Conclusion"]
|
| 49 |
+
outline["slides"].append({"type": "plan", "title": "PLAN DE PRÉSENTATION", "content": plan})
|
| 50 |
+
|
| 51 |
+
# Développement (Logique adaptative)
|
| 52 |
+
outline["slides"].append({"type": "content", "title": "I. INTRODUCTION ET CONTEXTE", "content": sentences[1:4]})
|
| 53 |
+
|
| 54 |
+
if data_points:
|
| 55 |
+
outline["slides"].append({"type": "content", "title": "II. ANALYSE DES DONNÉES CHIFFRÉES", "content": data_points[:5]})
|
| 56 |
+
|
| 57 |
+
outline["slides"].append({"type": "content", "title": "III. ENJEUX ET DÉFIS", "content": sentences[4:8][:4]})
|
| 58 |
+
outline["slides"].append({"type": "content", "title": "IV. RECOMMANDATIONS STRATÉGIQUES", "content": sentences[-6:-2]})
|
| 59 |
+
|
| 60 |
+
# Slide de Remerciements Multilingues
|
| 61 |
+
outline["slides"].append({
|
| 62 |
+
"type": "thanks",
|
| 63 |
+
"title": "MERCI POUR VOTRE ATTENTION",
|
| 64 |
+
"content": ["Thank you (English)", "Gracias (Español)", "Danke (Deutsch)", "شكra لك (Arab)", "Asante (Swahili)", "Merci beaucoup"]
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
return outline
|
| 68 |
+
|
| 69 |
+
def _detect_type(self, text):
|
| 70 |
+
t = text.lower()
|
| 71 |
+
if any(x in t for x in ['méthode', 'résultats', 'étude']): return "Rapport d'Étude"
|
| 72 |
+
if any(x in t for x in ['procès', 'réunion', 'ordre du jour']): return "Compte-rendu"
|
| 73 |
+
return "Note de Synthèse"
|
| 74 |
+
|
| 75 |
+
# ==========================================
|
| 76 |
+
# PARTIE 2 : DESIGN & GÉNÉRATION (PPTX)
|
| 77 |
+
# ==========================================
|
| 78 |
+
class PresentationGenerator:
|
| 79 |
+
def __init__(self):
|
| 80 |
+
self.blue_primary = RGBColor(0, 51, 102) # Bleu Lab_Math
|
| 81 |
+
self.gold_accent = RGBColor(218, 165, 32)
|
| 82 |
+
|
| 83 |
+
def generate(self, outline):
|
| 84 |
+
prs = Presentation()
|
| 85 |
+
prs.slide_width, prs.slide_height = Inches(13.33), Inches(7.5) # Format 16:9
|
| 86 |
+
|
| 87 |
+
for data in outline["slides"]:
|
| 88 |
+
stype = data["type"]
|
| 89 |
+
layout = prs.slide_layouts[0] if stype == "title" else prs.slide_layouts[1]
|
| 90 |
+
slide = prs.slides.add_slide(layout)
|
| 91 |
+
|
| 92 |
+
# --- DESIGN : Bandeau de Design Lab_Math ---
|
| 93 |
+
# Rectangle décoratif en haut
|
| 94 |
+
header_rect = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, 0, 0, prs.slide_width, Inches(0.15))
|
| 95 |
+
header_rect.fill.solid()
|
| 96 |
+
header_rect.fill.fore_color.rgb = self.blue_primary
|
| 97 |
+
header_rect.line.fill.background()
|
| 98 |
+
|
| 99 |
+
# --- REMPLISSAGE ---
|
| 100 |
+
title_shape = slide.shapes.title
|
| 101 |
+
title_shape.text = data["title"]
|
| 102 |
+
title_shape.text_frame.paragraphs[0].font.color.rgb = self.blue_primary
|
| 103 |
+
title_shape.text_frame.paragraphs[0].font.bold = True
|
| 104 |
+
|
| 105 |
+
if stype == "title":
|
| 106 |
+
if slide.placeholders[1]:
|
| 107 |
+
slide.placeholders[1].text = data["subtitle"]
|
| 108 |
+
else:
|
| 109 |
+
body = slide.placeholders[1]
|
| 110 |
+
tf = body.text_frame
|
| 111 |
+
tf.word_wrap = True
|
| 112 |
+
for point in data["content"]:
|
| 113 |
+
p = tf.add_paragraph()
|
| 114 |
+
p.text = f" • {point}"
|
| 115 |
+
p.font.size = Pt(22)
|
| 116 |
+
p.space_before = Pt(12)
|
| 117 |
+
|
| 118 |
+
# Signature bas de page
|
| 119 |
+
self._add_footer(slide)
|
| 120 |
+
|
| 121 |
+
output_path = f"Presentation_LabMath_{int(time.time())}.pptx"
|
| 122 |
+
prs.save(output_path)
|
| 123 |
+
return output_path
|
| 124 |
+
|
| 125 |
+
def _add_footer(self, slide):
|
| 126 |
+
tx = slide.shapes.add_textbox(Inches(10.5), Inches(7.1), Inches(2.5), Inches(0.4))
|
| 127 |
+
tx.text_frame.text = "© 2024 Lab_Math & Labhp"
|
| 128 |
+
p = tx.text_frame.paragraphs[0]
|
| 129 |
+
p.font.size = Pt(9)
|
| 130 |
+
p.font.color.rgb = RGBColor(150, 150, 150)
|
| 131 |
+
|
| 132 |
+
# ==========================================
|
| 133 |
+
# PARTIE 3 : INTERFACE & LOGIQUE DE FICHIER
|
| 134 |
+
# ==========================================
|
| 135 |
+
ai = AIEnhance()
|
| 136 |
+
gen = PresentationGenerator()
|
| 137 |
+
|
| 138 |
+
def extract_text(file_obj):
|
| 139 |
+
if file_obj.name.endswith('.pdf'):
|
| 140 |
+
reader = PyPDF2.PdfReader(file_obj.name)
|
| 141 |
+
return " ".join([p.extract_text() for p in reader.pages if p.extract_text()])
|
| 142 |
+
elif file_obj.name.endswith('.docx'):
|
| 143 |
+
doc = Document(file_obj.name)
|
| 144 |
+
return " ".join([p.text for p in doc.paragraphs])
|
| 145 |
+
else:
|
| 146 |
+
with open(file_obj.name, 'r', encoding='utf-8', errors='ignore') as f:
|
| 147 |
+
return f.read()
|
| 148 |
+
|
| 149 |
+
def main_process(input_file):
|
| 150 |
+
if input_file is None: return None, "Erreur : Aucun fichier uploadé."
|
| 151 |
+
try:
|
| 152 |
+
text = extract_text(input_file)
|
| 153 |
+
if len(text) < 50: return None, "Le document est trop court pour être analysé."
|
| 154 |
+
|
| 155 |
+
outline = ai.create_intelligent_outline(text)
|
| 156 |
+
pptx_file = gen.generate(outline)
|
| 157 |
+
return pptx_file, "✅ Présentation générée avec succès !"
|
| 158 |
+
except Exception as e:
|
| 159 |
+
return None, f"⚠️ Erreur système : {str(e)}"
|
| 160 |
+
|
| 161 |
+
# Interface utilisateur Gradio
|
| 162 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 163 |
+
gr.Markdown("""
|
| 164 |
+
# 🎤 Lab_Math & Labhp AI Presentation Builder
|
| 165 |
+
### Système Intelligent de Génération de Slides Professionnels
|
| 166 |
+
Déposez vos documents (PDF, Word, TXT) pour une transformation automatique.
|
| 167 |
+
""")
|
| 168 |
+
|
| 169 |
+
with gr.Row():
|
| 170 |
+
with gr.Column(scale=1):
|
| 171 |
+
file_input = gr.File(label="Déposer le document ici", file_types=[".pdf", ".docx", ".txt"])
|
| 172 |
+
generate_btn = gr.Button("🚀 GÉNÉRER LA PRÉSENTATION", variant="primary")
|
| 173 |
+
|
| 174 |
+
with gr.Column(scale=1):
|
| 175 |
+
file_output = gr.File(label="Télécharger votre PowerPoint (.pptx)")
|
| 176 |
+
status_text = gr.Textbox(label="Statut du traitement", interactive=False)
|
| 177 |
+
|
| 178 |
+
generate_btn.click(
|
| 179 |
+
fn=main_process,
|
| 180 |
+
inputs=file_input,
|
| 181 |
+
outputs=[file_output, status_text]
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
if __name__ == "__main__":
|
| 185 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
python-pptx
|
| 3 |
-
python-docx
|
| 4 |
-
PyPDF2
|
| 5 |
-
nltk
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
python-pptx
|
| 3 |
+
python-docx
|
| 4 |
+
PyPDF2
|
| 5 |
+
nltk
|