File size: 2,399 Bytes
c823921
 
 
 
 
 
 
 
 
 
 
 
dea9181
 
c823921
52ad197
c823921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Instalar las dependencias necesarias:
# Aseg煤rate de incluir estas en el archivo `requirements.txt` para Hugging Face Spaces:
# torch
# torchvision
# torchaudio
# diffusers
# huggingface_hub
# gradio

import torch
from diffusers import DiffusionPipeline
from huggingface_hub import login
import gradio as gr

# Configuraci贸n del modelo y adaptador LoRA
model_id = "camenduru/FLUX.1-dev"
adapter_id = "Emuixom/Trasher_Riddick"

# Cargar el pipeline y los pesos LoRA
pipeline = DiffusionPipeline.from_pretrained(model_id)
pipeline.load_lora_weights(adapter_id)

# Configuraci贸n del dispositivo (usar GPU si est谩 disponible)
device = "cuda" if torch.cuda.is_available() else "cpu"
pipeline.to(device)

# Funci贸n para generar im谩genes
def generate_image(prompt, steps=15, width=512, height=512, guidance_scale=3.5, seed=1641421826):
    # Generar la imagen con el prompt proporcionado
    generator = torch.Generator(device=device).manual_seed(seed)
    image = pipeline(
        prompt=prompt,
        num_inference_steps=steps,
        generator=generator,
        width=width,
        height=height,
        guidance_scale=guidance_scale,
    ).images[0]

    # Guardar la imagen generada temporalmente
    output_path = "output.png"
    image.save(output_path, format="PNG")
    return output_path

# Configurar la interfaz de Gradio
def gradio_interface(prompt, steps, width, height, guidance_scale, seed):
    output_path = generate_image(prompt, steps, width, height, guidance_scale, seed)
    return output_path

# Crear la interfaz
interface = gr.Interface(
    fn=gradio_interface,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Describe la imagen que deseas generar"),
        gr.Slider(label="Steps", minimum=1, maximum=50, value=15, step=1),
        gr.Slider(label="Width", minimum=128, maximum=1024, value=512, step=64),
        gr.Slider(label="Height", minimum=128, maximum=1024, value=512, step=64),
        gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=3.5, step=0.5),
        gr.Number(label="Seed", value=1641421826),
    ],
    outputs=gr.Image(type="file", label="Imagen Generada"),
    title="Generador de Im谩genes con Diffusion y LoRA",
    description="Introduce un texto descriptivo para generar una imagen utilizando un modelo Diffusion con pesos LoRA.",
)

# Lanzar la aplicaci贸n
if __name__ == "__main__":
    interface.launch()