Spaces:
Runtime error
Runtime error
| # 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() |