ivanoctaviogaitansantos commited on
Commit
1418785
·
verified ·
1 Parent(s): 4d1ceb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -214
app.py CHANGED
@@ -1,212 +1,142 @@
1
  import gradio as gr
2
  import time
3
  import random
4
- from diffusers import StableDiffusionPipeline
5
- import torch
6
  from PIL import Image, ImageFilter, ImageStat
7
  import numpy as np
8
 
9
- # ========== AGENTES DEFINIDOS DIRECTAMENTE EN EL ARCHIVO ==========
10
 
11
  class LightingAgent:
12
  def __init__(self):
13
- print("✅ LightingAgent inicializado")
14
  self.lighting_styles = {
15
- "cinematic": "dramatic lighting, cinematic style, film noir, high contrast, volumetric light",
16
- "natural": "natural sunlight, soft light, golden hour, ambient light, realistic lighting",
17
- "studio": "studio lighting, professional photo, softbox, beauty dish, even lighting",
18
- "moody": "moody lighting, low key, chiaroscuro, atmospheric, dramatic shadows"
19
  }
20
 
21
  def optimize_lighting(self, base_prompt, lighting_style):
22
- lighting_terms = self.lighting_styles.get(lighting_style, self.lighting_styles["natural"])
23
- optimized_prompt = f"{base_prompt}, {lighting_terms}"
24
- return optimized_prompt, lighting_style
25
 
26
  class TextureAgent:
27
  def __init__(self):
28
- print(" TextureAgent inicializado")
29
- self.texture_library = {
30
- "skin": [
31
- "detailed skin texture", "natural skin pores", "skin microdetails",
32
- "subsurface scattering", "realistic skin", "skin imperfections"
33
- ],
34
- "hair": [
35
- "individual hair strands", "hair texture", "realistic hair flow",
36
- "hair details", "natural hair"
37
- ]
38
- }
39
 
40
  def enhance_textures(self, prompt):
41
- enhanced_prompt = prompt
42
- skin_terms = random.sample(self.texture_library["skin"], 2)
43
- enhanced_prompt += f", {', '.join(skin_terms)}"
44
-
45
- if "8k" not in enhanced_prompt.lower():
46
- enhanced_prompt += ", detailed texture, high quality details"
47
-
48
- return enhanced_prompt
49
 
50
  class CompositionAgent:
51
  def __init__(self):
52
- print("✅ CompositionAgent inicializado")
53
  self.composition_rules = {
54
- "portrait": [
55
- "head and shoulders composition", "rule of thirds", "vertical portrait",
56
- "close-up shot", "professional portrait composition"
57
- ],
58
- "full_body": [
59
- "full body portrait", "vertical composition", "environmental portrait",
60
- "full figure", "standing pose vertical"
61
- ]
62
  }
63
 
64
  def optimize_composition(self, prompt, composition_type="portrait"):
65
- composition_terms = self.composition_rules.get(composition_type, self.composition_rules["portrait"])
66
- selected_terms = composition_terms[:2]
67
- optimized_prompt = f"{prompt}, {', '.join(selected_terms)}, vertical format 9:16"
68
- return optimized_prompt, composition_type
69
 
70
  class StyleTransferAgent:
71
  def __init__(self):
72
- print("✅ StyleTransferAgent inicializado")
73
- self.hyperreal_styles = {
74
- "photorealistic": {
75
- "terms": "photorealistic, 8k resolution, professional photography, highly detailed",
76
- "description": "Máximo realismo fotográfico"
77
- },
78
- "cinematic": {
79
- "terms": "cinematic, movie still, film photography, dramatic, cinematic style",
80
- "description": "Estilo de película o cine"
81
- }
82
  }
83
 
84
  def apply_style(self, prompt, style_name="photorealistic"):
85
- style_data = self.hyperreal_styles.get(style_name, self.hyperreal_styles["photorealistic"])
86
- styled_prompt = f"{prompt}, {style_data['terms']}"
87
- return styled_prompt, style_name, style_data["description"]
88
 
