| | import gradio as gr |
| | from PIL import Image |
| | import torch |
| | from diffusers import StableDiffusionImg2ImgPipeline, StableDiffusionInpaintPipeline |
| | import spaces |
| | from huggingface_hub import login |
| | from dotenv import load_dotenv |
| | import os |
| |
|
| | |
| | load_dotenv() |
| |
|
| | |
| | huggingface_token = os.getenv('HUGGINGFACE_TOKEN') |
| |
|
| | |
| | def huggingface_login(token): |
| | try: |
| | login(token) |
| | print("Autenticaci贸n exitosa con Hugging Face.") |
| | except Exception as e: |
| | print(f"Error al autenticar con Hugging Face: {e}") |
| |
|
| | |
| | huggingface_login(huggingface_token) |
| |
|
| | |
| | device = "cuda" if torch.cuda.is_available() else "cpu" |
| |
|
| | |
| | model_id_img2img = "CompVis/stable-diffusion-v1-4" |
| | pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(model_id_img2img).to(device) |
| |
|
| | |
| | model_id_inpainting = "stabilityai/stable-diffusion-inpainting" |
| | pipe_inpainting = StableDiffusionInpaintPipeline.from_pretrained(model_id_inpainting).to(device) |
| |
|
| | @spaces.GPU(duration=0) |
| | def cartoonize_image(image, prompt): |
| | try: |
| | |
| | image = image.convert("RGB") |
| | image = image.resize((512, 512)) |
| |
|
| | |
| | with torch.no_grad(): |
| | result = pipe_img2img(prompt=prompt, init_image=image, strength=0.75).images[0] |
| | |
| | return result |
| | except Exception as e: |
| | return f"Error procesando la imagen: {e}" |
| |
|
| | @spaces.GPU(duration=0) |
| | def edit_image(image, mask, prompt): |
| | try: |
| | |
| | image = image.convert("RGB") |
| | mask = mask.convert("L") |
| |
|
| | |
| | with torch.no_grad(): |
| | result = pipe_inpainting(prompt=prompt, image=image, mask_image=mask).images[0] |
| | |
| | return result |
| | except Exception as e: |
| | return f"Error editando la imagen: {e}" |
| |
|
| | def main(): |
| | with gr.Blocks() as demo: |
| | gr.Markdown("## Conversi贸n de Imagen a Caricatura Estilo Pixar y Edici贸n de Im谩genes") |
| | |
| | with gr.Tab("Caricaturizar"): |
| | with gr.Row(): |
| | with gr.Column(): |
| | input_image = gr.Image(type="pil", label="Sube tu imagen") |
| | prompt = gr.Textbox(label="Prompt", value="A Pixar-style cartoon character") |
| | |
| | with gr.Row(): |
| | submit_button = gr.Button("Submit") |
| | |
| | output_image = gr.Image(type="pil", label="Imagen Caricaturizada") |
| | |
| | def process_image(image, prompt): |
| | return cartoonize_image(image, prompt) |
| | |
| | submit_button.click( |
| | fn=process_image, |
| | inputs=[input_image, prompt], |
| | outputs=output_image |
| | ) |
| | |
| | with gr.Tab("Editar Imagen"): |
| | with gr.Row(): |
| | with gr.Column(): |
| | input_image = gr.Image(type="pil", label="Sube tu imagen a editar") |
| | mask_image = gr.Image(type="pil", label="Sube una m谩scara (blanco para la parte a editar)") |
| | prompt_edit = gr.Textbox(label="Describe los cambios", value="Cambia el color del objeto a rojo") |
| | |
| | with gr.Row(): |
| | submit_edit_button = gr.Button("Submit Edici贸n") |
| | |
| | output_image_edit = gr.Image(type="pil", label="Imagen Editada") |
| | |
| | def process_edit_image(image, mask, prompt_edit): |
| | return edit_image(image, mask, prompt_edit) |
| | |
| | submit_edit_button.click( |
| | fn=process_edit_image, |
| | inputs=[input_image, mask_image, prompt_edit], |
| | outputs=output_image_edit |
| | ) |
| | |
| | demo.launch() |
| |
|
| | if __name__ == "__main__": |
| | main() |
| |
|