| 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): |
| |
| gr.Info("Generating SDXL image first ...") |
| |
| while True: |
| try: |
| sdxl_result = get_sdxl(prompt_in) |
| break |
| except Exception as e: |
| print(f"Operation failed with error: {e}") |
| time.sleep(3) |
| |
| unique_id = str(uuid.uuid4()) |
| |
| |
| gr.Info("Face swap your face on result ...") |
| faceswap_result = faceswap_client.predict( |
| portrait_in, |
| sdxl_result, |
| unique_id, |
| ["face_swapper", "face_enhancer"], |
| "left-right", |
| "none", |
| "none", |
| 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(): |
| with gr.Group(): |
| with gr.Column(): |
| portrait_in = gr.Image(label="Your face portrait", type="filepath") |
| prompt_in = gr.Textbox(label="Prompt to desired portrait using your own face") |
| submit_btn = gr.Button("Submit") |
| with gr.Column(): |
| result = gr.Image(label="Result") |
|
|
| submit_btn.click( |
| fn = infer, |
| inputs = [ |
| portrait_in, |
| prompt_in |
| ], |
| outputs = [ |
| result |
| ] |
| ) |
|
|
| demo.queue().launch() |