89
  class QualityControlAgent:
90
- def __init__(self):
91
- print("✅ QualityControlAgent inicializado")
92
-
93
  def analyze_image_quality(self, image):
94
- if not isinstance(image, Image.Image):
95
- return {"score": 0.5, "feedback": ["No se pudo analizar la imagen"]}
96
-
97
  try:
98
- metrics = {
99
- "contrast": self._analyze_contrast(image),
100
- "sharpness": self._analyze_sharpness(image),
101
- "brightness": self._analyze_brightness(image)
102
- }
 
 
 
 
 
 
103
 
104
- overall_score = np.mean(list(metrics.values()))
105
 
106
  return {
107
- "score": round(overall_score, 2),
108
- "metrics": metrics,
109
- "feedback": self._generate_feedback(metrics)
110
  }
111
- except Exception as e:
112
  return {"score": 0.5, "feedback": ["Error en análisis"]}
113
-
114
- def _analyze_contrast(self, image):
115
- stat = ImageStat.Stat(image)
116
- if len(stat.stddev) == 3:
117
- return np.mean(stat.stddev) / 255.0
118
- return 0.5
119
-
120
- def _analyze_sharpness(self, image):
121
- small_img = image.resize((100, 100))
122
- edges = small_img.filter(ImageFilter.FIND_EDGES)
123
- stat = ImageStat.Stat(edges)
124
- return min(np.mean(stat.stddev) / 255.0, 1.0)
125
-
126
- def _analyze_brightness(self, image):
127
- stat = ImageStat.Stat(image)
128
- if len(stat.mean) == 3:
129
- brightness = np.mean(stat.mean) / 255.0
130
- return 1.0 - abs(brightness - 0.5) * 2
131
- return 0.5
132
-
133
- def _generate_feedback(self, metrics):
134
- feedback = []
135
- if metrics.get("contrast", 0) < 0.3:
136
- feedback.append("Puedes mejorar el contraste")
137
- if metrics.get("sharpness", 0) < 0.4:
138
- feedback.append("La imagen está algo borrosa")
139
- return feedback if feedback else ["¡Buena calidad de imagen!"]
140
 
141
  class EmotionAgent:
142
  def __init__(self):
143
- self.emotion_profiles = {
144
- "subtle_smile": "hint of smile, gentle eye crinkles, relaxed forehead, natural expression",
145
- "contemplative": "distant gaze, soft frown, pensive expression, thoughtful look",
146
- "joyful_laugh": "crow's feet, teeth slightly visible, raised cheeks, genuine smile",
147
- "serious": "neutral expression, focused gaze, composed demeanor, professional"
148
  }
149
- print("✅ EmotionAgent inicializado")
150
 
151
  def add_emotion_cues(self, base_prompt, emotion):
152
- emotion_cues = self.emotion_profiles.get(emotion, "natural expression")
153
  return f"{base_prompt}, {emotion_cues}"
154
 
155
  def get_available_emotions(self):
156
- return list(self.emotion_profiles.keys())
 
 
157
 
158
  class ImageGenerator:
159
  def __init__(self):
160
- print("🔄 Cargando generador de imágenes...")
161
- self.device = "cpu"
162
- self.pipeline = None
163
- self.model_id = "runwayml/stable-diffusion-v1-5"
164
- self.load_model()
165
-
166
- def load_model(self):
167
- try:
168
- print("📥 Descargando modelo Stable Diffusion...")
169
-
170
- self.pipeline = StableDiffusionPipeline.from_pretrained(
171
- self.model_id,
172
- torch_dtype=torch.float32,
173
- use_safetensors=True,
174
- safety_checker=None,
175
- requires_safety_checker=False,
176
- )
177
-
178
- self.pipeline = self.pipeline.to(self.device)
179
- self.pipeline.enable_attention_slicing()
180
-
181
- print("✅ Generador de imágenes cargado")
182
- except Exception as e:
183
- print(f"❌ Error cargando el generador: {e}")
184
- raise e
185
 
