Update prompt_enhancer.py
Browse files- prompt_enhancer.py +43 -162
prompt_enhancer.py
CHANGED
@@ -1,182 +1,63 @@
|
|
1 |
-
from typing import Dict,
|
2 |
-
import logging
|
3 |
import re
|
4 |
|
5 |
-
logger = logging.getLogger(__name__)
|
6 |
-
|
7 |
class PromptEnhancer:
|
8 |
def __init__(self):
|
9 |
-
self.
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
"élégant": "elegant sophisticated refined",
|
21 |
-
|
22 |
-
# Éléments visuels
|
23 |
-
"logo": "prominent logo design professional branding",
|
24 |
-
"texte": "clear readable text typography",
|
25 |
-
"image": "main visual focal point image",
|
26 |
-
"photo": "photographic image realistic",
|
27 |
-
|
28 |
-
# Caractéristiques techniques
|
29 |
-
"haute qualité": "high quality professional grade",
|
30 |
-
"détaillé": "highly detailed intricate",
|
31 |
-
"net": "sharp crisp clear",
|
32 |
-
"flou": "soft focus gentle blur",
|
33 |
-
|
34 |
-
# Styles spécifiques
|
35 |
-
"3D": "three dimensional depth realistic",
|
36 |
-
"plat": "flat design 2D clean",
|
37 |
-
"graphique": "graphic design vector-style",
|
38 |
-
"illustré": "illustrated hand-drawn artistic"
|
39 |
-
}
|
40 |
-
|
41 |
-
self.composition_patterns = {
|
42 |
-
# Structure de l'affiche
|
43 |
-
"haut": "top aligned composition with {element}",
|
44 |
-
"bas": "bottom aligned composition with {element}",
|
45 |
-
"centre": "centered composition with {element}",
|
46 |
-
"gauche": "left aligned composition with {element}",
|
47 |
-
"droite": "right aligned composition with {element}",
|
48 |
-
|
49 |
-
# Relations spatiales
|
50 |
-
"au-dessus": "{element1} positioned above {element2}",
|
51 |
-
"en-dessous": "{element1} positioned below {element2}",
|
52 |
-
"à côté": "{element1} next to {element2}",
|
53 |
-
"autour": "{element1} surrounding {element2}"
|
54 |
-
}
|
55 |
-
|
56 |
-
self.emphasis_patterns = {
|
57 |
-
"important": "(({})),", # Double emphase
|
58 |
-
"normal": "({}),", # Emphase simple
|
59 |
-
"subtil": "[{}]," # Emphase légère
|
60 |
-
}
|
61 |
-
|
62 |
-
self.common_improvements = {
|
63 |
-
"faire": "create",
|
64 |
-
"mettre": "place",
|
65 |
-
"avec": "featuring",
|
66 |
-
"contenant": "containing",
|
67 |
-
"il y a": "featuring",
|
68 |
-
"je veux": "",
|
69 |
-
"je souhaite": "",
|
70 |
-
"il faut": ""
|
71 |
}
|
72 |
|
73 |
-
def
|
74 |
-
"""Améliore le prompt
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
# Nettoyage initial
|
79 |
-
enhanced_prompt = self._clean_prompt(enhanced_prompt)
|
80 |
-
|
81 |
-
# Détection et amélioration du contexte
|
82 |
-
enhanced_prompt = self._add_context(enhanced_prompt)
|
83 |
|
84 |
-
# Ajout des
|
85 |
-
|
|
|
86 |
|
87 |
-
#
|
88 |
-
enhanced_prompt =
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
logger.debug(f"Prompt amélioré: {enhanced_prompt}")
|
94 |
-
return enhanced_prompt
|
95 |
|
96 |
def _clean_prompt(self, prompt: str) -> str:
|
97 |
"""Nettoie et normalise le prompt"""
|
98 |
-
#
|
99 |
-
|
100 |
-
prompt = prompt.replace(old, new)
|
101 |
|
102 |
-
#
|
103 |
-
|
|
|
|
|
104 |
|
105 |
-
return
|
106 |
|
107 |
-
def
|
108 |
-
"""
|
109 |
-
|
110 |
words = prompt.split()
|
|
|
|
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
else:
|
116 |
-
enhanced_parts.append(word)
|
117 |
|
118 |
-
return " ".join(enhanced_parts)
|
119 |
-
|
120 |
-
def _add_style_elements(self, prompt: str, style_context: Dict) -> str:
|
121 |
-
"""Ajoute les éléments de style au prompt"""
|
122 |
-
style_elements = [
|
123 |
-
style_context.get("prompt_prefix", ""),
|
124 |
-
prompt,
|
125 |
-
style_context.get("layout", ""),
|
126 |
-
style_context.get("ambiance", ""),
|
127 |
-
style_context.get("palette", ""),
|
128 |
-
"professional poster design",
|
129 |
-
"high quality"
|
130 |
-
]
|
131 |
-
|
132 |
-
return ", ".join(filter(None, style_elements))
|
133 |
-
|
134 |
-
def _structure_prompt(self, prompt: str) -> str:
|
135 |
-
"""Structure le prompt avec une emphase appropriée"""
|
136 |
-
# Identifie les éléments clés
|
137 |
-
main_elements = self._identify_main_elements(prompt)
|
138 |
-
|
139 |
-
structured_parts = []
|
140 |
-
for element, importance in main_elements.items():
|
141 |
-
pattern = self.emphasis_patterns.get(importance, "{}")
|
142 |
-
structured_parts.append(pattern.format(element))
|
143 |
-
|
144 |
-
return " ".join(structured_parts)
|
145 |
-
|
146 |
-
def _identify_main_elements(self, prompt: str) -> Dict[str, str]:
|
147 |
-
"""Identifie les éléments principaux et leur importance"""
|
148 |
-
elements = {}
|
149 |
-
|
150 |
-
# Analyse basique des éléments clés
|
151 |
-
words = prompt.split()
|
152 |
for word in words:
|
153 |
-
if
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
elements[word] = "normal"
|
158 |
-
|
159 |
-
return elements
|
160 |
-
|
161 |
-
def _add_technical_qualifiers(self, prompt: str) -> str:
|
162 |
-
"""Ajoute des qualificatifs techniques pour améliorer la qualité"""
|
163 |
-
technical_qualifiers = [
|
164 |
-
"professional quality",
|
165 |
-
"highly detailed",
|
166 |
-
"masterful composition",
|
167 |
-
"perfect lighting",
|
168 |
-
"sharp focus",
|
169 |
-
"8k resolution"
|
170 |
-
]
|
171 |
|
172 |
-
return
|
173 |
-
|
174 |
-
def analyze_prompt_effectiveness(self, prompt: str) -> Dict:
|
175 |
-
"""Analyse l'efficacité du prompt"""
|
176 |
-
return {
|
177 |
-
"length": len(prompt),
|
178 |
-
"key_elements": len(self._identify_main_elements(prompt)),
|
179 |
-
"has_context": any(keyword in prompt for keyword in self.context_keywords),
|
180 |
-
"has_composition": any(pattern in prompt for pattern in self.composition_patterns),
|
181 |
-
"technical_quality": len([q for q in ["detailed", "quality", "professional"] if q in prompt])
|
182 |
-
}
|
|
|
1 |
+
from typing import Dict, Any, List
|
|
|
2 |
import re
|
3 |
|
|
|
|
|
4 |
class PromptEnhancer:
|
5 |
def __init__(self):
|
6 |
+
self.quality_terms = {
|
7 |
+
"Ultra Réaliste": [
|
8 |
+
"masterpiece", "best quality", "ultra realistic",
|
9 |
+
"photorealistic", "8k uhd", "high resolution",
|
10 |
+
"detailed", "sharp focus", "professional photography"
|
11 |
+
],
|
12 |
+
"Artistique Pro": [
|
13 |
+
"masterpiece", "best quality", "professional",
|
14 |
+
"detailed", "artistic", "perfect composition",
|
15 |
+
"award winning", "trending on artstation"
|
16 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
}
|
18 |
|
19 |
+
def enhance(self, prompt: str, style: str, composition: str, mood: str) -> str:
|
20 |
+
"""Améliore le prompt en ajoutant des termes de qualité et de style appropriés"""
|
21 |
+
# Nettoyage et normalisation du prompt
|
22 |
+
cleaned_prompt = self._clean_prompt(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Ajout des termes de qualité spécifiques au style
|
25 |
+
quality_terms = self.quality_terms.get(style, self.quality_terms["Ultra Réaliste"])
|
26 |
+
quality_string = ", ".join(quality_terms)
|
27 |
|
28 |
+
# Construction du prompt final
|
29 |
+
enhanced_prompt = f"{cleaned_prompt}, {quality_string}, {composition}, {mood}"
|
30 |
|
31 |
+
# Optimisation finale
|
32 |
+
return self._optimize_prompt(enhanced_prompt)
|
|
|
|
|
|
|
33 |
|
34 |
def _clean_prompt(self, prompt: str) -> str:
|
35 |
"""Nettoie et normalise le prompt"""
|
36 |
+
# Suppression des espaces multiples
|
37 |
+
cleaned = re.sub(r'\s+', ' ', prompt.strip())
|
|
|
38 |
|
39 |
+
# Suppression des termes de qualité redondants
|
40 |
+
redundant_terms = ["high quality", "good quality", "best quality", "hq"]
|
41 |
+
for term in redundant_terms:
|
42 |
+
cleaned = re.sub(rf'\b{term}\b', '', cleaned, flags=re.IGNORECASE)
|
43 |
|
44 |
+
return cleaned.strip()
|
45 |
|
46 |
+
def _optimize_prompt(self, prompt: str) -> str:
|
47 |
+
"""Optimisation finale du prompt"""
|
48 |
+
# Limitation de la longueur
|
49 |
words = prompt.split()
|
50 |
+
if len(words) > 77: # SDXL peut gérer jusqu'à 77 tokens
|
51 |
+
words = words[:77]
|
52 |
|
53 |
+
# Réorganisation pour mettre les termes importants en premier
|
54 |
+
important_terms = []
|
55 |
+
regular_terms = []
|
|
|
|
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
for word in words:
|
58 |
+
if word.lower() in ["masterpiece", "best quality", "professional"]:
|
59 |
+
important_terms.append(word)
|
60 |
+
else:
|
61 |
+
regular_terms.append(word)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
return ", ".join(important_terms + regular_terms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|