Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Instalar las dependencias necesarias:
|
| 2 |
+
# Aseg煤rate de incluir estas en el archivo `requirements.txt` para Hugging Face Spaces:
|
| 3 |
+
# torch
|
| 4 |
+
# torchvision
|
| 5 |
+
# torchaudio
|
| 6 |
+
# diffusers
|
| 7 |
+
# huggingface_hub
|
| 8 |
+
# gradio
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
from diffusers import DiffusionPipeline
|
| 12 |
+
from huggingface_hub import login
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
+
# Configuraci贸n del modelo y adaptador LoRA
|
| 16 |
+
model_id = "black-forest-labs/FLUX.1-dev"
|
| 17 |
+
adapter_id = "Emuixom/Trasher_Riddick"
|
| 18 |
+
|
| 19 |
+
# Cargar el pipeline y los pesos LoRA
|
| 20 |
+
pipeline = DiffusionPipeline.from_pretrained(model_id)
|
| 21 |
+
pipeline.load_lora_weights(adapter_id)
|
| 22 |
+
|
| 23 |
+
# Configuraci贸n del dispositivo (usar GPU si est谩 disponible)
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
pipeline.to(device)
|
| 26 |
+
|
| 27 |
+
# Funci贸n para generar im谩genes
|
| 28 |
+
def generate_image(prompt, steps=15, width=512, height=512, guidance_scale=3.5, seed=1641421826):
|
| 29 |
+
# Generar la imagen con el prompt proporcionado
|
| 30 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 31 |
+
image = pipeline(
|
| 32 |
+
prompt=prompt,
|
| 33 |
+
num_inference_steps=steps,
|
| 34 |
+
generator=generator,
|
| 35 |
+
width=width,
|
| 36 |
+
height=height,
|
| 37 |
+
guidance_scale=guidance_scale,
|
| 38 |
+
).images[0]
|
| 39 |
+
|
| 40 |
+
# Guardar la imagen generada temporalmente
|
| 41 |
+
output_path = "output.png"
|
| 42 |
+
image.save(output_path, format="PNG")
|
| 43 |
+
return output_path
|
| 44 |
+
|
| 45 |
+
# Configurar la interfaz de Gradio
|
| 46 |
+
def gradio_interface(prompt, steps, width, height, guidance_scale, seed):
|
| 47 |
+
output_path = generate_image(prompt, steps, width, height, guidance_scale, seed)
|
| 48 |
+
return output_path
|
| 49 |
+
|
| 50 |
+
# Crear la interfaz
|
| 51 |
+
interface = gr.Interface(
|
| 52 |
+
fn=gradio_interface,
|
| 53 |
+
inputs=[
|
| 54 |
+
gr.Textbox(label="Prompt", placeholder="Describe la imagen que deseas generar"),
|
| 55 |
+
gr.Slider(label="Steps", minimum=1, maximum=50, value=15, step=1),
|
| 56 |
+
gr.Slider(label="Width", minimum=128, maximum=1024, value=512, step=64),
|
| 57 |
+
gr.Slider(label="Height", minimum=128, maximum=1024, value=512, step=64),
|
| 58 |
+
gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=3.5, step=0.5),
|
| 59 |
+
gr.Number(label="Seed", value=1641421826),
|
| 60 |
+
],
|
| 61 |
+
outputs=gr.Image(type="file", label="Imagen Generada"),
|
| 62 |
+
title="Generador de Im谩genes con Diffusion y LoRA",
|
| 63 |
+
description="Introduce un texto descriptivo para generar una imagen utilizando un modelo Diffusion con pesos LoRA.",
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Lanzar la aplicaci贸n
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
interface.launch()
|