Update app.py
Browse files
app.py
CHANGED
@@ -112,61 +112,6 @@ COMPOSITION_PARAMS = {
|
|
112 |
"Néon": "neon color palette, vibrant glowing colors"
|
113 |
}
|
114 |
}
|
115 |
-
class GenerationManager:
|
116 |
-
def __init__(self):
|
117 |
-
self.save_dir = Path("generations")
|
118 |
-
self.save_dir.mkdir(exist_ok=True)
|
119 |
-
self.history_file = self.save_dir / "history.json"
|
120 |
-
self.history = self.load_history()
|
121 |
-
logger.info("Système de génération initialisé")
|
122 |
-
|
123 |
-
def load_history(self):
|
124 |
-
if self.history_file.exists():
|
125 |
-
try:
|
126 |
-
with open(self.history_file, "r", encoding="utf-8") as f:
|
127 |
-
return json.load(f)
|
128 |
-
except Exception as e:
|
129 |
-
logger.error(f"Erreur chargement historique: {e}")
|
130 |
-
return []
|
131 |
-
return []
|
132 |
-
|
133 |
-
def save_generation(self, image, params):
|
134 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
135 |
-
image_name = f"generation_{timestamp}.png"
|
136 |
-
image_path = self.save_dir / image_name
|
137 |
-
|
138 |
-
try:
|
139 |
-
# Sauvegarde de l'image
|
140 |
-
image.save(image_path)
|
141 |
-
|
142 |
-
# Création de l'entrée
|
143 |
-
entry = {
|
144 |
-
"id": timestamp,
|
145 |
-
"image_path": str(image_path),
|
146 |
-
"params": params,
|
147 |
-
"created_at": timestamp
|
148 |
-
}
|
149 |
-
|
150 |
-
self.history.append(entry)
|
151 |
-
self._save_history()
|
152 |
-
return entry
|
153 |
-
except Exception as e:
|
154 |
-
logger.error(f"Erreur sauvegarde génération: {e}")
|
155 |
-
return None
|
156 |
-
|
157 |
-
def _save_history(self):
|
158 |
-
try:
|
159 |
-
with open(self.history_file, "w", encoding="utf-8") as f:
|
160 |
-
json.dump(self.history, f, indent=2, ensure_ascii=False)
|
161 |
-
except Exception as e:
|
162 |
-
logger.error(f"Erreur sauvegarde historique: {e}")
|
163 |
-
|
164 |
-
def get_recent_generations(self, limit=10):
|
165 |
-
return sorted(
|
166 |
-
self.history,
|
167 |
-
key=lambda x: x["created_at"],
|
168 |
-
reverse=True
|
169 |
-
)[:limit]
|
170 |
|
171 |
class ImageGenerator:
|
172 |
def __init__(self):
|
@@ -264,7 +209,6 @@ def create_interface():
|
|
264 |
"""
|
265 |
|
266 |
generator = ImageGenerator()
|
267 |
-
generation_manager = GenerationManager()
|
268 |
|
269 |
with gr.Blocks(css=css) as app:
|
270 |
gr.HTML("""
|
@@ -425,268 +369,4 @@ def create_interface():
|
|
425 |
if __name__ == "__main__":
|
426 |
app = create_interface()
|
427 |
logger.info("Démarrage de l'application")
|
428 |
-
app.launch()
|
429 |
-
# [Gardez tous vos imports existants]
|
430 |
-
# Ajoutez ces imports en haut du fichier
|
431 |
-
from pathlib import Path
|
432 |
-
import time
|
433 |
-
import json
|
434 |
-
from datetime import datetime
|
435 |
-
|
436 |
-
# [Gardez toutes vos configurations existantes (ART_STYLES, etc.)]
|
437 |
-
|
438 |
-
# Ajoutez cette classe juste avant la classe ImageGenerator
|
439 |
-
class GenerationManager:
|
440 |
-
def __init__(self):
|
441 |
-
self.save_dir = Path("generations")
|
442 |
-
self.save_dir.mkdir(exist_ok=True)
|
443 |
-
self.history_file = self.save_dir / "history.json"
|
444 |
-
self.history = self.load_history()
|
445 |
-
|
446 |
-
def load_history(self):
|
447 |
-
if self.history_file.exists():
|
448 |
-
try:
|
449 |
-
with open(self.history_file, "r", encoding="utf-8") as f:
|
450 |
-
return json.load(f)
|
451 |
-
except Exception as e:
|
452 |
-
logger.error(f"Erreur chargement historique: {e}")
|
453 |
-
return []
|
454 |
-
return []
|
455 |
-
|
456 |
-
def save_generation(self, image, params):
|
457 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
458 |
-
image_name = f"generation_{timestamp}.png"
|
459 |
-
image_path = self.save_dir / image_name
|
460 |
-
|
461 |
-
# Sauvegarde de l'image
|
462 |
-
image.save(image_path)
|
463 |
-
|
464 |
-
# Création de l'entrée
|
465 |
-
entry = {
|
466 |
-
"id": timestamp,
|
467 |
-
"image_path": str(image_path),
|
468 |
-
"params": params,
|
469 |
-
"created_at": timestamp
|
470 |
-
}
|
471 |
-
|
472 |
-
self.history.append(entry)
|
473 |
-
self._save_history()
|
474 |
-
return entry
|
475 |
-
|
476 |
-
def _save_history(self):
|
477 |
-
with open(self.history_file, "w", encoding="utf-8") as f:
|
478 |
-
json.dump(self.history, f, indent=2, ensure_ascii=False)
|
479 |
-
|
480 |
-
def get_recent_generations(self, limit=10):
|
481 |
-
return sorted(
|
482 |
-
self.history,
|
483 |
-
key=lambda x: x["created_at"],
|
484 |
-
reverse=True
|
485 |
-
)[:limit]
|
486 |
-
|
487 |
-
# [Gardez votre classe ImageGenerator existante]
|
488 |
-
|
489 |
-
# Modifiez votre fonction create_interface comme ceci
|
490 |
-
def create_interface():
|
491 |
-
def create_interface():
|
492 |
-
logger.info("Création de l'interface Gradio")
|
493 |
-
|
494 |
-
css = """
|
495 |
-
.container { max-width: 1200px; margin: auto; }
|
496 |
-
.welcome { text-align: center; margin: 20px 0; padding: 20px; background: #1e293b; border-radius: 10px; }
|
497 |
-
.controls-group { background: #2d3748; padding: 15px; border-radius: 5px; margin: 10px 0; }
|
498 |
-
.advanced-controls { background: #374151; padding: 12px; border-radius: 5px; margin: 8px 0; }
|
499 |
-
"""
|
500 |
-
|
501 |
-
generator = ImageGenerator()
|
502 |
-
generation_manager = GenerationManager()
|
503 |
-
|
504 |
-
with gr.Blocks(css=css) as app:
|
505 |
-
gr.HTML("""
|
506 |
-
<div class="welcome">
|
507 |
-
<h1>🎨 Equity Artisan 3.0</h1>
|
508 |
-
<p>Assistant de création d'affiches professionnelles</p>
|
509 |
-
</div>
|
510 |
-
""")
|
511 |
-
|
512 |
-
with gr.Column(elem_classes="container"):
|
513 |
-
# Format et Orientation
|
514 |
-
with gr.Group(elem_classes="controls-group"):
|
515 |
-
gr.Markdown("### 📐 Format et Orientation")
|
516 |
-
with gr.Row():
|
517 |
-
format_size = gr.Dropdown(
|
518 |
-
choices=["A4", "A3", "A2", "A1", "A0"],
|
519 |
-
value="A4",
|
520 |
-
label="Format"
|
521 |
-
)
|
522 |
-
orientation = gr.Radio(
|
523 |
-
choices=["Portrait", "Paysage"],
|
524 |
-
value="Portrait",
|
525 |
-
label="Orientation"
|
526 |
-
)
|
527 |
-
|
528 |
-
# Style et Composition
|
529 |
-
with gr.Group(elem_classes="controls-group"):
|
530 |
-
gr.Markdown("### 🎨 Style et Composition")
|
531 |
-
with gr.Row():
|
532 |
-
style = gr.Dropdown(
|
533 |
-
choices=list(ART_STYLES.keys()),
|
534 |
-
value="Art Moderne",
|
535 |
-
label="Style artistique"
|
536 |
-
)
|
537 |
-
layout = gr.Dropdown(
|
538 |
-
choices=list(COMPOSITION_PARAMS["Layouts"].keys()),
|
539 |
-
value="Centré",
|
540 |
-
label="Composition"
|
541 |
-
)
|
542 |
-
|
543 |
-
with gr.Row():
|
544 |
-
ambiance = gr.Dropdown(
|
545 |
-
choices=list(COMPOSITION_PARAMS["Ambiances"].keys()),
|
546 |
-
value="Dramatique",
|
547 |
-
label="Ambiance"
|
548 |
-
)
|
549 |
-
palette = gr.Dropdown(
|
550 |
-
choices=list(COMPOSITION_PARAMS["Palette"].keys()),
|
551 |
-
value="Contrasté",
|
552 |
-
label="Palette"
|
553 |
-
)
|
554 |
-
|
555 |
-
# Contenu
|
556 |
-
with gr.Group(elem_classes="controls-group"):
|
557 |
-
gr.Markdown("### 📝 Contenu")
|
558 |
-
subject = gr.Textbox(
|
559 |
-
label="Description",
|
560 |
-
placeholder="Décrivez votre vision..."
|
561 |
-
)
|
562 |
-
title = gr.Textbox(
|
563 |
-
label="Titre",
|
564 |
-
placeholder="Titre de l'affiche..."
|
565 |
-
)
|
566 |
-
|
567 |
-
# Ajustements fins
|
568 |
-
with gr.Group(elem_classes="advanced-controls"):
|
569 |
-
gr.Markdown("### 🎯 Ajustements Fins")
|
570 |
-
with gr.Row():
|
571 |
-
detail_level = gr.Slider(
|
572 |
-
minimum=1,
|
573 |
-
maximum=10,
|
574 |
-
value=7,
|
575 |
-
step=1,
|
576 |
-
label="Niveau de Détail"
|
577 |
-
)
|
578 |
-
contrast = gr.Slider(
|
579 |
-
minimum=1,
|
580 |
-
maximum=10,
|
581 |
-
value=5,
|
582 |
-
step=1,
|
583 |
-
label="Contraste"
|
584 |
-
)
|
585 |
-
saturation = gr.Slider(
|
586 |
-
minimum=1,
|
587 |
-
maximum=10,
|
588 |
-
value=5,
|
589 |
-
step=1,
|
590 |
-
label="Saturation"
|
591 |
-
)
|
592 |
-
|
593 |
-
# Paramètres de génération
|
594 |
-
with gr.Group(elem_classes="controls-group"):
|
595 |
-
with gr.Row():
|
596 |
-
quality = gr.Slider(
|
597 |
-
minimum=30,
|
598 |
-
maximum=50,
|
599 |
-
value=35,
|
600 |
-
label="Qualité"
|
601 |
-
)
|
602 |
-
creativity = gr.Slider(
|
603 |
-
minimum=5,
|
604 |
-
maximum=15,
|
605 |
-
value=7.5,
|
606 |
-
label="Créativité"
|
607 |
-
)
|
608 |
-
|
609 |
-
# Historique
|
610 |
-
with gr.Tab("📜 Historique"):
|
611 |
-
with gr.Row():
|
612 |
-
history_gallery = gr.Gallery(
|
613 |
-
label="Générations Récentes",
|
614 |
-
columns=3,
|
615 |
-
height=400,
|
616 |
-
show_label=True
|
617 |
-
)
|
618 |
-
|
619 |
-
with gr.Row():
|
620 |
-
refresh_btn = gr.Button("🔄 Actualiser")
|
621 |
-
|
622 |
-
# Boutons
|
623 |
-
with gr.Row():
|
624 |
-
generate_btn = gr.Button("✨ Générer", variant="primary")
|
625 |
-
clear_btn = gr.Button("🗑️ Effacer", variant="secondary")
|
626 |
-
|
627 |
-
image_output = gr.Image(label="Aperçu")
|
628 |
-
status = gr.Textbox(label="Statut", interactive=False)
|
629 |
-
|
630 |
-
# Fonctions
|
631 |
-
def generate(*args):
|
632 |
-
logger.info("Démarrage d'une nouvelle génération")
|
633 |
-
params = {
|
634 |
-
"format_size": args[0],
|
635 |
-
"orientation": args[1],
|
636 |
-
"style": args[2],
|
637 |
-
"layout": args[3],
|
638 |
-
"ambiance": args[4],
|
639 |
-
"palette": args[5],
|
640 |
-
"subject": args[6],
|
641 |
-
"title": args[7],
|
642 |
-
"detail_level": args[8],
|
643 |
-
"contrast": args[9],
|
644 |
-
"saturation": args[10],
|
645 |
-
"quality": args[11],
|
646 |
-
"creativity": args[12]
|
647 |
-
}
|
648 |
-
result = generator.generate(params)
|
649 |
-
|
650 |
-
if result[0] is not None:
|
651 |
-
generation_manager.save_generation(result[0], params)
|
652 |
-
|
653 |
-
logger.info(f"Génération terminée avec statut: {result[1]}")
|
654 |
-
return result
|
655 |
-
|
656 |
-
def refresh_history():
|
657 |
-
recent = generation_manager.get_recent_generations()
|
658 |
-
return gr.Gallery.update(value=[entry["image_path"] for entry in recent])
|
659 |
-
|
660 |
-
# Events
|
661 |
-
generate_btn.click(
|
662 |
-
generate,
|
663 |
-
inputs=[
|
664 |
-
format_size,
|
665 |
-
orientation,
|
666 |
-
style,
|
667 |
-
layout,
|
668 |
-
ambiance,
|
669 |
-
palette,
|
670 |
-
subject,
|
671 |
-
title,
|
672 |
-
detail_level,
|
673 |
-
contrast,
|
674 |
-
saturation,
|
675 |
-
quality,
|
676 |
-
creativity
|
677 |
-
],
|
678 |
-
outputs=[image_output, status]
|
679 |
-
)
|
680 |
-
|
681 |
-
refresh_btn.click(
|
682 |
-
refresh_history,
|
683 |
-
outputs=[history_gallery]
|
684 |
-
)
|
685 |
-
|
686 |
-
clear_btn.click(
|
687 |
-
lambda: (None, "🗑️ Image effacée"),
|
688 |
-
outputs=[image_output, status]
|
689 |
-
)
|
690 |
-
|
691 |
-
logger.info("Interface créée avec succès")
|
692 |
-
return app
|
|
|
112 |
"Néon": "neon color palette, vibrant glowing colors"
|
113 |
}
|
114 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
class ImageGenerator:
|
117 |
def __init__(self):
|
|
|
209 |
"""
|
210 |
|
211 |
generator = ImageGenerator()
|
|
|
212 |
|
213 |
with gr.Blocks(css=css) as app:
|
214 |
gr.HTML("""
|
|
|
369 |
if __name__ == "__main__":
|
370 |
app = create_interface()
|
371 |
logger.info("Démarrage de l'application")
|
372 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|