File size: 2,652 Bytes
ec2943e
5ccc57b
 
a894dcf
 
 
 
b436e03
a894dcf
5ccc57b
a894dcf
c22b152
 
b436e03
 
a894dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b436e03
a894dcf
 
 
 
b436e03
5ccc57b
 
a894dcf
 
 
 
5ccc57b
 
c22b152
b436e03
 
 
 
 
 
 
5ccc57b
b436e03
a894dcf
b436e03
a894dcf
 
5ccc57b
b436e03
 
 
 
 
a894dcf
 
 
 
 
 
 
 
 
b436e03
 
 
 
396fdec
 
a894dcf
5ccc57b
b436e03
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
os.environ["MPLCONFIGDIR"] = "/tmp"  # matplotlib sans ~/.config

import gradio as gr
from pathlib import Path
import subprocess, json, tempfile, sys

# Chemin vers ComfyUI et le workflow JSON
COMFY_DIR = Path("ComfyUI")
WORKFLOW_JSON = Path("workflow_api.json")

# ✅ Correction ici : logs en dossier local
LOGS_DIR = Path("logs")
LOGS_DIR.mkdir(parents=True, exist_ok=True)

def patch_workflow(wf, pic_path, face_path, prompt):
    for node in wf.values():
        if node.get("_meta", {}).get("title") == "Picture to swap":
            node["inputs"]["image"] = str(pic_path)
        if node.get("_meta", {}).get("title") == "Input face":
            node["inputs"]["image"] = str(face_path)
        if node["class_type"] == "easy positive":
            node["inputs"]["text"] = prompt
    return wf

def run(picture, face, positive):
    tmpdir = Path(tempfile.mkdtemp())
    pic_path  = tmpdir / "pic.png"
    face_path = tmpdir / "face.png"
    picture.save(pic_path)
    face.save(face_path)

    with open(WORKFLOW_JSON) as f:
        wf = json.load(f)
    wf = patch_workflow(wf, pic_path, face_path, positive)

    tmp_wf = tmpdir / "wf.json"
    with open(tmp_wf, "w") as f:
        json.dump(wf, f)

    # Lancement de ComfyUI
    result = subprocess.run([
        sys.executable, "main.py",
        "--disable-auto-launch",
        "--input", str(tmpdir),
        "--output", str(tmpdir),
        "--workflow", str(tmp_wf)
    ], cwd=COMFY_DIR, capture_output=True, text=True)

    # ✅ Sauvegarde des logs dans ./logs
    log_file = LOGS_DIR / "comfy.txt"
    with open(log_file, "w") as f:
        f.write("=== STDOUT ===\n")
        f.write(result.stdout)
        f.write("\n=== STDERR ===\n")
        f.write(result.stderr)

    if result.returncode != 0:
        raise RuntimeError("ComfyUI failed to run — voir comfy.txt dans Files.")

    # Vérifie la sortie image
    out = list(tmpdir.glob("*.png"))
    if not out:
        raise RuntimeError("No output image generated.")

    final_img = LOGS_DIR / "output.png"
    out[0].replace(final_img)

    return final_img, log_file

demo = gr.Interface(
    fn=run,
    inputs=[
        gr.Image(type="pil", label="Picture to swap"),
        gr.Image(type="pil", label="Face input"),
        gr.Textbox(label="Positive prompt", lines=3,
                   placeholder="portrait of a woman, detailed face...")
    ],
    outputs=[
        gr.Image(type="filepath", label="Swapped"),
        gr.File(label="ComfyUI logs")
    ],
    title="ComfyUI InstantID Face Swap",
    allow_flagging="never"
)

demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)