FaceFusion / app.py
leonelhs's picture
starting from the scratch
8e7e652
#######################################################################################
#
# MIT License
#
# Copyright (c) [2025] [leonelhs@gmail.com]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#######################################################################################
#
# Source code is based on or inspired by several projects.
# For more details and proper attribution, please refer to the following resources:
#
# - [Deepinsight] - [https://github.com/deepinsight/insightface]
# - [FaceFusion] [https://github.com/facefusion/facefusion]
#
import gradio as gr
from huggingface_hub import hf_hub_download
from itertools import islice
from face_analysis import FaceAnalysis
from models.inswapper import INSwapper
REPO_ID = "leonelhs/insightface"
model_inswapper_path = hf_hub_download(repo_id=REPO_ID, filename="inswapper_128.onnx")
face_analyser = FaceAnalysis()
swapper = INSwapper(model_inswapper_path)
def predict(src_img, dst_img):
# Get faces
src_faces = face_analyser.get(src_img)
dst_faces = face_analyser.get(dst_img)
# Swap the first face found
if len(src_faces) > 0 and len(dst_faces) > 0:
return dst_img, swapper.get(dst_img, dst_faces[0], src_faces[0], paste_back=True)
else:
raise gr.Error("No faces were found!")
with gr.Blocks(title="FaceFusion") as app:
navbar = gr.Navbar(visible=True, main_page_name="Workspace")
gr.Markdown("## FaceFusion Lite")
with gr.Row():
with gr.Column(scale=1):
with gr.Row():
source_image = gr.Image(type="numpy", label="Face image")
target_image = gr.Image(type="numpy", label="Body image")
image_btn = gr.Button("Swap face")
with gr.Column(scale=1):
with gr.Row():
output_image = gr.ImageSlider(label="Swapped image", type="pil")
image_btn.click(
fn=predict,
inputs=[source_image, target_image],
outputs=output_image,
)
with app.route("Readme", "/readme"):
with open("README.md") as f:
for line in islice(f, 12, None):
gr.Markdown(line.strip())
app.launch(share=False, debug=True, show_error=True, mcp_server=True, pwa=True)
app.queue()