Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,47 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
demo = gr.Interface(fn=generate, inputs="text", outputs="image")
|
| 19 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline, DDIMScheduler
|
| 4 |
+
from ip_adapter.ip_adapter_faceid import IPAdapterFaceID
|
| 5 |
+
from insightface.app import FaceAnalysis
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
# Configuraci贸n inicial
|
| 10 |
+
device = "cuda"
|
| 11 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 12 |
+
ip_ckpt = "ip-adapter-faceid_sd15.bin"
|
| 13 |
+
|
| 14 |
+
# Cargar el pipeline
|
| 15 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to(device)
|
| 16 |
+
app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider'])
|
| 17 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 18 |
+
|
| 19 |
+
# Cargar IP-Adapter
|
| 20 |
+
ip_model = IPAdapterFaceID(pipe, ip_ckpt, device)
|
| 21 |
|
| 22 |
+
def generate_image(input_image, prompt):
|
| 23 |
+
# Procesar cara
|
| 24 |
+
img_cv = cv2.cvtColor(np.array(input_image), cv2.COLOR_RGB2BGR)
|
| 25 |
+
faces = app.get(img_cv)
|
| 26 |
+
if not faces:
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
# Generar
|
| 30 |
+
image = ip_model.generate(
|
| 31 |
+
pil_image=input_image,
|
| 32 |
+
face_embed=faces[0].embedding,
|
| 33 |
+
prompt=prompt,
|
| 34 |
+
width=512,
|
| 35 |
+
height=512
|
| 36 |
+
)
|
| 37 |
+
return image[0]
|
| 38 |
|
| 39 |
+
# Interfaz de Gradio
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=generate_image,
|
| 42 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Prompt")],
|
| 43 |
+
outputs="image",
|
| 44 |
+
title="IP-Adapter FaceID Generator"
|
| 45 |
+
)
|
| 46 |
|
|
|
|
| 47 |
demo.launch()
|