Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,42 @@
|
|
| 1 |
import torch
|
| 2 |
-
import spaces
|
| 3 |
-
from diffusers import FluxPipeline
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
-
# Carga
|
| 7 |
model_id = "black-forest-labs/FLUX.1-schnell"
|
|
|
|
| 8 |
pipe = FluxPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16).to("cuda")
|
|
|
|
|
|
|
|
|
|
| 9 |
pipe.load_lora_weights("joyfox/Kontext-Cosplay-Lora", weight_name="kontext_cosplay_lora_20000.safetensors")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
@spaces.GPU
|
| 13 |
def generate_image(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
full_prompt = f"{prompt}, r2cos"
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return image
|
| 17 |
|
| 18 |
-
# Interfaz
|
| 19 |
-
demo = gr.Interface(
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import spaces
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
+
from diffusers import FluxPipeline
|
| 5 |
|
| 6 |
+
# 1. Carga del modelo (Fuera de la funci贸n para no recargar cada vez)
|
| 7 |
model_id = "black-forest-labs/FLUX.1-schnell"
|
| 8 |
+
print("Cargando modelo en GPU...")
|
| 9 |
pipe = FluxPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16).to("cuda")
|
| 10 |
+
|
| 11 |
+
# 2. Carga del LoRA
|
| 12 |
+
print("Inyectando LoRA...")
|
| 13 |
pipe.load_lora_weights("joyfox/Kontext-Cosplay-Lora", weight_name="kontext_cosplay_lora_20000.safetensors")
|
| 14 |
|
| 15 |
+
# 3. Funci贸n de generaci贸n con decorador y semilla fija
|
| 16 |
@spaces.GPU
|
| 17 |
def generate_image(prompt):
|
| 18 |
+
# Usamos una semilla fija (42) para que la imagen sea consistente
|
| 19 |
+
# y no "parpadee" o cambie dr谩sticamente cada vez.
|
| 20 |
+
generator = torch.Generator(device="cuda").manual_seed(42)
|
| 21 |
+
|
| 22 |
full_prompt = f"{prompt}, r2cos"
|
| 23 |
+
|
| 24 |
+
# Subimos steps a 6 para mayor estabilidad visual
|
| 25 |
+
image = pipe(
|
| 26 |
+
full_prompt,
|
| 27 |
+
num_inference_steps=6,
|
| 28 |
+
guidance_scale=0.0,
|
| 29 |
+
generator=generator
|
| 30 |
+
).images[0]
|
| 31 |
return image
|
| 32 |
|
| 33 |
+
# 4. Interfaz de Gradio
|
| 34 |
+
demo = gr.Interface(
|
| 35 |
+
fn=generate_image,
|
| 36 |
+
inputs=gr.Textbox(label="Describe al personaje"),
|
| 37 |
+
outputs=gr.Image(label="Resultado"),
|
| 38 |
+
title="Generador de Cosplay AI"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
demo.launch()
|