Spaces:
Sleeping
Sleeping
File size: 18,734 Bytes
cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 f6bf4b0 cfc4fb9 | 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | import gradio as gr
import os
import tempfile
from datetime import datetime
import logging
import json
import PyPDF2
from docx import Document
# Configuration logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialisation des services
from ai_enhance import AIEnhance
from presentation_generator import PresentationGenerator
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."
# FONCTIONS D'EXTRACTION DE TEXTE CORRIGÉES
def extract_text_from_pdf(file_path):
"""Extrait le texte d'un fichier PDF"""
try:
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text.strip()
except Exception as e:
logger.error(f"Erreur extraction PDF: {e}")
raise Exception(f"Erreur lors de l'extraction du PDF: {str(e)}")
def extract_text_from_docx(file_path):
"""Extrait le texte d'un fichier Word"""
try:
doc = Document(file_path)
text = ""
for paragraph in doc.paragraphs:
text += paragraph.text + "\n"
return text.strip()
except Exception as e:
logger.error(f"Erreur extraction DOCX: {e}")
raise Exception(f"Erreur lors de l'extraction du document Word: {str(e)}")
def extract_text_from_txt(file_path):
"""Extrait le texte d'un fichier texte"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
return file.read().strip()
except Exception as e:
logger.error(f"Erreur extraction TXT: {e}")
raise Exception(f"Erreur lors de l'extraction du fichier texte: {str(e)}")
def extract_text_from_file(file_obj):
"""Extrait le texte selon le format du fichier - VERSION CORRIGÉE"""
try:
# GESTION CORRIGÉE DU TYPE DE FICHIER
if hasattr(file_obj, 'name'):
file_path = file_obj.name
else:
file_path = file_obj # C'est déjà un chemin
file_extension = os.path.splitext(file_path)[1].lower()
logger.info(f"📁 Extraction du fichier: {file_path}")
if file_extension == '.pdf':
return extract_text_from_pdf(file_path)
elif file_extension in ['.doc', '.docx']:
return extract_text_from_docx(file_path)
elif file_extension == '.txt':
return extract_text_from_txt(file_path)
else:
raise Exception(f"Format non supporté: {file_extension}")
except Exception as e:
logger.error(f"❌ Erreur extraction: {e}")
raise
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('.'),
"paragraph_count": len([p for p in texte.split('\n\n') if p.strip()])
},
"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"}
],
"recommended_style": "professionnel"
},
"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
##### FONCTIONS UPLOAD CORRIGÉES #####
def handle_file_upload(files):
"""Gère l'upload de fichiers et extrait le texte - VERSION CORRIGÉE"""
if not files:
return None, None, "❌ Aucun fichier sélectionné."
try:
# CORRECTION : Gradio passe une liste, prendre le premier élément
file_obj = files[0] if isinstance(files, list) else files
extracted_text = extract_text_from_file(file_obj)
if not extracted_text or len(extracted_text.strip()) < 10:
return None, None, "❌ Le fichier semble vide ou ne contient pas de texte extractible."
# Statistiques
word_count = len(extracted_text.split())
char_count = len(extracted_text)
message = f"✅ Fichier traité avec succès! 📊 {word_count} mots, {char_count} caractères"
return extracted_text, extracted_text, message
except Exception as e:
logger.error(f"Erreur upload: {e}")
return None, None, f"❌ Erreur: {str(e)}"
def generate_from_uploaded_text(analyzed_text, style="professionnel"):
"""Génère la présentation à partir du texte uploadé"""
return generate_presentation_gradio(analyzed_text, style)
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)}"
# FONCTION CORRIGÉE POUR LES INFOS DE FICHIER
def update_file_info(files):
"""Met à jour les informations du fichier uploadé - VERSION CORRIGÉE"""
if not files:
return "Aucun fichier sélectionné"
try:
# CORRECTION : Gradio passe une liste, prendre le premier élément
file_obj = files[0] if isinstance(files, list) else files
# Gestion des différents types d'objets fichier
if hasattr(file_obj, 'name'):
file_path = file_obj.name
elif isinstance(file_obj, str):
file_path = file_obj
else:
return "Type de fichier non supporté"
file_name = os.path.basename(file_path)
file_size = os.path.getsize(file_path) / 1024 # KB
return f"📄 {file_name} ({file_size:.1f} KB)"
except Exception as e:
logger.error(f"Erreur dans update_file_info: {e}")
return "Erreur lors de la lecture du fichier"
# 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 à partir de Texte"):
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("📁 Générer à partir de Fichier"):
gr.Markdown("### 📤 Uploader votre document")
gr.Markdown("""
**Formats supportés:**
- 📄 PDF (rapports, articles)
- 📝 DOC/DOCX (documents Word)
- 📋 TXT (fichiers texte)
""")
with gr.Row():
with gr.Column():
upload_button = gr.UploadButton(
"📁 Choisir un fichier",
file_types=[".pdf", ".doc", ".docx", ".txt"],
file_count="single",
size="lg"
)
uploaded_file_info = gr.Textbox(
label="📋 Fichier sélectionné",
interactive=False,
value="Aucun fichier sélectionné"
)
extract_btn = gr.Button("🔍 Extraire et Analyser", variant="primary")
with gr.Column():
extracted_text = gr.Textbox(
label="📝 Texte extrait",
interactive=False,
lines=8,
max_lines=12,
placeholder="Le texte extrait apparaîtra ici..."
)
upload_status = gr.Textbox(
label="📊 Statut extraction",
interactive=False,
value="En attente de fichier..."
)
with gr.Row():
file_style_dropdown = gr.Dropdown(
choices=["professionnel", "moderne", "creatif"],
label="🎨 Style de présentation",
value="professionnel"
)
generate_from_file_btn = gr.Button("🚀 Générer la Présentation", variant="primary")
with gr.Row():
file_output = gr.File(label="📥 Présentation Générée", file_types=[".pptx"])
file_preview = gr.HTML(label="👁️ Aperçu")
file_message = gr.Textbox(
label="📋 Statut génération",
interactive=False,
value="En attente..."
)
# GESTION DES INTERACTIONS UPLOAD - VERSION CORRIGÉE
upload_button.upload(
fn=update_file_info,
inputs=[upload_button],
outputs=[uploaded_file_info]
)
extract_btn.click(
fn=handle_file_upload,
inputs=[upload_button],
outputs=[extracted_text, extracted_text, upload_status]
)
generate_from_file_btn.click(
fn=generate_from_uploaded_text,
inputs=[extracted_text, file_style_dropdown],
outputs=[file_output, file_preview, file_message]
)
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,
share=False
) |