File size: 10,805 Bytes
c290e43 c645839 343349d c645839 343349d 2bc8286 c645839 6ff0647 343349d 89f7e0d 343349d c645839 343349d c645839 343349d c645839 343349d c645839 343349d 6ff0647 d20b726 343349d 6ff0647 c645839 343349d 5647bfd 343349d 6ff0647 343349d 6ff0647 343349d 6ff0647 343349d c645839 343349d c645839 d20b726 c645839 343349d c645839 343349d c645839 343349d d20b726 343349d d20b726 343349d c645839 343349d c645839 343349d c645839 343349d 6ff0647 343349d 6ff0647 343349d 6ff0647 343349d 6ff0647 c645839 343349d c645839 343349d c645839 343349d c645839 343349d 6ff0647 c645839 343349d c645839 343349d c645839 5647bfd c645839 343349d 5647bfd 6ff0647 343349d 6ff0647 5647bfd aa12eaa 343349d c645839 343349d c645839 343349d c645839 343349d aa12eaa 343349d 5647bfd 6ff0647 b09f3fe 2bc8286 343349d c645839 343349d c645839 343349d c645839 6ff0647 c645839 343349d c645839 2bc8286 aa12eaa c645839 c290e43 c645839 d6bf8a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
import gradio as gr
import os
from PIL import Image, ImageEnhance
import requests
import io
import gc
import json
from typing import Tuple, Optional, Dict, Any
import logging
from dotenv import load_dotenv
import numpy as np
import cv2
# Configuration du logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('equity_space.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
load_dotenv()
# 1. STYLES EQUITY (basés sur les 8 documents)
ART_STYLES = {
"Renaissance Technologique": {
"prompt_prefix": """ultra-detailed technical drawing, da vinci style engineering precision,
anatomical accuracy, golden ratio proportions, scientific illustration quality,
masterful light and shadow, highest level of detail, museum quality artwork""",
"negative_prompt": "simple sketch, imprecise, cartoon, abstract, low detail",
"quality_boost": 1.5,
"style_params": {
"detail_level": "maximum",
"technical_precision": True,
"historical_accuracy": True
}
},
"Innovation Moderne": {
"prompt_prefix": """cutting-edge technological design, futuristic engineering visual,
advanced mechanical detail, precise technical schematic, innovative concept art,
professional industrial visualization, high-tech aesthetic""",
"negative_prompt": "vintage, retro, simplified, abstract",
"quality_boost": 1.4,
"style_params": {
"tech_detail": "ultra",
"innovation_focus": True
}
},
"Photographie Analytique": {
"prompt_prefix": """professional analytical photography, ansel adams zone system,
ultra sharp detail, perfect exposure, museum grade quality, technical perfection,
masterful composition, extreme clarity""",
"negative_prompt": "blurry, artistic, painterly, low quality",
"quality_boost": 1.5,
"style_params": {
"photographic_precision": True,
"detail_preservation": "maximum"
}
},
"Vision Futuriste": {
"prompt_prefix": """advanced technological concept, future engineering visualization,
innovative architectural design, sci-fi technical precision, ultra modern aesthetic,
professional technical illustration""",
"negative_prompt": "historical, vintage, traditional, hand-drawn",
"quality_boost": 1.4,
"style_params": {
"future_tech": True,
"concept_clarity": "high"
}
}
}
# 2. OPTIMISATION DE LA QUALITÉ
class ImageEnhancer:
def __init__(self):
self.enhancement_pipeline = {
"technical_detail": self._enhance_technical_detail,
"color_refinement": self._refine_colors,
"sharpness_boost": self._boost_sharpness,
"contrast_optimization": self._optimize_contrast
}
def _enhance_technical_detail(self, image: np.ndarray) -> np.ndarray:
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
return cv2.filter2D(image, -1, kernel)
def _refine_colors(self, image: np.ndarray) -> np.ndarray:
lab = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
l = clahe.apply(l)
lab = cv2.merge((l,a,b))
return cv2.cvtColor(lab, cv2.COLOR_Lab2BGR)
def _boost_sharpness(self, image: np.ndarray) -> np.ndarray:
blurred = cv2.GaussianBlur(image, (0, 0), 3)
return cv2.addWeighted(image, 1.5, blurred, -0.5, 0)
def _optimize_contrast(self, image: np.ndarray) -> np.ndarray:
lab = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
l = clahe.apply(l)
lab = cv2.merge((l,a,b))
return cv2.cvtColor(lab, cv2.COLOR_Lab2BGR)
def enhance_image(self, image: Image.Image, style_params: Dict) -> Image.Image:
cv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
for enhancement, should_apply in style_params.items():
if should_apply and enhancement in self.enhancement_pipeline:
cv_image = self.enhancement_pipeline[enhancement](cv_image)
return Image.fromarray(cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB))
# 3. GÉNÉRATEUR D'IMAGES PRINCIPAL
class ImageGenerator:
def __init__(self):
self.API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
self.enhancer = ImageEnhancer()
token = os.getenv('HUGGINGFACE_TOKEN')
if not token:
logger.error("HUGGINGFACE_TOKEN non trouvé!")
self.headers = {"Authorization": f"Bearer {token}"}
logger.info("ImageGenerator initialisé")
def generate(self, params: Dict[str, Any]) -> Tuple[Optional[Image.Image], str]:
try:
style_info = ART_STYLES.get(params["style"])
if not style_info:
return None, "⚠️ Style non trouvé"
# Construction du prompt optimisé
prompt = f"{params['subject']}, {style_info['prompt_prefix']}"
if params.get('title'):
prompt += f", with text: {params['title']}"
payload = {
"inputs": prompt,
"parameters": {
"negative_prompt": style_info["negative_prompt"],
"num_inference_steps": int(50 * style_info["quality_boost"]),
"guidance_scale": min(9.0 * style_info["quality_boost"], 12.0),
"width": 1024 if params.get("quality", 35) > 40 else 768,
"height": 1024 if params["orientation"] == "Portrait" else 768
}
}
response = requests.post(
self.API_URL,
headers=self.headers,
json=payload,
timeout=45
)
if response.status_code == 200:
image = Image.open(io.BytesIO(response.content))
enhanced_image = self.enhancer.enhance_image(
image,
style_info["style_params"]
)
return enhanced_image, "✨ Création réussie!"
else:
error_msg = f"⚠️ Erreur API {response.status_code}: {response.text}"
logger.error(error_msg)
return None, error_msg
except Exception as e:
error_msg = f"⚠️ Erreur: {str(e)}"
logger.exception("Erreur génération:")
return None, error_msg
finally:
gc.collect()
# 4. INTERFACE UTILISATEUR
def create_interface():
generator = ImageGenerator()
# Style CSS personnalisé
css = """
.container { max-width: 1200px; margin: auto; }
.welcome {
text-align: center;
margin: 20px 0;
padding: 20px;
background: linear-gradient(135deg, #1e293b, #334155);
border-radius: 10px;
color: white;
}
.controls-group {
background: #2d3748;
padding: 15px;
border-radius: 5px;
margin: 10px 0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
"""
with gr.Blocks(css=css) as app:
gr.HTML("""
<div class="welcome">
<h1>🎨 Equity Artisan 3.0</h1>
<p>Assistant de Création d'Images Professionnelles</p>
</div>
""")
with gr.Column(elem_classes="container"):
# Format et Style
with gr.Group(elem_classes="controls-group"):
with gr.Row():
format_size = gr.Dropdown(
choices=["A4", "A3", "A2", "A1"],
value="A4",
label="Format"
)
orientation = gr.Radio(
choices=["Portrait", "Paysage"],
value="Portrait",
label="Orientation"
)
style = gr.Dropdown(
choices=list(ART_STYLES.keys()),
value="Renaissance Technologique",
label="Style Artistique"
)
# Description
with gr.Group(elem_classes="controls-group"):
subject = gr.Textbox(
label="Description",
placeholder="Décrivez votre vision technique ou artistique...",
lines=3
)
title = gr.Textbox(
label="Titre (optionnel)",
placeholder="Titre à inclure dans l'image..."
)
# Paramètres avancés
with gr.Group(elem_classes="controls-group"):
with gr.Row():
quality = gr.Slider(
minimum=30,
maximum=50,
value=40,
label="Qualité"
)
detail_level = gr.Slider(
minimum=1,
maximum=10,
value=8,
step=1,
label="Niveau de Détail"
)
# Boutons
with gr.Row():
generate_btn = gr.Button("✨ Générer", variant="primary")
clear_btn = gr.Button("🗑️ Effacer")
# Résultat
image_output = gr.Image(label="Résultat")
status = gr.Textbox(label="Status", interactive=False)
def generate(*args):
params = {
"format_size": args[0],
"orientation": args[1],
"style": args[2],
"subject": args[3],
"title": args[4],
"quality": args[5],
"detail_level": args[6]
}
return generator.generate(params)
generate_btn.click(
generate,
inputs=[
format_size,
orientation,
style,
subject,
title,
quality,
detail_level
],
outputs=[image_output, status]
)
clear_btn.click(
lambda: (None, "🗑️ Image effacée"),
outputs=[image_output, status]
)
return app
if __name__ == "__main__":
app = create_interface()
app.launch() |