salomonsky commited on
Commit
f7786aa
1 Parent(s): d1ac4a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -21
app.py CHANGED
@@ -2,14 +2,27 @@ import streamlit as st
2
  import torch
3
  from PIL import Image
4
  import random
5
- from diffusers import StableDiffusionInstructPix2PixPipeline
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
- selected_model = "timbrooks/instruct-pix2pix"
 
 
 
 
 
 
 
 
9
 
10
  @st.cache_resource
11
  def load_model():
12
- return StableDiffusionInstructPix2PixPipeline.from_pretrained(selected_model, torch_dtype=torch.float32, safety_checker=None).to(device)
 
 
 
 
 
13
 
14
  pipe = load_model()
15
 
@@ -26,17 +39,34 @@ def infer(source_img, prompt, steps, seed, text_cfg_scale, image_cfg_scale):
26
 
27
  try:
28
  for i in range(steps):
29
- output = pipe(
30
- prompt,
31
- image=source_image,
32
- guidance_scale=text_cfg_scale,
33
- num_inference_steps=1,
34
- image_guidance_scale=image_cfg_scale,
35
- safety_checker=None # Desactivar el verificador de seguridad
36
- )
37
- if len(output.images) == 0:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  raise ValueError("No se generaron imágenes.")
39
- result = output.images[0]
40
  progress_bar.progress((i + 1) / steps)
41
 
42
  progress_bar.progress(1.0)
@@ -48,19 +78,17 @@ def infer(source_img, prompt, steps, seed, text_cfg_scale, image_cfg_scale):
48
 
49
  st.title("Flux Image to Image")
50
 
51
- # Sidebar para controles
52
  with st.sidebar:
53
  uploaded_image = st.file_uploader("Sube una imagen", type=["png", "jpg", "jpeg"], key="unique_file_uploader")
54
  prompt = st.text_input("Texto del prompt (máx. 77 tokens)")
55
- steps = st.slider("Número de Iteraciones", min_value=1, max_value=50, value=2, step=1)
56
- randomize_seed = st.radio("Randomize Seed", ["Fix Seed", "Randomize Seed"])
57
  seed = st.slider("Seed", min_value=0, max_value=9999, step=1, value=random.randint(0, 9999) if randomize_seed == "Randomize Seed" else 1111)
58
- text_cfg_scale = st.slider("Text CFG Scale", min_value=0.0, max_value=10.0, step=0.1, value=7.5)
59
- image_cfg_scale = st.slider("Image CFG Scale", min_value=0.0, max_value=10.0, step=0.1, value=1.5)
60
 
61
- # Columna para mostrar la imagen generada
62
  if uploaded_image is not None and st.button("Generar imagen"):
63
- image = Image.open(uploaded_image)
64
  result_image = infer(image, prompt, steps, seed, text_cfg_scale, image_cfg_scale)
65
  if result_image is not None:
66
- st.image(result_image, caption="Imagen generada", use_column_width=True) # Mostrar la imagen generada
 
2
  import torch
3
  from PIL import Image
4
  import random
5
+ from diffusers import StableDiffusionInstructPix2PixPipeline, DiffusionPipeline, FluxPipeline
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
+
9
+ model_options = {
10
+ "Instruct Pix2Pix": "timbrooks/instruct-pix2pix",
11
+ "SDXL Turbo": "stabilityai/sdxl-turbo",
12
+ "FLUX": "alimama-creative/FLUX.1-Turbo-Alpha"
13
+ }
14
+
15
+ selected_model = st.sidebar.selectbox("Selecciona el modelo", list(model_options.keys()))
16
+ model_id = model_options[selected_model]
17
 
18
  @st.cache_resource
19
  def load_model():
20
+ if selected_model == "Instruct Pix2Pix":
21
+ return StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float32, safety_checker=None).to(device)
22
+ elif selected_model == "SDXL Turbo":
23
+ return DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32, safety_checker=None).to(device)
24
+ else:
25
+ return FluxPipeline.from_pretrained(model_id, torch_dtype=torch.float32, safety_checker=None).to(device)
26
 
27
  pipe = load_model()
28
 
 
39
 
40
  try:
41
  for i in range(steps):
42
+ if selected_model == "Instruct Pix2Pix":
43
+ output = pipe(
44
+ prompt,
45
+ image=source_image,
46
+ guidance_scale=text_cfg_scale,
47
+ num_inference_steps=1,
48
+ image_guidance_scale=image_cfg_scale
49
+ )
50
+ elif selected_model == "SDXL Turbo":
51
+ output = pipe(
52
+ prompt,
53
+ num_inference_steps=steps,
54
+ guidance_scale=text_cfg_scale,
55
+ image=source_image
56
+ )
57
+ else:
58
+ output = pipe(
59
+ prompt,
60
+ image=source_image,
61
+ num_inference_steps=steps,
62
+ guidance_scale=text_cfg_scale
63
+ )
64
+
65
+ result = output.images[0] if output.images else None
66
+
67
+ if result is None:
68
  raise ValueError("No se generaron imágenes.")
69
+
70
  progress_bar.progress((i + 1) / steps)
71
 
72
  progress_bar.progress(1.0)
 
78
 
79
  st.title("Flux Image to Image")
80
 
 
81
  with st.sidebar:
82
  uploaded_image = st.file_uploader("Sube una imagen", type=["png", "jpg", "jpeg"], key="unique_file_uploader")
83
  prompt = st.text_input("Texto del prompt (máx. 77 tokens)")
84
+ steps = st.slider("Número de Iteraciones", min_value=1, max_value=50, value=11, step=1)
85
+ randomize_seed = st.radio("Randomize Seed", ["Randomize Seed", "Fix Seed"])
86
  seed = st.slider("Seed", min_value=0, max_value=9999, step=1, value=random.randint(0, 9999) if randomize_seed == "Randomize Seed" else 1111)
87
+ text_cfg_scale = st.slider("Text CFG Scale", min_value=0.0, max_value=10.0, step=0.1, value=9.0)
88
+ image_cfg_scale = st.slider("Image CFG Scale", min_value=0.0, max_value=10.0, step=0.1, value=1.0)
89
 
 
90
  if uploaded_image is not None and st.button("Generar imagen"):
91
+ image = Image.open(uploaded_image).convert("RGB")
92
  result_image = infer(image, prompt, steps, seed, text_cfg_scale, image_cfg_scale)
93
  if result_image is not None:
94
+ st.image(result_image, caption="Imagen generada", use_column_width=True)