Spaces:
No application file
No application file
psurmreqmer
commited on
Commit
·
80645ad
1
Parent(s):
43555c1
app6.py
CHANGED
|
@@ -3,53 +3,32 @@ 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 |
-
|
| 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 |
-
|
| 21 |
-
|
| 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=
|
| 35 |
image=input_image,
|
| 36 |
-
strength=strength,
|
| 37 |
guidance_scale=7.5
|
| 38 |
).images[0]
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# 3. Interfaz de Gradio
|
| 43 |
iface = gr.Interface(
|
| 44 |
-
fn=
|
| 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__":
|
|
|
|
| 3 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 6 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def convert_simple(input_image: Image.Image) -> Image.Image:
|
| 12 |
+
prompt = "A high contrast, dramatic photo, black and white, monochrome, grayscale"
|
| 13 |
+
strength = 0.95
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 15 |
input_image = input_image.convert("RGB").resize((512, 512))
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
output_image = pipe(
|
| 18 |
+
prompt=prompt,
|
| 19 |
image=input_image,
|
| 20 |
+
strength=strength,
|
| 21 |
guidance_scale=7.5
|
| 22 |
).images[0]
|
| 23 |
|
| 24 |
+
final_bn_image = output_image.convert('L')
|
| 25 |
+
|
| 26 |
+
return final_bn_image
|
| 27 |
|
|
|
|
| 28 |
iface = gr.Interface(
|
| 29 |
+
fn=convert_simple,
|
| 30 |
+
inputs=[gr.Image(type="pil")],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
outputs="image",
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|