sohfossted commited on
Commit
f6bf4b0
·
verified ·
1 Parent(s): cfc4fb9

Upload app.py

Browse files

New version of app Premium

Files changed (1) hide show
  1. app.py +58 -39
app.py CHANGED
@@ -28,7 +28,7 @@ def verifier_code(code):
28
  else:
29
  return False, "❌ Code incorrect. Veuillez réessayer."
30
 
31
- # FONCTIONS D'EXTRACTION DE TEXTE
32
  def extract_text_from_pdf(file_path):
33
  """Extrait le texte d'un fichier PDF"""
34
  try:
@@ -64,9 +64,14 @@ def extract_text_from_txt(file_path):
64
  raise Exception(f"Erreur lors de l'extraction du fichier texte: {str(e)}")
65
 
66
  def extract_text_from_file(file_obj):
67
- """Extrait le texte selon le format du fichier"""
68
  try:
69
- file_path = file_obj.name
 
 
 
 
 
70
  file_extension = os.path.splitext(file_path)[1].lower()
71
 
72
  logger.info(f"📁 Extraction du fichier: {file_path}")
@@ -212,14 +217,16 @@ def create_preview_html(structure, summary):
212
 
213
  return html_content
214
 
215
- ##### FONCTIONS UPLOAD AMÉLIORÉES #####
216
  def handle_file_upload(files):
217
- """Gère l'upload de fichiers et extrait le texte"""
218
  if not files:
219
  return None, None, "❌ Aucun fichier sélectionné."
220
 
221
  try:
222
- file_obj = files[0] # Premier fichier
 
 
223
  extracted_text = extract_text_from_file(file_obj)
224
 
225
  if not extracted_text or len(extracted_text.strip()) < 10:
@@ -274,6 +281,33 @@ def analyze_text_gradio(texte):
274
  except Exception as e:
275
  return f"❌ Erreur d'analyse: {str(e)}"
276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  # INTERFACE PRINCIPALE AVEC AUTHENTIFICATION
278
  with gr.Blocks(theme=gr.themes.Soft(), title="Générateur de Présentation IA") as demo:
279
 
@@ -348,7 +382,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Générateur de Présentation IA")
348
  file_count="single",
349
  size="lg"
350
  )
351
- uploaded_file_info = gr.Textbox(label="📋 Fichier sélectionné", interactive=False)
 
 
 
 
352
  extract_btn = gr.Button("🔍 Extraire et Analyser", variant="primary")
353
 
354
  with gr.Column():
@@ -356,9 +394,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Générateur de Présentation IA")
356
  label="📝 Texte extrait",
357
  interactive=False,
358
  lines=8,
359
- max_lines=12
 
 
 
 
 
 
360
  )
361
- upload_status = gr.Textbox(label="📊 Statut extraction", interactive=False)
362
 
363
  with gr.Row():
