Inspecta commited on
Commit
a894dcf
·
verified ·
1 Parent(s): b305711

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pathlib import Path
3
+ import subprocess, json, tempfile, sys
4
+
5
+ COMFY_DIR = Path("ComfyUI")
6
+ WORKFLOW_JSON = Path("workflow_api.json") # we’ll create it later
7
+
8
+ def patch_workflow(wf, pic_path, face_path, prompt):
9
+ for node in wf.values():
10
+ if node.get("_meta", {}).get("title") == "Picture to swap":
11
+ node["inputs"]["image"] = str(pic_path)
12
+ if node.get("_meta", {}).get("title") == "Input face":
13
+ node["inputs"]["image"] = str(face_path)
14
+ if node["class_type"] == "easy positive":
15
+ node["inputs"]["text"] = prompt
16
+ return wf
17
+
18
+ def run(picture, face, positive):
19
+ tmpdir = Path(tempfile.mkdtemp())
20
+ pic_path = tmpdir / "pic.png"
21
+ face_path = tmpdir / "face.png"
22
+ picture.save(pic_path)
23
+ face.save(face_path)
24
+
25
+ with open(WORKFLOW_JSON) as f:
26
+ wf = json.load(f)
27
+ wf = patch_workflow(wf, pic_path, face_path, positive)
28
+ tmp_wf = tmpdir / "wf.json"
29
+ with open(tmp_wf, "w") as f:
30
+ json.dump(wf, f)
31
+
32
+ subprocess.run([
33
+ sys.executable, "ComfyUI/main.py",
34
+ "--disable-auto-launch",
35
+ "--input", str(tmpdir),
36
+ "--output", str(tmpdir),
37
+ "--workflow", str(tmp_wf)
38
+ ], cwd=COMFY_DIR, check=True)
39
+
40
+ out = list(tmpdir.glob("*.png"))
41
+ if not out:
42
+ raise RuntimeError("No output image")
43
+ return out[0]
44
+
45
+ demo = gr.Interface(
46
+ fn=run,
47
+ inputs=[
48
+ gr.Image(type="pil", label="Picture to swap"),
49
+ gr.Image(type="pil", label="Face input"),
50
+ gr.Textbox(label="Positive prompt", lines=3,
51
+ placeholder="portrait of a woman, detailed face...")
52
+ ],
53
+ outputs=gr.Image(type="filepath", label="Swapped"),
54
+ title="ComfyUI Face Swap"
55
+ )
56
+ demo.queue().launch()