186
  def generate_image(self, prompt, negative_prompt="", width=512, height=768):
187
- print(f"🎨 Generando: {prompt[:80]}...")
188
- start_time = time.time()
189
 
190
  try:
191
- with torch.no_grad():
192
- result = self.pipeline(
193
- prompt=prompt,
194
- negative_prompt=negative_prompt,
195
- width=width,
196
- height=height,
197
- num_inference_steps=20,
198
- guidance_scale=7.5,
199
- generator=torch.manual_seed(42)
 
 
200
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
- image = result.images[0]
203
- generation_time = time.time() - start_time
204
- print(f"✅ Imagen generada en {generation_time:.1f}s")
205
- return image
206
 
207
  except Exception as e:
208
- print(f"❌ Error generando imagen: {e}")
209
- return Image.new('RGB', (width, height), color='gray')
 
 
 
 
 
 
210
 
211
  # ========== APLICACIÓN PRINCIPAL ==========
212
 
@@ -214,7 +144,6 @@ class HyperRealStudio916:
214
  def __init__(self):
215
  print("🚀 Iniciando HyperReal Studio 916...")
216
 
217
- # Inicializar todos los agentes
218
  self.lighting_agent = LightingAgent()
219
  self.texture_agent = TextureAgent()
220
  self.composition_agent = CompositionAgent()
@@ -226,121 +155,84 @@ class HyperRealStudio916:
226
  print("✅ ¡Aplicación lista!")
227
 
228
  def generate_portrait(self, description, lighting_style, emotion, composition_type, enhance_texture, style_preset):
229
- print(f"🎨 Iniciando generación...")
230
- start_time = time.time()
231
 
232
- # Negative prompt
233
- negative_prompt = "cartoon, 3d, render, anime, fake, plastic, doll, deformed, blurry"
234
 
235
  # Aplicar agentes
236
  current_prompt = description
237
-
238
- # Agente de emociones
239
  current_prompt = self.emotion_agent.add_emotion_cues(current_prompt, emotion)
240
-
241
- # Agente de iluminación
242
  current_prompt, _ = self.lighting_agent.optimize_lighting(current_prompt, lighting_style)
243
-
244
- # Agente de composición
245
  current_prompt, _ = self.composition_agent.optimize_composition(current_prompt, composition_type)
246
-
247
- # Agente de estilo
248
  current_prompt, _, _ = self.style_agent.apply_style(current_prompt, style_preset)
249
 
250
- # Agente de texturas
251
  if enhance_texture:
252
  current_prompt = self.texture_agent.enhance_textures(current_prompt)
253
 
254
  # Generar imagen
255
- image = self.image_generator.generate_image(
256
- prompt=current_prompt,
257
- negative_prompt=negative_prompt,
258
- width=512,
259
- height=768
260
- )
261
 
262
  # Analizar calidad
263
  quality_analysis = self.quality_agent.analyze_image_quality(image)
264
 
265
- total_time = time.time() - start_time
266
- print(f"✅ Proceso completado en {total_time:.1f}s")
267
 
268
  return image, quality_analysis['score'], current_prompt, quality_analysis
269
 
270
  def create_interface():
271
  studio = HyperRealStudio916()
272
 
273
- with gr.Blocks(theme=gr.themes.Soft(), title="HyperReal Studio 916") as demo:
274
- gr.Markdown("""
275
- # 🎨 HyperReal Studio 916
276
- ### Generador de Retratos Hiperrealistas
277
- """)
278
 
279
  with gr.Row():
280
- with gr.Column(scale=1):
281
  description = gr.Textbox(
282
- label="📝 Descripción",
283
- placeholder="Ej: mujer joven con cabello castaño, sonriendo...",
284
- lines=3
285
  )
286
 
287
  lighting_style = gr.Dropdown(
288
- choices=["cinematic", "natural", "studio", "moody"],
289
- label="💡 Iluminación",
290
  value="natural"
291
  )
292
 
293
  emotion = gr.Dropdown(
294
  choices=studio.emotion_agent.get_available_emotions(),
295
- label="😊 Emoción",
296
  value="subtle_smile"
297
  )
298
 
299
  composition_type = gr.Dropdown(
300
  choices=["portrait", "full_body"],
301
- label="📐 Composición",
302
  value="portrait"
303
  )
304
 
305
  style_preset = gr.Dropdown(
306
  choices=["photorealistic", "cinematic"],
307
- label="🎨 Estilo",
308
  value="photorealistic"
309
  )
310
 
311
- enhance_texture = gr.Checkbox(
312
- label="🔍 Mejorar Texturas",
313
- value=True
314
- )
315
-
316
- generate_btn = gr.Button("🎨 Generar Retrato", variant="primary")
317
 
318
- with gr.Column(scale=2):
319
- output_image = gr.Image(
320
- label="🖼️ Retrato Generado",
321
- format="png",
322
- height=400
323
- )
324
-
325
- realism_score = gr.Number(
326
- label="📊 Calidad",
327
- precision=2
328
- )
329
-
330
- used_prompt = gr.Textbox(
331
- label="📝 Prompt Utilizado",
332
- lines=2
333
- )
334
 
335
- with gr.Accordion("📈 Análisis", open=False):
336
- quality_analysis = gr.JSON(
337
- label="Métricas"
338
- )
339
 
340
  gr.Examples(
341
  examples=[
342
- ["mujer joven con cabello largo, sonriendo", "natural", "subtle_smile", "portrait", "photorealistic"],
343
- ["hombre maduro con barba, serio", "cinematic", "serious", "portrait", "cinematic"]
344
  ],
345
  inputs=[description, lighting_style, emotion, composition_type, style_preset],
346
  )
@@ -355,4 +247,4 @@ def create_interface():
355
 
356
  if __name__ == "__main__":
357
  demo = create_interface()
358
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
  import time
3
  import random
 
 
4
  from PIL import Image, ImageFilter, ImageStat
5
  import numpy as np
6
 
7
+ # ========== AGENTES SIMPLIFICADOS ==========
8
 
9
  class LightingAgent:
10
  def __init__(self):
 
11
  self.lighting_styles = {
12
+ "cinematic": "dramatic lighting, cinematic style, high contrast",
13
+ "natural": "natural sunlight, soft light, golden hour",
14
+ "studio": "studio lighting, professional photo, softbox"
 
15
  }
16
 
17
  def optimize_lighting(self, base_prompt, lighting_style):
18
+ lighting_terms = self.lighting_styles.get(lighting_style, "professional lighting")
19
+ return f"{base_prompt}, {lighting_terms}", lighting_style
 
20
 
21
  class TextureAgent:
22
  def __init__(self):
23
+ self.texture_terms = ["detailed skin texture", "natural skin pores", "realistic skin"]
 
 
 
 
 
 
 
 
 
 
24
 
25
  def enhance_textures(self, prompt):
26
+ texture = random.choice(self.texture_terms)
27
+ return f"{prompt}, {texture}, high quality details"
 
 
 
 
 
 
28
 
29
  class CompositionAgent:
30
  def __init__(self):
 
31
  self.composition_rules = {
32
+ "portrait": "vertical portrait, head and shoulders composition",
33
+ "full_body": "full body portrait, vertical composition"
 
 
 
 
 
 
34
  }
35
 
36
  def optimize_composition(self, prompt, composition_type="portrait"):
37
+ composition = self.composition_rules.get(composition_type, "vertical portrait")
38
+ return f"{prompt}, {composition}, 9:16 format", composition_type
 
 
39
 
40
  class StyleTransferAgent:
41
  def __init__(self):
42
+ self.styles = {
43
+ "photorealistic": "photorealistic, professional photography",
44
+ "cinematic": "cinematic, movie style, dramatic"
 
 
 
 
 
 
 
45
  }
46
 
47
  def apply_style(self, prompt, style_name="photorealistic"):
48
+ style = self.styles.get(style_name, "photorealistic")
49
+ return f"{prompt}, {style}", style_name, f"Estilo {style_name}"
 
50
 
51
  class QualityControlAgent:
 
 
 
52
  def analyze_image_quality(self, image):
 
 
 
53
  try:
54
+ if not isinstance(image, Image.Image):
55
+ return {"score": 0.5, "feedback": ["No se pudo analizar"]}
56
+
57
+ # Métrica simple de contraste
58
+ stat = ImageStat.Stat(image)
59
+ if len(stat.stddev) == 3:
60
+ contrast = np.mean(stat.stddev) / 255.0
61
+ else:
62
+ contrast = 0.5
63
+
64
+ score = min(contrast * 2, 1.0) # Escalar a 0-1
65
 
66
+ feedback = ["Buena calidad"] if score > 0.6 else ["Calidad mejorable"]
67
 
68
  return {
69
+ "score": round(score, 2),
70
+ "metrics": {"contrast": contrast},
71
+ "feedback": feedback
72
  }
73
+ except:
74
  return {"score": 0.5, "feedback": ["Error en análisis"]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  class EmotionAgent:
77
  def __init__(self):
78
+ self.emotions = {
79
+ "subtle_smile": "hint of smile, natural expression",
80
+ "serious": "neutral expression, focused gaze",
81
+ "joyful": "genuine smile, happy expression"
 
82
  }
 
83
 
84
  def add_emotion_cues(self, base_prompt, emotion):
85
+ emotion_cues = self.emotions.get(emotion, "natural expression")
86
  return f"{base_prompt}, {emotion_cues}"
87
 
88
  def get_available_emotions(self):
89
+ return list(self.emotions.keys())
90
+
91
+ # ========== GENERADOR DE IMÁGENES SIMPLIFICADO ==========
92
 
93
  class ImageGenerator:
94
  def __init__(self):
95
+ self.model_loaded = False
96
+ print("🔄 Inicializando generador...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  def generate_image(self, prompt, negative_prompt="", width=512, height=768):
99
+ print(f"🎨 Generando: {prompt[:50]}...")
 
100
 
101
  try:
102
+ # Intentar cargar diffusers solo cuando sea necesario
103
+ from diffusers import StableDiffusionPipeline
104
+ import torch
105
+
106
+ if not self.model_loaded:
107
+ print("📥 Cargando modelo...")
108
+ self.pipeline = StableDiffusionPipeline.from_pretrained(
109
+ "runwayml/stable-diffusion-v1-5",
110
+ torch_dtype=torch.float32,
111
+ safety_checker=None,
112
+ requires_safety_checker=False,
113
  )
114
+ self.pipeline = self.pipeline.to("cpu")
115
+ self.pipeline.enable_attention_slicing()
116
+ self.model_loaded = True
117
+
118
+ # Generar imagen
119
+ result = self.pipeline(
120
+ prompt=prompt,
121
+ negative_prompt=negative_prompt,
122
+ width=width,
123
+ height=height,
124
+ num_inference_steps=15, # Menos pasos para más velocidad
125
+ guidance_scale=7.0,
126
+ generator=torch.manual_seed(42)
127
+ )
128
 
129
+ return result.images[0]
 
 
 
130
 
131
  except Exception as e:
132
+ print(f"❌ Error: {e}")
133
+ # Crear imagen de placeholder
134
+ return self.create_placeholder_image(width, height)
135
+
136
+ def create_placeholder_image(self, width, height):
137
+ # Crear una imagen simple de placeholder
138
+ img = Image.new('RGB', (width, height), color=(73, 109, 137))
139
+ return img
140
 
141
  # ========== APLICACIÓN PRINCIPAL ==========
142
 
 
144
  def __init__(self):
145
  print("🚀 Iniciando HyperReal Studio 916...")
146
 
 
147
  self.lighting_agent = LightingAgent()
148
  self.texture_agent = TextureAgent()
149
  self.composition_agent = CompositionAgent()
 
155
  print("✅ ¡Aplicación lista!")
156
 
157
  def generate_portrait(self, description, lighting_style, emotion, composition_type, enhance_texture, style_preset):
158
+ print("🎨 Iniciando generación...")
 
159
 
160
+ negative_prompt = "cartoon, 3d, render, anime, fake, deformed, blurry"
 
161
 
162
  # Aplicar agentes
163
  current_prompt = description
 
 
164
  current_prompt = self.emotion_agent.add_emotion_cues(current_prompt, emotion)
 
 
165
  current_prompt, _ = self.lighting_agent.optimize_lighting(current_prompt, lighting_style)
 
 
166
  current_prompt, _ = self.composition_agent.optimize_composition(current_prompt, composition_type)
 
 
167
  current_prompt, _, _ = self.style_agent.apply_style(current_prompt, style_preset)
168
 
 
169
  if enhance_texture:
170
  current_prompt = self.texture_agent.enhance_textures(current_prompt)
171
 
172
  # Generar imagen
173
+ image = self.image_generator.generate_image(current_prompt, negative_prompt)
 
 
 
 
 
174
 
175
  # Analizar calidad
176
  quality_analysis = self.quality_agent.analyze_image_quality(image)
177
 
178
+ print("✅ Proceso completado")
 
179
 
180
  return image, quality_analysis['score'], current_prompt, quality_analysis
181
 
182
  def create_interface():
183
  studio = HyperRealStudio916()
184
 
185
+ with gr.Blocks(title="HyperReal Studio 916") as demo:
186
+ gr.Markdown("# 🎨 HyperReal Studio 916")
187
+ gr.Markdown("Generador de retratos hiperrealistas")
 
 
188
 
189
  with gr.Row():
190
+ with gr.Column():
191
  description = gr.Textbox(
192
+ label="Descripción",
193
+ placeholder="Describe la persona o escena...",
194
+ lines=2
195
  )
196
 
197
  lighting_style = gr.Dropdown(
198
+ choices=["cinematic", "natural", "studio"],
199
+ label="Iluminación",
200
  value="natural"
201
  )
202
 
203
  emotion = gr.Dropdown(
204
  choices=studio.emotion_agent.get_available_emotions(),
205
+ label="Emoción",
206
  value="subtle_smile"
207
  )
208
 
209
  composition_type = gr.Dropdown(
210
  choices=["portrait", "full_body"],
211
+ label="Composición",
212
  value="portrait"
213
  )
214
 
215
  style_preset = gr.Dropdown(
216
  choices=["photorealistic", "cinematic"],
217
+ label="Estilo",
218
  value="photorealistic"
219
  )
220
 
221
+ enhance_texture = gr.Checkbox(label="Mejorar texturas", value=True)
222
+ generate_btn = gr.Button("Generar Retrato", variant="primary")
 
 
 
 
223
 
224
+ with gr.Column():
225
+ output_image = gr.Image(label="Retrato Generado", height=400)
226
+ realism_score = gr.Number(label="Calidad", precision=2)
227
+ used_prompt = gr.Textbox(label="Prompt Utilizado", lines=2)
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
+ with gr.Accordion("Análisis", open=False):
230
+ quality_analysis = gr.JSON(label="Métricas")
 
 
231
 
232
  gr.Examples(
233
  examples=[
234
+ ["mujer joven sonriendo", "natural", "subtle_smile", "portrait", "photorealistic"],
235
+ ["hombre serio con barba", "cinematic", "serious", "portrait", "cinematic"]
236
  ],
237
  inputs=[description, lighting_style, emotion, composition_type, style_preset],
238
  )
 
247
 
248
  if __name__ == "__main__":
249
  demo = create_interface()
250
+ demo.launch()