Equityone commited on
Commit
0f75b55
·
verified ·
1 Parent(s): d8815db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py CHANGED
@@ -430,6 +430,160 @@ class GenerationManager:
430
 
431
  # [Gardez votre classe ImageGenerator existante]
432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
433
  # Modifiez votre fonction create_interface comme ceci
434
  def create_interface():
435
  logger.info("Création de l'interface Gradio")
 
430
 
431
  # [Gardez votre classe ImageGenerator existante]
432
 
433
+ # Modifiez votre fonction create_interface comme ceci
434
+ def create_interface():
435
+ logger.info("Création de l'interface Gradio")
436
+
437
+ css = """
438
+ .container { max-width: 1200px; margin: auto; }
439
+ .welcome { text-align: center; margin: 20px 0; padding: 20px; background: #1e293b; border-radius: 10px; }
440
+ .controls-group { background: #2d3748; padding: 15px; border-radius: 5px; margin: 10px 0; }
441
+ .advanced-controls { background: #374151; padding: 12px; border-radius: 5px; margin: 8px 0; }
442
+ """
443
+
444
+ generator = ImageGenerator()
445
+ generation_manager = GenerationManager() # Ajout du gestionnaire
446
+
447
+ with gr.Blocks(css=css) as app:
448
+ # [Gardez toute votre interface existante jusqu'à la fin]
449
+
450
+ # Ajoutez cet onglet à la fin, avant les boutons de génération
451
+ with gr.Tab("📜 Historique"):
452
+ with gr.Row():
453
+ history_gallery = gr.Gallery(
454
+ label="Générations Récentes",
455
+ columns=3,
456
+ height=400,
457
+ show_label=True
458
+ )
459
+
460
+ with gr.Row():
461
+ refresh_btn = gr.Button("🔄 Actualiser")
462
+
463
+ # Modification de la fonction generate existante
464
+ def generate(*args):
465
+ logger.info("Démarrage d'une nouvelle génération")
466
+ params = {
467
+ "format_size": args[0],
468
+ "orientation": args[1],
469
+ "style": args[2],
470
+ "layout": args[3],
471
+ "ambiance": args[4],
472
+ "palette": args[5],
473
+ "subject": args[6],
474
+ "title": args[7],
475
+ "detail_level": args[8],
476
+ "contrast": args[9],
477
+ "saturation": args[10],
478
+ "quality": args[11],
479
+ "creativity": args[12]
480
+ }
481
+ result = generator.generate(params)
482
+
483
+ # Ajout de la sauvegarde automatique
484
+ if result[0] is not None:
485
+ generation_manager.save_generation(result[0], params)
486
+
487
+ logger.info(f"Génération terminée avec statut: {result[1]}")
488
+ return result
489
+
490
+ def refresh_history():
491
+ recent = generation_manager.get_recent_generations()
492
+ return gr.Gallery.update(value=[entry["image_path"] for entry in recent])
493
+
494
+ # Connexion des boutons
495
+ generate_btn.click(
496
+ generate,
497
+ inputs=[
498
+ format_size,
499
+ orientation,
500
+ style,
501
+ layout,
502
+ ambiance,
503
+ palette,
504
+ subject,
505
+ title,
506
+ detail_level,
507
+ contrast,
508
+ saturation,
509
+ quality,
510
+ creativity
511
+ ],
512
+ outputs=[image_output, status]
513
+ )
514
+
515
+ refresh_btn.click(
516
+ refresh_history,
517
+ outputs=[history_gallery]
518
+ )
519
+
520
+ clear_btn.click(
521
+ lambda: (None, "🗑️ Image effacée"),
522
+ outputs=[image_output, status]
523
+ )
524
+
525
+ logger.info("Interface créée avec succès")
526
+ return app
527
+ # [Gardez tous vos imports existants]
528
+ # Ajoutez ces imports en haut du fichier
529
+ from pathlib import Path
530
+ import time
531
+ import json
532
+ from datetime import datetime
533
+
534
+ # [Gardez toutes vos configurations existantes (ART_STYLES, etc.)]
535
+
536
+ # Ajoutez cette classe juste avant la classe ImageGenerator
537
+ class GenerationManager:
538
+ def __init__(self):
539
+ self.save_dir = Path("generations")
540
+ self.save_dir.mkdir(exist_ok=True)
541
+ self.history_file = self.save_dir / "history.json"
542
+ self.history = self.load_history()
543
+
544
+ def load_history(self):
545
+ if self.history_file.exists():
546
+ try:
547
+ with open(self.history_file, "r", encoding="utf-8") as f:
548
+ return json.load(f)
549
+ except Exception as e:
550
+ logger.error(f"Erreur chargement historique: {e}")
551
+ return []
552
+ return []
553
+
554
+ def save_generation(self, image, params):
555
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
556
+ image_name = f"generation_{timestamp}.png"
557
+ image_path = self.save_dir / image_name
558
+
559
+ # Sauvegarde de l'image
560
+ image.save(image_path)
561
+
562
+ # Création de l'entrée
563
+ entry = {
564
+ "id": timestamp,
565
+ "image_path": str(image_path),
566
+ "params": params,
567
+ "created_at": timestamp
568
+ }
569
+
570
+ self.history.append(entry)
571
+ self._save_history()
572
+ return entry
573
+
574
+ def _save_history(self):
575
+ with open(self.history_file, "w", encoding="utf-8") as f:
576
+ json.dump(self.history, f, indent=2, ensure_ascii=False)
577
+
578
+ def get_recent_generations(self, limit=10):
579
+ return sorted(
580
+ self.history,
581
+ key=lambda x: x["created_at"],
582
+ reverse=True
583
+ )[:limit]
584
+
585
+ # [Gardez votre classe ImageGenerator existante]
586
+
587
  # Modifiez votre fonction create_interface comme ceci
588
  def create_interface():
589
  logger.info("Création de l'interface Gradio")