File size: 3,677 Bytes
6d1d2e6
e509ed3
0b49beb
34caf6a
 
 
6d1d2e6
 
7eff961
6d1d2e6
34caf6a
1ecc27a
34caf6a
 
 
 
1ecc27a
34caf6a
1ecc27a
 
 
 
 
 
 
 
 
 
3d72809
1ecc27a
6d1d2e6
 
34caf6a
6d1d2e6
34caf6a
6d1d2e6
 
 
4805adb
6d1d2e6
 
 
 
34caf6a
 
 
 
 
 
 
 
 
 
 
 
 
00971c6
34caf6a
 
17d1df1
 
 
9b9b72a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34caf6a
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
import os
import time
import uuid
import gradio as gr
from gradio_client import Client

hf_token = os.environ.get('HF_TOKEN')

sdxl_client = Client("https://fffiloni-sdxl-dpo.hf.space/")
faceswap_client = Client("https://fffiloni-deepfakeai.hf.space/", hf_token=hf_token)

def get_sdxl(prompt_in):
    sdxl_result = sdxl_client.predict(
        prompt_in,
        api_name="/infer"
    )
    return sdxl_result

def infer(portrait_in, prompt_in):
    # Generate Image from SDXL
    gr.Info("Generating SDXL image first ...")
    # Keep trying the operation until it succeeds without raising an exception
    while True:
        try:
            sdxl_result = get_sdxl(prompt_in)
            break  # Exit the while loop if the operation succeeded
        except Exception as e:
            print(f"Operation failed with error: {e}")
            time.sleep(10)  # Wait for 5 seconds before attempting again
    
    unique_id = str(uuid.uuid4())
    
    # Face Swap
    gr.Info("Face swap your face on result ...")
    faceswap_result = faceswap_client.predict(
        portrait_in,	# str (filepath or URL to image) in 'SOURCE IMAGE' Image component
        sdxl_result,	# str (filepath or URL to image) in 'TARGET IMAGE' Image component
        unique_id,	# str in 'parameter_12' Textbox component
        ["face_swapper", "face_enhancer"],	# List[str] in 'FRAME PROCESSORS' Checkboxgroup component
        "left-right",	# str (Option from: ['left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small']) in 'FACE ANALYSER DIRECTION' Dropdown component
        "none",	# str (Option from: ['none', 'reference', 'many']) in 'FACE RECOGNITION' Dropdown component
        "none",	# str (Option from: ['none', 'male', 'female']) in 'FACE ANALYSER GENDER' Dropdown component
        fn_index=1
    )

    return faceswap_result

css = """
#col-container{
    margin: 0 auto;
    max-width: 840px;
}
"""
with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.HTML("""
        <h2 style="text-align: center;">SDXL Auto FaceSwap</h2>
        """)
        with gr.Row():
            portrait_in = gr.Image(label="Your source portrait", type="filepath")
            result = gr.Image(label="Swapped SDXL Result")
        prompt_in = gr.Textbox(label="Prompt target")
        submit_btn = gr.Button("Submit")
       
        gr.Examples(
            examples = [
                [
                    "./examples/monalisa.png", 
                    "A beautiful brunette pilot girl, beautiful, moody lighting, best quality, full body portrait, real picture, intricate details, depth of field, in a cold snowstorm, , Fujifilm XT3, outdoors, Beautiful lighting, RAW photo, 8k uhd, film grain, unreal engine 5, ray trace, detail skin, realistic."
                ],
                [
                    "./examples/gustave.jpeg",
                    "close-up fantasy-inspired portrait of haute couture hauntingly handsome 19 year old Persian male fashion model looking directly into camera, warm brown eyes, roguish black hair, wearing black assassin robes and billowing black cape , background is desert at night, ethereal dreamy foggy, photoshoot by Alessio Albi , editorial Fashion Magazine photoshoot, fashion poses, . Kinfolk Magazine. Film Grain."
                ]
            ],
            inputs = [
                portrait_in, 
                prompt_in
            ]
        )

        submit_btn.click(
            fn = infer, 
            inputs = [
                portrait_in,
                prompt_in
            ],
            outputs = [
                result
            ]  
        )

demo.queue().launch()