appinitdev commited on
Commit
257e36c
路
verified 路
1 Parent(s): 7e0debc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -1,27 +1,46 @@
 
1
  import torch
2
  from diffusers import AutoPipelineForText2Image
3
  import gradio as gr
4
 
5
- # Cargar modelo (se ejecuta una sola vez)
 
 
 
6
  pipe = AutoPipelineForText2Image.from_pretrained(
7
  "black-forest-labs/FLUX.1-dev",
8
- torch_dtype=torch.bfloat16
 
9
  )
10
 
11
- pipe = pipe.to("cuda")
 
 
 
 
 
 
 
12
 
13
  # Cargar LoRA
14
- pipe.load_lora_weights(
15
- "enhanceaiteam/Flux-uncensored-v2",
16
- weight_name="lora.safetensors"
17
- )
 
 
 
18
 
19
  # Funci贸n para generar imagen
20
  def generate(prompt):
21
- image = pipe(prompt).images[0]
 
 
 
 
22
  return image
23
 
24
- # Interfaz
25
  demo = gr.Interface(
26
  fn=generate,
27
  inputs=gr.Textbox(label="Prompt"),
 
1
+ import os
2
  import torch
3
  from diffusers import AutoPipelineForText2Image
4
  import gradio as gr
5
 
6
+ # Detectar dispositivo
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+
9
+ # Cargar modelo con token (OBLIGATORIO para FLUX)
10
  pipe = AutoPipelineForText2Image.from_pretrained(
11
  "black-forest-labs/FLUX.1-dev",
12
+ torch_dtype=torch.float16, # m谩s compatible que bfloat16
13
+ token=os.getenv("HF_TOKEN") # <-- clave aqu铆
14
  )
15
 
16
+ pipe = pipe.to(device)
17
+
18
+ # Optimizaci贸n (MUY recomendado en Spaces)
19
+ pipe.enable_attention_slicing()
20
+ pipe.enable_vae_slicing()
21
+
22
+ # Opcional: si tienes poca VRAM
23
+ # pipe.enable_model_cpu_offload()
24
 
25
  # Cargar LoRA
26
+ try:
27
+ pipe.load_lora_weights(
28
+ "enhanceaiteam/Flux-uncensored-v2",
29
+ weight_name="lora.safetensors"
30
+ )
31
+ except Exception as e:
32
+ print("Error cargando LoRA:", e)
33
 
34
  # Funci贸n para generar imagen
35
  def generate(prompt):
36
+ image = pipe(
37
+ prompt,
38
+ num_inference_steps=20, # m谩s r谩pido
39
+ guidance_scale=7.5
40
+ ).images[0]
41
  return image
42
 
43
+ # UI
44
  demo = gr.Interface(
45
  fn=generate,
46
  inputs=gr.Textbox(label="Prompt"),