Spaces:
Runtime error
Runtime error
Update app.py
#43
by
fmeiden
- opened
app.py
CHANGED
@@ -713,50 +713,99 @@ def get_model(token="", model_choice="", model_path=""):
|
|
713 |
return model["model"]
|
714 |
|
715 |
|
716 |
-
def run_outpaint(
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
-
):
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
739 |
cur_model = get_model()
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
744 |
-
|
745 |
-
|
746 |
-
|
747 |
-
|
748 |
-
|
749 |
-
enable_safety
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
759 |
base64_str_lst = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
760 |
if enable_img2img:
|
761 |
use_correction = "border_mode"
|
762 |
for image in images:
|
|
|
713 |
return model["model"]
|
714 |
|
715 |
|
716 |
+
def run_outpaint(sel_buffer_str, prompt_text, negative_prompt_text, strength, guidance, step, resize_check, fill_mode, enable_safety, use_correction, enable_img2img, use_seed, seed_val, generate_num, scheduler, scheduler_eta, state):
|
717 |
+
try:
|
718 |
+
# Decodifica los datos base64
|
719 |
+
data = base64.b64decode(sel_buffer_str)
|
720 |
+
|
721 |
+
# Intenta abrir la imagen desde los datos decodificados
|
722 |
+
pil = Image.open(io.BytesIO(data))
|
723 |
+
|
724 |
+
# Verifica si la imagen se abrió correctamente
|
725 |
+
if pil.mode in ("RGBA", "P"):
|
726 |
+
pil = pil.convert("RGB")
|
727 |
+
|
728 |
+
# Obtén las dimensiones de la imagen
|
729 |
+
width, height = pil.size
|
730 |
+
|
731 |
+
# Convierte la imagen PIL a un array numpy si es necesario
|
732 |
+
sel_buffer = np.array(pil)
|
733 |
+
|
734 |
+
except (ValueError, TypeError, base64.binascii.Error) as e:
|
735 |
+
print(f"Error al decodificar base64: {e}")
|
736 |
+
return None
|
737 |
+
except IOError as e:
|
738 |
+
print(f"Error de IO al abrir la imagen: {e}")
|
739 |
+
return None
|
740 |
+
except UnidentifiedImageError as e:
|
741 |
+
print(f"Error al identificar la imagen: {e}")
|
742 |
+
return None
|
743 |
+
except Exception as e:
|
744 |
+
print(f"Error desconocido: {e}")
|
745 |
+
return None
|
746 |
+
|
747 |
+
# Carga del modelo
|
748 |
cur_model = get_model()
|
749 |
+
if not cur_model:
|
750 |
+
print("Error: No se pudo cargar el modelo.")
|
751 |
+
return None
|
752 |
+
|
753 |
+
# Configuración de parámetros del modelo
|
754 |
+
inpaint = self.inpaint # Asumiendo que `self.inpaint` está definido correctamente
|
755 |
+
selected_scheduler = scheduler_dict.get(scheduler, scheduler_dict["PLMS"])
|
756 |
+
for item in [inpaint]:
|
757 |
+
item.scheduler = selected_scheduler
|
758 |
+
if enable_safety:
|
759 |
+
item.safety_checker = self.safety_checker
|
760 |
+
else:
|
761 |
+
item.safety_checker = lambda images, **kwargs: (images, None)
|
762 |
+
|
763 |
+
# Ejecución del modelo para procesar la imagen
|
764 |
+
try:
|
765 |
+
images = cur_model.run(
|
766 |
+
image_pil=pil,
|
767 |
+
prompt=prompt_text,
|
768 |
+
negative_prompt=negative_prompt_text,
|
769 |
+
guidance_scale=guidance,
|
770 |
+
strength=strength,
|
771 |
+
step=step,
|
772 |
+
resize_check=resize_check,
|
773 |
+
fill_mode=fill_mode,
|
774 |
+
enable_safety=enable_safety,
|
775 |
+
use_seed=use_seed,
|
776 |
+
seed_val=seed_val,
|
777 |
+
generate_num=generate_num,
|
778 |
+
scheduler=scheduler,
|
779 |
+
scheduler_eta=scheduler_eta,
|
780 |
+
enable_img2img=enable_img2img,
|
781 |
+
width=width,
|
782 |
+
height=height,
|
783 |
+
)
|
784 |
+
except Exception as e:
|
785 |
+
print(f"Error al ejecutar el modelo de procesamiento de imágenes: {e}")
|
786 |
+
return None
|
787 |
+
|
788 |
+
# Procesamiento de las imágenes generadas y conversión a base64
|
789 |
base64_str_lst = []
|
790 |
+
if enable_img2img:
|
791 |
+
use_correction = "border_mode"
|
792 |
+
|
793 |
+
for image in images:
|
794 |
+
# Ajusta la lógica de procesamiento de cada imagen según tu implementación
|
795 |
+
image = correction_func.run(pil.resize(image.size), image, mode=use_correction)
|
796 |
+
resized_img = image.resize((width, height), resample=SAMPLING_MODE,)
|
797 |
+
out = sel_buffer.copy()
|
798 |
+
out[:, :, 0:3] = np.array(resized_img)
|
799 |
+
out[:, :, -1] = 255
|
800 |
+
out_pil = Image.fromarray(out)
|
801 |
+
out_buffer = io.BytesIO()
|
802 |
+
out_pil.save(out_buffer, format="PNG")
|
803 |
+
out_buffer.seek(0)
|
804 |
+
base64_bytes = base64.b64encode(out_buffer.read())
|
805 |
+
base64_str = base64_bytes.decode("ascii")
|
806 |
+
base64_str_lst.append(base64_str)
|
807 |
+
|
808 |
+
return base64_str_lst
|
809 |
if enable_img2img:
|
810 |
use_correction = "border_mode"
|
811 |
for image in images:
|