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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -1
app.py CHANGED
@@ -369,4 +369,158 @@ def create_interface():
369
  if __name__ == "__main__":
370
  app = create_interface()
371
  logger.info("Démarrage de l'application")
372
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  if __name__ == "__main__":
370
  app = create_interface()
371
  logger.info("Démarrage de l'application")
372
+ app.launch()
373
+ # [Gardez tous vos imports existants]
374
+ # Ajoutez ces imports en haut du fichier
375
+ from pathlib import Path
376
+ import time
377
+ import json
378
+ from datetime import datetime
379
+
380
+ # [Gardez toutes vos configurations existantes (ART_STYLES, etc.)]
381
+
382
+ # Ajoutez cette classe juste avant la classe ImageGenerator
383
+ class GenerationManager:
384
+ def __init__(self):
385
+ self.save_dir = Path("generations")
386
+ self.save_dir.mkdir(exist_ok=True)
387
+ self.history_file = self.save_dir / "history.json"
388
+ self.history = self.load_history()
389
+
390
+ def load_history(self):
391
+ if self.history_file.exists():
392
+ try:
393
+ with open(self.history_file, "r", encoding="utf-8") as f:
394
+ return json.load(f)
395
+ except Exception as e:
396
+ logger.error(f"Erreur chargement historique: {e}")
397
+ return []
398
+ return []
399
+
400
+ def save_generation(self, image, params):
401
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
402
+ image_name = f"generation_{timestamp}.png"
403
+ image_path = self.save_dir / image_name
404
+
405
+ # Sauvegarde de l'image
406
+ image.save(image_path)
407
+
408
+ # Création de l'entrée
409
+ entry = {
410
+ "id": timestamp,
411
+ "image_path": str(image_path),
412
+ "params": params,
413
+ "created_at": timestamp
414
+ }
415
+
416
+ self.history.append(entry)
417
+ self._save_history()
418
+ return entry
419
+
420
+ def _save_history(self):
421
+ with open(self.history_file, "w", encoding="utf-8") as f:
422
+ json.dump(self.history, f, indent=2, ensure_ascii=False)
423
+
424
+ def get_recent_generations(self, limit=10):
425
+ return sorted(
426
+ self.history,
427
+ key=lambda x: x["created_at"],
428
+ reverse=True
429
+ )[:limit]
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