Arreglando generacion de imagenes - version simplificada
Browse files
app.py
CHANGED
@@ -29,17 +29,15 @@ MODELS = {
|
|
29 |
"Helsinki-NLP/opus-mt-en-es": "Traductor ingl茅s-espa帽ol"
|
30 |
},
|
31 |
"image": {
|
|
|
32 |
"runwayml/stable-diffusion-v1-5": "Stable Diffusion v1.5",
|
33 |
-
"CompVis/stable-diffusion-v1-4": "Stable Diffusion v1.4",
|
34 |
"stabilityai/stable-diffusion-2-1": "Stable Diffusion 2.1",
|
35 |
"stabilityai/stable-diffusion-xl-base-1.0": "SDXL Base",
|
36 |
"stabilityai/stable-diffusion-xl-refiner-1.0": "SDXL Refiner",
|
37 |
"prompthero/openjourney": "Midjourney style",
|
38 |
"dreamlike-art/dreamlike-photoreal-2.0": "Fotorealista",
|
39 |
"nitrosocke/Ghibli-Diffusion": "Estilo Studio Ghibli",
|
40 |
-
"nitrosocke/mo-di-diffusion": "Estilo moderno"
|
41 |
-
"CompVis/stable-diffusion-v1-4": "Stable Diffusion v1.4",
|
42 |
-
"runwayml/stable-diffusion-v1-5": "Stable Diffusion v1.5"
|
43 |
},
|
44 |
"chat": {
|
45 |
"microsoft/DialoGPT-medium": "Chat conversacional",
|
@@ -83,21 +81,19 @@ def load_text_model(model_name):
|
|
83 |
return model_cache[model_name]
|
84 |
|
85 |
def load_image_model(model_name):
|
86 |
-
"""Cargar modelo de imagen -
|
87 |
if model_name not in model_cache:
|
88 |
print(f"Cargando modelo de imagen: {model_name}")
|
89 |
|
90 |
-
#
|
91 |
pipe = StableDiffusionPipeline.from_pretrained(
|
92 |
model_name,
|
93 |
-
torch_dtype=torch.float32,
|
94 |
-
safety_checker=None
|
95 |
-
requires_safety_checker=False
|
96 |
)
|
97 |
|
98 |
-
#
|
99 |
-
pipe.enable_attention_slicing()
|
100 |
-
pipe.enable_sequential_cpu_offload() # Optimizar para CPU
|
101 |
|
102 |
model_cache[model_name] = {
|
103 |
"pipeline": pipe,
|
@@ -148,27 +144,27 @@ def generate_text(prompt, model_name, max_length=100):
|
|
148 |
return f"Error generando texto: {str(e)}"
|
149 |
|
150 |
def generate_image(prompt, model_name, num_inference_steps=20):
|
151 |
-
"""Generar imagen con el modelo seleccionado -
|
152 |
try:
|
|
|
|
|
|
|
|
|
153 |
model_data = load_image_model(model_name)
|
154 |
pipeline = model_data["pipeline"]
|
155 |
|
156 |
-
#
|
157 |
-
if num_inference_steps > 20:
|
158 |
-
num_inference_steps = 20 # Limitar a m谩ximo 20 pasos para velocidad
|
159 |
-
|
160 |
-
# Generar imagen con configuraci贸n optimizada
|
161 |
image = pipeline(
|
162 |
prompt,
|
163 |
num_inference_steps=num_inference_steps,
|
164 |
-
guidance_scale=7.
|
165 |
-
height=512, # Tama帽o fijo para consistencia
|
166 |
-
width=512
|
167 |
).images[0]
|
168 |
|
|
|
169 |
return image
|
170 |
|
171 |
except Exception as e:
|
|
|
172 |
return f"Error generando imagen: {str(e)}"
|
173 |
|
174 |
def chat_with_model(message, history, model_name):
|
@@ -329,7 +325,7 @@ with gr.Blocks(title="Modelos Libres de IA", theme=gr.themes.Soft()) as demo:
|
|
329 |
with gr.Column():
|
330 |
image_model = gr.Dropdown(
|
331 |
choices=list(MODELS["image"].keys()),
|
332 |
-
value="
|
333 |
label="Modelo de Imagen"
|
334 |
)
|
335 |
image_prompt = gr.Textbox(
|
|
|
29 |
"Helsinki-NLP/opus-mt-en-es": "Traductor ingl茅s-espa帽ol"
|
30 |
},
|
31 |
"image": {
|
32 |
+
"CompVis/stable-diffusion-v1-4": "Stable Diffusion v1.4 (B谩sico)",
|
33 |
"runwayml/stable-diffusion-v1-5": "Stable Diffusion v1.5",
|
|
|
34 |
"stabilityai/stable-diffusion-2-1": "Stable Diffusion 2.1",
|
35 |
"stabilityai/stable-diffusion-xl-base-1.0": "SDXL Base",
|
36 |
"stabilityai/stable-diffusion-xl-refiner-1.0": "SDXL Refiner",
|
37 |
"prompthero/openjourney": "Midjourney style",
|
38 |
"dreamlike-art/dreamlike-photoreal-2.0": "Fotorealista",
|
39 |
"nitrosocke/Ghibli-Diffusion": "Estilo Studio Ghibli",
|
40 |
+
"nitrosocke/mo-di-diffusion": "Estilo moderno"
|
|
|
|
|
41 |
},
|
42 |
"chat": {
|
43 |
"microsoft/DialoGPT-medium": "Chat conversacional",
|
|
|
81 |
return model_cache[model_name]
|
82 |
|
83 |
def load_image_model(model_name):
|
84 |
+
"""Cargar modelo de imagen - versi贸n simplificada"""
|
85 |
if model_name not in model_cache:
|
86 |
print(f"Cargando modelo de imagen: {model_name}")
|
87 |
|
88 |
+
# Configuraci贸n b谩sica sin optimizaciones complejas
|
89 |
pipe = StableDiffusionPipeline.from_pretrained(
|
90 |
model_name,
|
91 |
+
torch_dtype=torch.float32,
|
92 |
+
safety_checker=None
|
|
|
93 |
)
|
94 |
|
95 |
+
# Solo optimizaci贸n b谩sica de memoria
|
96 |
+
pipe.enable_attention_slicing()
|
|
|
97 |
|
98 |
model_cache[model_name] = {
|
99 |
"pipeline": pipe,
|
|
|
144 |
return f"Error generando texto: {str(e)}"
|
145 |
|
146 |
def generate_image(prompt, model_name, num_inference_steps=20):
|
147 |
+
"""Generar imagen con el modelo seleccionado - versi贸n simplificada"""
|
148 |
try:
|
149 |
+
print(f"Generando imagen con modelo: {model_name}")
|
150 |
+
print(f"Prompt: {prompt}")
|
151 |
+
print(f"Pasos: {num_inference_steps}")
|
152 |
+
|
153 |
model_data = load_image_model(model_name)
|
154 |
pipeline = model_data["pipeline"]
|
155 |
|
156 |
+
# Configuraci贸n b谩sica
|
|
|
|
|
|
|
|
|
157 |
image = pipeline(
|
158 |
prompt,
|
159 |
num_inference_steps=num_inference_steps,
|
160 |
+
guidance_scale=7.5
|
|
|
|
|
161 |
).images[0]
|
162 |
|
163 |
+
print("Imagen generada exitosamente")
|
164 |
return image
|
165 |
|
166 |
except Exception as e:
|
167 |
+
print(f"Error generando imagen: {str(e)}")
|
168 |
return f"Error generando imagen: {str(e)}"
|
169 |
|
170 |
def chat_with_model(message, history, model_name):
|
|
|
325 |
with gr.Column():
|
326 |
image_model = gr.Dropdown(
|
327 |
choices=list(MODELS["image"].keys()),
|
328 |
+
value="CompVis/stable-diffusion-v1-4",
|
329 |
label="Modelo de Imagen"
|
330 |
)
|
331 |
image_prompt = gr.Textbox(
|