Update app.py
Browse files
app.py
CHANGED
@@ -5,9 +5,14 @@ import gradio as gr
|
|
5 |
from pathlib import Path
|
6 |
import subprocess, json, tempfile, sys
|
7 |
|
|
|
8 |
COMFY_DIR = Path("ComfyUI")
|
9 |
WORKFLOW_JSON = Path("workflow_api.json")
|
10 |
|
|
|
|
|
|
|
|
|
11 |
def patch_workflow(wf, pic_path, face_path, prompt):
|
12 |
for node in wf.values():
|
13 |
if node.get("_meta", {}).get("title") == "Picture to swap":
|
@@ -28,10 +33,12 @@ def run(picture, face, positive):
|
|
28 |
with open(WORKFLOW_JSON) as f:
|
29 |
wf = json.load(f)
|
30 |
wf = patch_workflow(wf, pic_path, face_path, positive)
|
|
|
31 |
tmp_wf = tmpdir / "wf.json"
|
32 |
with open(tmp_wf, "w") as f:
|
33 |
json.dump(wf, f)
|
34 |
|
|
|
35 |
result = subprocess.run([
|
36 |
sys.executable, "main.py",
|
37 |
"--disable-auto-launch",
|
@@ -40,17 +47,26 @@ def run(picture, face, positive):
|
|
40 |
"--workflow", str(tmp_wf)
|
41 |
], cwd=COMFY_DIR, capture_output=True, text=True)
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
if result.returncode != 0:
|
44 |
-
|
45 |
-
print(result.stdout)
|
46 |
-
print("=== STDERR ===")
|
47 |
-
print(result.stderr)
|
48 |
-
raise RuntimeError("ComfyUI failed to run")
|
49 |
|
|
|
50 |
out = list(tmpdir.glob("*.png"))
|
51 |
if not out:
|
52 |
raise RuntimeError("No output image generated.")
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
|
55 |
demo = gr.Interface(
|
56 |
fn=run,
|
@@ -60,9 +76,12 @@ demo = gr.Interface(
|
|
60 |
gr.Textbox(label="Positive prompt", lines=3,
|
61 |
placeholder="portrait of a woman, detailed face...")
|
62 |
],
|
63 |
-
outputs=
|
|
|
|
|
|
|
64 |
title="ComfyUI InstantID Face Swap",
|
65 |
allow_flagging="never"
|
66 |
)
|
67 |
|
68 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
5 |
from pathlib import Path
|
6 |
import subprocess, json, tempfile, sys
|
7 |
|
8 |
+
# Chemin vers ComfyUI et le workflow JSON
|
9 |
COMFY_DIR = Path("ComfyUI")
|
10 |
WORKFLOW_JSON = Path("workflow_api.json")
|
11 |
|
12 |
+
# S'assurer que le dossier de logs existe et est visible par Hugging Face
|
13 |
+
LOGS_DIR = Path("/app/logs")
|
14 |
+
LOGS_DIR.mkdir(parents=True, exist_ok=True)
|
15 |
+
|
16 |
def patch_workflow(wf, pic_path, face_path, prompt):
|
17 |
for node in wf.values():
|
18 |
if node.get("_meta", {}).get("title") == "Picture to swap":
|
|
|
33 |
with open(WORKFLOW_JSON) as f:
|
34 |
wf = json.load(f)
|
35 |
wf = patch_workflow(wf, pic_path, face_path, positive)
|
36 |
+
|
37 |
tmp_wf = tmpdir / "wf.json"
|
38 |
with open(tmp_wf, "w") as f:
|
39 |
json.dump(wf, f)
|
40 |
|
41 |
+
# Lancement de ComfyUI
|
42 |
result = subprocess.run([
|
43 |
sys.executable, "main.py",
|
44 |
"--disable-auto-launch",
|
|
|
47 |
"--workflow", str(tmp_wf)
|
48 |
], cwd=COMFY_DIR, capture_output=True, text=True)
|
49 |
|
50 |
+
# Sauvegarder les logs dans un fichier visible
|
51 |
+
log_file = LOGS_DIR / "comfy.txt"
|
52 |
+
with open(log_file, "w") as f:
|
53 |
+
f.write("=== STDOUT ===\n")
|
54 |
+
f.write(result.stdout)
|
55 |
+
f.write("\n=== STDERR ===\n")
|
56 |
+
f.write(result.stderr)
|
57 |
+
|
58 |
if result.returncode != 0:
|
59 |
+
raise RuntimeError("ComfyUI failed to run — voir comfy.txt dans Files.")
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
# Vérifie la sortie image
|
62 |
out = list(tmpdir.glob("*.png"))
|
63 |
if not out:
|
64 |
raise RuntimeError("No output image generated.")
|
65 |
+
|
66 |
+
final_img = LOGS_DIR / "output.png"
|
67 |
+
out[0].replace(final_img)
|
68 |
+
|
69 |
+
return final_img, log_file
|
70 |
|
71 |
demo = gr.Interface(
|
72 |
fn=run,
|
|
|
76 |
gr.Textbox(label="Positive prompt", lines=3,
|
77 |
placeholder="portrait of a woman, detailed face...")
|
78 |
],
|
79 |
+
outputs=[
|
80 |
+
gr.Image(type="filepath", label="Swapped"),
|
81 |
+
gr.File(label="ComfyUI logs")
|
82 |
+
],
|
83 |
title="ComfyUI InstantID Face Swap",
|
84 |
allow_flagging="never"
|
85 |
)
|
86 |
|
87 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|