Equityone commited on
Commit
3c1109a
·
verified ·
1 Parent(s): 05c6ee3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -61
app.py CHANGED
@@ -112,9 +112,7 @@ COMPOSITION_PARAMS = {
112
  "Terre": "earthy color palette, natural tones",
113
  "Néon": "neon color palette, vibrant glowing colors"
114
  }
115
- }
116
-
117
- class ImageGenerator:
118
  def __init__(self):
119
  self.API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
120
  token = os.getenv('HUGGINGFACE_TOKEN')
@@ -124,65 +122,40 @@ class ImageGenerator:
124
  logger.info("ImageGenerator initialisé")
125
 
126
  def _build_prompt(self, params: Dict[str, Any]) -> str:
127
- """Construction de prompt améliorée avec le PromptEnhancer"""
128
- try:
129
- # Initialisation du PromptEnhancer
130
- enhancer = PromptEnhancer()
131
- # Préparation du prompt initial
132
- base_prompt = f"{params['subject']}"
133
- if params.get('title'):
134
- base_prompt += f", with text '{params['title']}'"
135
-
136
- # Amélioration du prompt
137
- enhanced_prompt = enhancer.enhance_prompt(base_prompt, style_context)
138
-
139
- # Analyse de l'efficacité du prompt
140
- prompt_analysis = enhancer.analyze_prompt_effectiveness(enhanced_prompt)
141
- logger.debug(f"Analyse du prompt: {json.dumps(prompt_analysis, indent=2)}")
142
-
143
- return enhanced_prompt
144
- except Exception as e:
145
- logger.error(f"Erreur dans la construction du prompt: {str(e)}")
146
- # Fallback sur le prompt basique en cas d'erreur
147
- return f"{style_info['prompt_prefix']}, {params['subject']}"
148
-
149
- # Récupération du style
150
- style_info = ART_STYLES.get(params["style"], ART_STYLES["Art Moderne"])
151
-
152
- # Construction du contexte de style
153
- style_context = {
154
- "prompt_prefix": style_info['prompt_prefix'],
155
- "layout": COMPOSITION_PARAMS['Layouts'][params['layout']],
156
- "ambiance": COMPOSITION_PARAMS['Ambiances'][params['ambiance']],
157
- "palette": COMPOSITION_PARAMS['Palette'][params['palette']]
158
- }
159
-
160
- # Ajout des paramètres de composition
161
- if params.get("layout"):
162
- prompt += f", {COMPOSITION_PARAMS['Layouts'][params['layout']]}"
163
- if params.get("ambiance"):
164
- prompt += f", {COMPOSITION_PARAMS['Ambiances'][params['ambiance']]}"
165
- if params.get("palette"):
166
- prompt += f", {COMPOSITION_PARAMS['Palette'][params['palette']]}"
167
-
168
- # Ajout des ajustements fins
169
- if params.get("detail_level"):
170
- detail_strength = params["detail_level"]
171
- prompt += f", {'highly detailed, intricate' if detail_strength > 7 else 'moderately detailed'}"
172
-
173
- if params.get("contrast"):
174
- contrast_strength = params["contrast"]
175
- prompt += f", {'high contrast, dramatic lighting' if contrast_strength > 7 else 'balanced contrast'}"
176
-
177
- if params.get("saturation"):
178
- saturation_strength = params["saturation"]
179
- prompt += f", {'vibrant saturated colors' if saturation_strength > 7 else 'subtle muted colors'}"
180
-
181
- if params.get("title"):
182
- prompt += f", with text saying '{params['title']}'"
183
 
184
- logger.debug(f"Prompt final: {prompt}")
185
- return prompt
 
 
186
 
187
  def generate(self, params: Dict[str, Any]) -> Tuple[Optional[Image.Image], str]:
188
  try:
 
112
  "Terre": "earthy color palette, natural tones",
113
  "Néon": "neon color palette, vibrant glowing colors"
114
  }
115
+ }class ImageGenerator:
 
 
116
  def __init__(self):
117
  self.API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
118
  token = os.getenv('HUGGINGFACE_TOKEN')
 
122
  logger.info("ImageGenerator initialisé")
123
 
124
  def _build_prompt(self, params: Dict[str, Any]) -> str:
125
+ """Construction de prompt améliorée avec le PromptEnhancer"""
126
+ try:
127
+ # Initialisation du PromptEnhancer
128
+ enhancer = PromptEnhancer()
129
+
130
+ # Récupération du style
131
+ style_info = ART_STYLES.get(params["style"], ART_STYLES["Art Moderne"])
132
+
133
+ # Construction du contexte de style
134
+ style_context = {
135
+ "prompt_prefix": style_info['prompt_prefix'],
136
+ "layout": COMPOSITION_PARAMS['Layouts'][params['layout']],
137
+ "ambiance": COMPOSITION_PARAMS['Ambiances'][params['ambiance']],
138
+ "palette": COMPOSITION_PARAMS['Palette'][params['palette']]
139
+ }
140
+
141
+ # Préparation du prompt initial
142
+ base_prompt = f"{params['subject']}"
143
+ if params.get('title'):
144
+ base_prompt += f", with text '{params['title']}'"
145
+
146
+ # Amélioration du prompt
147
+ enhanced_prompt = enhancer.enhance_prompt(base_prompt, style_context)
148
+
149
+ # Analyse de l'efficacité du prompt
150
+ prompt_analysis = enhancer.analyze_prompt_effectiveness(enhanced_prompt)
151
+ logger.debug(f"Analyse du prompt: {json.dumps(prompt_analysis, indent=2)}")
152
+
153
+ return enhanced_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
+ except Exception as e:
156
+ logger.error(f"Erreur dans la construction du prompt: {str(e)}")
157
+ # Fallback sur le prompt basique en cas d'erreur
158
+ return f"{style_info['prompt_prefix']}, {params['subject']}"
159
 
160
  def generate(self, params: Dict[str, Any]) -> Tuple[Optional[Image.Image], str]:
161
  try: