Update prompt_enhancer.py
Browse files- prompt_enhancer.py +117 -5
prompt_enhancer.py
CHANGED
@@ -54,15 +54,127 @@ class PromptEnhancer:
|
|
54 |
}
|
55 |
|
56 |
self.emphasis_patterns = {
|
57 |
-
"important": "(({})),",
|
58 |
-
"normal": "({}),",
|
59 |
-
"subtil": "[{}],"
|
60 |
}
|
61 |
|
62 |
-
# Expressions courantes à optimiser
|
63 |
self.common_improvements = {
|
64 |
"faire": "create",
|
65 |
"mettre": "place",
|
66 |
"avec": "featuring",
|
67 |
"contenant": "containing",
|
68 |
-
"il y a": "featuring"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
}def enhance_prompt(self, user_input: str, style_context: Dict) -> str:
|
72 |
+
"""Améliore le prompt utilisateur avec un contexte enrichi"""
|
73 |
+
enhanced_prompt = user_input.lower()
|
74 |
+
logger.debug(f"Prompt initial: {enhanced_prompt}")
|
75 |
+
|
76 |
+
# Nettoyage initial
|
77 |
+
enhanced_prompt = self._clean_prompt(enhanced_prompt)
|
78 |
+
|
79 |
+
# Détection et amélioration du contexte
|
80 |
+
enhanced_prompt = self._add_context(enhanced_prompt)
|
81 |
+
|
82 |
+
# Ajout des éléments de style
|
83 |
+
enhanced_prompt = self._add_style_elements(enhanced_prompt, style_context)
|
84 |
+
|
85 |
+
# Structure et emphase
|
86 |
+
enhanced_prompt = self._structure_prompt(enhanced_prompt)
|
87 |
+
|
88 |
+
# Ajout des qualificatifs techniques
|
89 |
+
enhanced_prompt = self._add_technical_qualifiers(enhanced_prompt)
|
90 |
+
|
91 |
+
logger.debug(f"Prompt amélioré: {enhanced_prompt}")
|
92 |
+
return enhanced_prompt
|
93 |
+
|
94 |
+
def _clean_prompt(self, prompt: str) -> str:
|
95 |
+
"""Nettoie et normalise le prompt"""
|
96 |
+
# Remplace les expressions communes par leurs versions optimisées
|
97 |
+
for old, new in self.common_improvements.items():
|
98 |
+
prompt = prompt.replace(old, new)
|
99 |
+
|
100 |
+
# Supprime les espaces multiples
|
101 |
+
prompt = " ".join(prompt.split())
|
102 |
+
|
103 |
+
return prompt
|
104 |
+
|
105 |
+
def _add_context(self, prompt: str) -> str:
|
106 |
+
"""Ajoute du contexte basé sur les mots-clés détectés"""
|
107 |
+
enhanced_parts = []
|
108 |
+
words = prompt.split()
|
109 |
+
|
110 |
+
for word in words:
|
111 |
+
if word in self.context_keywords:
|
112 |
+
enhanced_parts.append(self.context_keywords[word])
|
113 |
+
else:
|
114 |
+
enhanced_parts.append(word)
|
115 |
+
|
116 |
+
return " ".join(enhanced_parts)
|
117 |
+
|
118 |
+
def _add_style_elements(self, prompt: str, style_context: Dict) -> str:
|
119 |
+
"""Ajoute les éléments de style au prompt"""
|
120 |
+
style_elements = [
|
121 |
+
style_context.get("prompt_prefix", ""),
|
122 |
+
prompt,
|
123 |
+
style_context.get("layout", ""),
|
124 |
+
style_context.get("ambiance", ""),
|
125 |
+
style_context.get("palette", ""),
|
126 |
+
"professional poster design",
|
127 |
+
"high quality"
|
128 |
+
]
|
129 |
+
|
130 |
+
return ", ".join(filter(None, style_elements))
|
131 |
+
|
132 |
+
def _structure_prompt(self, prompt: str) -> str:
|
133 |
+
"""Structure le prompt avec une emphase appropriée"""
|
134 |
+
# Identifie les éléments clés
|
135 |
+
main_elements = self._identify_main_elements(prompt)
|
136 |
+
|
137 |
+
structured_parts = []
|
138 |
+
for element, importance in main_elements.items():
|
139 |
+
pattern = self.emphasis_patterns.get(importance, "{}")
|
140 |
+
structured_parts.append(pattern.format(element))
|
141 |
+
|
142 |
+
return " ".join(structured_parts)
|
143 |
+
|
144 |
+
def _identify_main_elements(self, prompt: str) -> Dict[str, str]:
|
145 |
+
"""Identifie les éléments principaux et leur importance"""
|
146 |
+
elements = {}
|
147 |
+
|
148 |
+
# Analyse basique des éléments clés
|
149 |
+
words = prompt.split()
|
150 |
+
for word in words:
|
151 |
+
if len(word) > 3: # Ignore les mots très courts
|
152 |
+
if word in self.context_keywords:
|
153 |
+
elements[word] = "important"
|
154 |
+
else:
|
155 |
+
elements[word] = "normal"
|
156 |
+
|
157 |
+
return elements
|
158 |
+
|
159 |
+
def _add_technical_qualifiers(self, prompt: str) -> str:
|
160 |
+
"""Ajoute des qualificatifs techniques pour améliorer la qualité"""
|
161 |
+
technical_qualifiers = [
|
162 |
+
"professional quality",
|
163 |
+
"highly detailed",
|
164 |
+
"masterful composition",
|
165 |
+
"perfect lighting",
|
166 |
+
"sharp focus",
|
167 |
+
"8k resolution"
|
168 |
+
]
|
169 |
+
|
170 |
+
return f"{prompt}, {', '.join(technical_qualifiers)}"
|
171 |
+
|
172 |
+
def analyze_prompt_effectiveness(self, prompt: str) -> Dict:
|
173 |
+
"""Analyse l'efficacité du prompt"""
|
174 |
+
return {
|
175 |
+
"length": len(prompt),
|
176 |
+
"key_elements": len(self._identify_main_elements(prompt)),
|
177 |
+
"has_context": any(keyword in prompt for keyword in self.context_keywords),
|
178 |
+
"has_composition": any(pattern in prompt for pattern in self.composition_patterns),
|
179 |
+
"technical_quality": len([q for q in ["detailed", "quality", "professional"] if q in prompt])
|
180 |
+
}
|