364
  file_style_dropdown = gr.Dropdown(
@@ -371,37 +414,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Générateur de Présentation IA")
371
  with gr.Row():
372
  file_output = gr.File(label="📥 Présentation Générée", file_types=[".pptx"])
373
  file_preview = gr.HTML(label="👁️ Aperçu")
374
- file_message = gr.Textbox(label="📋 Statut génération", interactive=False)
375
-
376
- with gr.Tab("🔍 Analyser le Texte"):
377
- with gr.Row():
378
- with gr.Column():
379
- analyze_text_input = gr.Textbox(
380
- label="📝 Texte à analyser",
381
- placeholder="Collez votre texte pour l'analyse IA...",
382
- lines=8
383
- )
384
- analyze_btn = gr.Button("🔍 Analyser avec IA", variant="secondary")
385
-
386
- with gr.Column():
387
- analysis_output = gr.Markdown(label="📊 Résultats de l'analyse")
388
-
389
- analyze_btn.click(
390
- fn=analyze_text_gradio,
391
- inputs=[analyze_text_input],
392
- outputs=[analysis_output]
393
- )
394
 
395
- # GESTION DES INTERACTIONS UPLOAD
396
- def update_file_info(files):
397
- """Met à jour les informations du fichier uploadé"""
398
- if files:
399
- file_obj = files[0]
400
- file_size = os.path.getsize(file_obj.name) / 1024 # KB
401
- return f"📄 {os.path.basename(file_obj.name)} ({file_size:.1f} KB)"
402
- return "Aucun fichier sélectionné"
403
-
404
- # Lier les événements upload
405
  upload_button.upload(
406
  fn=update_file_info,
407
  inputs=[upload_button],
 
28
  else:
29
  return False, "❌ Code incorrect. Veuillez réessayer."
30
 
31
+ # FONCTIONS D'EXTRACTION DE TEXTE CORRIGÉES
32
  def extract_text_from_pdf(file_path):
33
  """Extrait le texte d'un fichier PDF"""
34
  try:
 
64
  raise Exception(f"Erreur lors de l'extraction du fichier texte: {str(e)}")
65
 
66
  def extract_text_from_file(file_obj):
67
+ """Extrait le texte selon le format du fichier - VERSION CORRIGÉE"""
68
  try:
69
+ # GESTION CORRIGÉE DU TYPE DE FICHIER
70
+ if hasattr(file_obj, 'name'):
71
+ file_path = file_obj.name
72
+ else:
73
+ file_path = file_obj # C'est déjà un chemin
74
+
75
  file_extension = os.path.splitext(file_path)[1].lower()
76
 
77
  logger.info(f"📁 Extraction du fichier: {file_path}")
 
217
 
218
  return html_content
219
 
220
+ ##### FONCTIONS UPLOAD CORRIGÉES #####
221
  def handle_file_upload(files):
222
+ """Gère l'upload de fichiers et extrait le texte - VERSION CORRIGÉE"""
223
  if not files:
224
  return None, None, "❌ Aucun fichier sélectionné."
225
 
226
  try:
227
+ # CORRECTION : Gradio passe une liste, prendre le premier élément
228
+ file_obj = files[0] if isinstance(files, list) else files
229
+
230
  extracted_text = extract_text_from_file(file_obj)
231
 
232
  if not extracted_text or len(extracted_text.strip()) < 10:
 
281
  except Exception as e:
282
  return f"❌ Erreur d'analyse: {str(e)}"
283
 
284
+ # FONCTION CORRIGÉE POUR LES INFOS DE FICHIER
285
+ def update_file_info(files):
286
+ """Met à jour les informations du fichier uploadé - VERSION CORRIGÉE"""
287
+ if not files:
288
+ return "Aucun fichier sélectionné"
289
+
290
+ try:
291
+ # CORRECTION : Gradio passe une liste, prendre le premier élément
292
+ file_obj = files[0] if isinstance(files, list) else files
293
+
294
+ # Gestion des différents types d'objets fichier
295
+ if hasattr(file_obj, 'name'):
296
+ file_path = file_obj.name
297
+ elif isinstance(file_obj, str):
298
+ file_path = file_obj
299
+ else:
300
+ return "Type de fichier non supporté"
301
+
302
+ file_name = os.path.basename(file_path)
303
+ file_size = os.path.getsize(file_path) / 1024 # KB
304
+
305
+ return f"📄 {file_name} ({file_size:.1f} KB)"
306
+
307
+ except Exception as e:
308
+ logger.error(f"Erreur dans update_file_info: {e}")
309
+ return "Erreur lors de la lecture du fichier"
310
+
311
  # INTERFACE PRINCIPALE AVEC AUTHENTIFICATION
312
  with gr.Blocks(theme=gr.themes.Soft(), title="Générateur de Présentation IA") as demo:
313
 
 
382
  file_count="single",
383
  size="lg"
384
  )
385
+ uploaded_file_info = gr.Textbox(
386
+ label="📋 Fichier sélectionné",
387
+ interactive=False,
388
+ value="Aucun fichier sélectionné"
389
+ )
390
  extract_btn = gr.Button("🔍 Extraire et Analyser", variant="primary")
391
 
392
  with gr.Column():
 
394
  label="📝 Texte extrait",
395
  interactive=False,
396
  lines=8,
397
+ max_lines=12,
398
+ placeholder="Le texte extrait apparaîtra ici..."
399
+ )
400
+ upload_status = gr.Textbox(
401
+ label="📊 Statut extraction",
402
+ interactive=False,
403
+ value="En attente de fichier..."
404
  )
 
405
 
406
  with gr.Row():
407
  file_style_dropdown = gr.Dropdown(
 
414
  with gr.Row():
415
  file_output = gr.File(label="📥 Présentation Générée", file_types=[".pptx"])
416
  file_preview = gr.HTML(label="👁️ Aperçu")
417
+ file_message = gr.Textbox(
418
+ label="📋 Statut génération",
419
+ interactive=False,
420
+ value="En attente..."
421
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
 
423
+ # GESTION DES INTERACTIONS UPLOAD - VERSION CORRIGÉE
 
 
 
 
 
 
 
 
 
424
  upload_button.upload(
425
  fn=update_file_info,
426
  inputs=[upload_button],