File size: 2,915 Bytes
31e9635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cbf0c9
31e9635
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
import gradio as gr
import config
import face_swapper

def render_cpu_tools(input_img):
    with gr.Accordion("🛠️ Standalone Image Face Swapper & GFPGAN Restoration Tool", open=True):
        with gr.Row():
            tool_target_img = gr.Image(type="pil", label="Target Image to Swap / Enhance", sources=["upload", "clipboard"], height=180)
            tool_ref_img = gr.Image(type="pil", label="Reference Face Image (Optional)", sources=["upload", "clipboard"], height=180)
        with gr.Row():
            tool_gender_dropdown = gr.Dropdown(
                choices=["Any / All Faces", "Female Faces Only", "Male Faces Only"],
                value="Any / All Faces",
                label="👥 Target Gender to Swap"
            )
            tool_gfpgan_checkbox = gr.Checkbox(
                label="✨ Enhance Face Detail (GFPGAN v1.4)",
                value=True,
                info="Sharpens blurry facial details via GFPGAN restoration"
            )
        with gr.Row():
            tool_swap_btn = gr.Button("🚀 Swap Single Image Face (CPU)", variant="primary")
            tool_enhance_only_btn = gr.Button("✨ Enhance Face Only (GFPGAN v1.4)", variant="secondary")

        tool_result_img = gr.Image(type="pil", label="Result Image (Swapped / Enhanced)", interactive=False, height=200)
        tool_send_btn = gr.Button("📸 Send Result Image to Main Video Input", variant="secondary")

    def run_standalone_swap(target_img, ref_img, gender, enhance_gfpgan):
        if target_img is None:
            raise gr.Error("Please upload a target image to swap.")
        return face_swapper.swap_face_in_single_image(target_img, ref_img, gender, enhance_with_gfpgan=enhance_gfpgan)

    def run_standalone_face_enhance(target_img):
        if target_img is None:
            raise gr.Error("Please upload an image to enhance.")
        res = face_swapper.call_sulphur_enhance_face_api(target_img)
        if res is None:
            raise gr.Error("Sulphur AI GFPGAN Face Enhance API is offline or not configured.")
        return res

    tool_swap_btn.click(
        fn=run_standalone_swap,
        inputs=[tool_target_img, tool_ref_img, tool_gender_dropdown, tool_gfpgan_checkbox],
        outputs=[tool_result_img]
    )

    tool_enhance_only_btn.click(
        fn=run_standalone_face_enhance,
        inputs=[tool_target_img],
        outputs=[tool_result_img]
    )

    tool_send_btn.click(
        fn=lambda img: img,
        inputs=[tool_result_img],
        outputs=[input_img]
    )

    return {
        "tool_target_img": tool_target_img,
        "tool_ref_img": tool_ref_img,
        "tool_gender_dropdown": tool_gender_dropdown,
        "tool_gfpgan_checkbox": tool_gfpgan_checkbox,
        "tool_swap_btn": tool_swap_btn,
        "tool_enhance_only_btn": tool_enhance_only_btn,
        "tool_result_img": tool_result_img,
        "tool_send_btn": tool_send_btn
    }