File size: 1,649 Bytes
ce78d19
 
 
 
 
 
d857e0c
 
1a527b6
51d8073
 
 
b588e4a
cf4e354
 
5ef2565
d857e0c
 
b588e4a
d857e0c
51d8073
d857e0c
cf4e354
51d8073
ce451f8
cf4e354
ce451f8
 
51d8073
d857e0c
ce78d19
ce451f8
51d8073
 
42d6b3b
51d8073
1a527b6
d857e0c
 
51d8073
ce451f8
 
b588e4a
d857e0c
 
 
 
51d8073
d857e0c
1a527b6
ce78d19
d857e0c
d3eab7e
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
# 1. IMPORTACIÓN PREVENTIVA: Si 'spaces' existe, debe ir primero
try:
    import spaces
except ImportError:
    pass

import sys
import os
import gradio as gr
import torch
import cv2
import numpy as np
from PIL import Image
from diffusers import StableDiffusionPipeline
from huggingface_hub import hf_hub_download

# --- FORZAR RUTA PARA QUE ENCUENTRE EL MÓDULO ---
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from ip_adapter.ip_adapter_faceid import IPAdapterFaceID
from insightface.app import FaceAnalysis

# Configuración
device = "cpu"
model_id = "runwayml/stable-diffusion-v1-5"

# Descarga automática del modelo
print("Descargando/Verificando el modelo IP-Adapter...")
ip_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid_sd15.bin")

# Carga de modelos
# NOTA: Al usar 'onnxruntime' (versión CPU) en requirements.txt, esto debería ir fluido
app = FaceAnalysis(name="buffalo_l", providers=['CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))

pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32).to(device)
ip_model = IPAdapterFaceID(pipe, ip_ckpt, device)

def generate(image, prompt):
    img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    faces = app.get(img_cv)
    if not faces: 
        return None
    
    # Generar
    results = ip_model.generate(
        pil_image=image, face_embed=faces[0].embedding, 
        prompt=prompt, width=512, height=512
    )
    return results[0]

# Lanzar con opciones optimizadas
demo = gr.Interface(fn=generate, inputs=[gr.Image(type="pil"), gr.Textbox()], outputs="image")
demo.launch()