Spaces:
No application file
No application file
psurmreqmer
commited on
Commit
·
43555c1
1
Parent(s):
551e5e1
app6.py
CHANGED
|
@@ -1,116 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from diffusers import DiffusionPipeline
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
device = "cpu"
|
| 10 |
-
dtype_config = torch.float32 # Usamos float32 para la CPU
|
| 11 |
-
|
| 12 |
-
# Modelo Qwen para edición de imagen
|
| 13 |
-
model_id = "Qwen/Qwen-Image-Edit-2509"
|
| 14 |
|
| 15 |
-
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
pipe =
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
print("✅ Modelo Qwen-Image-Edit cargado con éxito en la CPU.")
|
| 25 |
-
|
| 26 |
-
except Exception as e:
|
| 27 |
-
print(f"❌ Error CRÍTICO al cargar el modelo Qwen: {e}")
|
| 28 |
-
print("El modelo NO ha podido cargarse. Podría ser un problema de memoria RAM, incluso con CPU.")
|
| 29 |
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
def
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
"""
|
| 36 |
-
# Manejo de error de carga
|
| 37 |
-
if pipe is None:
|
| 38 |
-
return Image.new('RGB', (512, 512), color = 'red')
|
| 39 |
-
|
| 40 |
-
if imagen_entrada is None:
|
| 41 |
-
return None
|
| 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 |
-
).images[0]
|
| 71 |
-
|
| 72 |
-
return image
|
| 73 |
-
except Exception as e:
|
| 74 |
-
print(f"Error durante la ejecución del pipeline: {e}")
|
| 75 |
-
# Devuelve un cuadro de error si el proceso falla
|
| 76 |
-
return Image.new('RGB', (512, 512), color = 'red')
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# --- Interfaz Gradio con gr.Blocks() ---
|
| 80 |
-
with gr.Blocks(title="Qwen Image Edit Estilos Fijos") as demo:
|
| 81 |
-
gr.Markdown(
|
| 82 |
-
"""
|
| 83 |
-
# 🖼️ Tarea de Difusión (Image-to-Image) con Qwen Edit
|
| 84 |
-
Carga una imagen y selecciona un **Estilo Radial** para que el modelo Qwen la transforme.
|
| 85 |
-
**Nota:** La generación puede ser lenta al ejecutarse en CPU.
|
| 86 |
-
"""
|
| 87 |
-
)
|
| 88 |
-
with gr.Row():
|
| 89 |
-
with gr.Column(scale=1):
|
| 90 |
-
image_input = gr.Image(
|
| 91 |
-
type="pil",
|
| 92 |
-
label="1. Cargar Imagen Inicial",
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
estilo_radial = gr.Radio(
|
| 96 |
-
["Original (Poco Ruido)", "Blanco y Negro (Monocromático)", "Alto Contraste y Saturación"],
|
| 97 |
-
label="2. Selecciona el Estilo de Transformación",
|
| 98 |
-
value="Blanco y Negro (Monocromático)"
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
process_button = gr.Button("✨ Aplicar Difusión Qwen", variant="primary")
|
| 102 |
-
|
| 103 |
-
with gr.Column(scale=1):
|
| 104 |
-
image_output = gr.Image(
|
| 105 |
-
type="pil",
|
| 106 |
-
label="Imagen Transformada por Difusión",
|
| 107 |
-
height=512
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
process_button.click(
|
| 111 |
-
fn=procesar_con_difusion,
|
| 112 |
-
inputs=[image_input, estilo_radial],
|
| 113 |
-
outputs=image_output
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
demo.launch(inbrowser=True)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# 1. Cargar el modelo ligero (Solo se ejecuta una vez al iniciar el Space)
|
| 7 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Usamos StableDiffusionImg2ImgPipeline para Image-to-Image
|
| 11 |
try:
|
| 12 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 13 |
+
pipe = pipe.to(device)
|
| 14 |
+
except:
|
| 15 |
+
# Fallback si no hay GPU o si falla la carga con float16
|
| 16 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id)
|
| 17 |
+
pipe = pipe.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
+
# 2. Función de procesamiento (Aquí se ejecuta la inferencia)
|
| 21 |
+
def convert_to_bn_diffusion(input_image: Image.Image, prompt: str, strength: float) -> Image.Image:
|
| 22 |
"""
|
| 23 |
+
Ejecuta el pipeline de difusión I2I para estilizar la imagen.
|
| 24 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Aseguramos que la imagen esté en RGB y redimensionamos si es necesario
|
| 27 |
+
input_image = input_image.convert("RGB").resize((512, 512))
|
| 28 |
|
| 29 |
+
# Prompt forzando el estilo monocromático
|
| 30 |
+
bn_prompt = f"{prompt}, high contrast, black and white, monochrome, grayscale"
|
| 31 |
+
|
| 32 |
+
# Generación (Inferencia)
|
| 33 |
+
output_image = pipe(
|
| 34 |
+
prompt=bn_prompt,
|
| 35 |
+
image=input_image,
|
| 36 |
+
strength=strength, # Cuanto más alto, más se transforma (más BN)
|
| 37 |
+
guidance_scale=7.5
|
| 38 |
+
).images[0]
|
| 39 |
|
| 40 |
+
return output_image
|
| 41 |
+
|
| 42 |
+
# 3. Interfaz de Gradio
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=convert_to_bn_diffusion,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Image(type="pil", label="Sube tu imagen (se redimensionará a 512x512)"),
|
| 47 |
+
gr.Textbox(label="Prompt adicional (ej: 'a moody photo', 'vintage style')", value="A sharp, detailed photograph"),
|
| 48 |
+
gr.Slider(minimum=0.5, maximum=1.0, step=0.05, value=0.9, label="Fuerza de Estilización (Strength - Cuánto se convierte a B/N)")
|
| 49 |
+
],
|
| 50 |
+
outputs="image",
|
| 51 |
+
title="Conversor a B/N con Modelo de Difusión (Img2Img)",
|
| 52 |
+
description="Sube una imagen y el modelo Stable Diffusion intentará convertirla a blanco y negro basado en el prompt y la fuerza de estilización."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|