import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
import gradio as gr
from refacer import Refacer
import argparse
import ngrok
import imageio
import numpy as np
from PIL import Image
import tempfile
import base64
import pyfiglet
import shutil
import time
print("\033[94m" + pyfiglet.Figlet(font='slant').renderText("NeoRefacer") + "\033[0m")
def cleanup_temp(folder_path):
try:
shutil.rmtree(folder_path)
print("Gradio cache cleared successfully.")
except Exception as e:
print(f"Error: {e}")
# Prepare temp folder
os.environ["GRADIO_TEMP_DIR"] = "./tmp"
if os.path.exists("./tmp"):
cleanup_temp(os.environ['GRADIO_TEMP_DIR'])
if not os.path.exists("./tmp"):
os.makedirs("./tmp")
# Parse arguments
parser = argparse.ArgumentParser(description='Refacer')
parser.add_argument("--max_num_faces", type=int, default=8)
parser.add_argument("--force_cpu", default=False, action="store_true")
parser.add_argument("--share_gradio", default=False, action="store_true")
parser.add_argument("--server_name", type=str, default="127.0.0.1")
parser.add_argument("--server_port", type=int, default=7860)
parser.add_argument("--colab_performance", default=False, action="store_true")
parser.add_argument("--ngrok", type=str, default=None)
parser.add_argument("--ngrok_region", type=str, default="us")
args = parser.parse_args()
# Initialize
refacer = Refacer(force_cpu=args.force_cpu, colab_performance=args.colab_performance)
num_faces = args.max_num_faces
def create_dummy_image():
dummy = Image.new('RGB', (1, 1), color=(255, 255, 255))
temp_file = tempfile.NamedTemporaryFile(delete=False, dir="./tmp", suffix=".png")
dummy.save(temp_file.name)
return temp_file.name
def run_image(*vars):
image_path = vars[0]
origins = vars[1:(num_faces+1)]
destinations = vars[(num_faces+1):(num_faces*2)+1]
thresholds = vars[(num_faces*2)+1:-1]
face_mode = vars[-1]
disable_similarity = (face_mode in ["Single Face", "Multiple Faces"])
multiple_faces_mode = (face_mode == "Multiple Faces")
faces = []
for k in range(num_faces):
if destinations[k] is not None:
faces.append({
'origin': origins[k] if not multiple_faces_mode else None,
'destination': destinations[k],
'threshold': thresholds[k] if not multiple_faces_mode else 0.0
})
return refacer.reface_image(image_path, faces, disable_similarity=disable_similarity, multiple_faces_mode=multiple_faces_mode)
def run(*vars):
video_path = vars[0]
origins = vars[1:(num_faces+1)]
destinations = vars[(num_faces+1):(num_faces*2)+1]
thresholds = vars[(num_faces*2)+1:-2]
preview = vars[-2]
face_mode = vars[-1]
disable_similarity = (face_mode in ["Single Face", "Multiple Faces"])
multiple_faces_mode = (face_mode == "Multiple Faces")
faces = []
for k in range(num_faces):
if destinations[k] is not None:
faces.append({
'origin': origins[k] if not multiple_faces_mode else None,
'destination': destinations[k],
'threshold': thresholds[k] if not multiple_faces_mode else 0.0
})
mp4_path, gif_path = refacer.reface(video_path, faces, preview=preview, disable_similarity=disable_similarity, multiple_faces_mode=multiple_faces_mode)
return mp4_path, gif_path if gif_path else None
def load_first_frame(filepath):
if filepath is None:
return None
frames = imageio.get_reader(filepath)
return frames.get_data(0)
def extract_faces_auto(filepath, refacer_instance, max_faces=5, isvideo=False):
if filepath is None:
return [None] * max_faces
# Check if video
if isvideo:
if os.path.getsize(filepath) > 5 * 1024 * 1024: # larger than 5MB
print("Video too large for auto-extract, skipping face extraction.")
return [None] * max_faces
frame = load_first_frame(filepath)
if frame is None:
return [None] * max_faces
# Create manual temp image inside ./tmp
temp_image_path = os.path.join("./tmp", f"temp_face_extract_{int(time.time() * 1000)}.png")
Image.fromarray(frame).save(temp_image_path)
try:
faces = refacer_instance.extract_faces_from_image(temp_image_path, max_faces=max_faces)
output_faces = faces + [None] * (max_faces - len(faces))
return output_faces
finally:
if os.path.exists(temp_image_path):
try:
os.remove(temp_image_path)
except Exception as e:
print(f"Warning: Could not delete temp file {temp_image_path}: {e}")
def toggle_tabs_and_faces(mode, face_tabs, origin_faces):
if mode == "Single Face":
tab_updates = [gr.update(visible=(i == 0)) for i in range(len(face_tabs))]
origin_updates = [gr.update(visible=False) for _ in range(len(origin_faces))]
elif mode == "Multiple Faces":
tab_updates = [gr.update(visible=True) for _ in range(len(face_tabs))]
origin_updates = [gr.update(visible=False) for _ in range(len(origin_faces))]
else:
tab_updates = [gr.update(visible=True) for _ in range(len(face_tabs))]
origin_updates = [gr.update(visible=True) for _ in range(len(origin_faces))]
return tab_updates + origin_updates
# --- UI ---
theme = gr.themes.Base(primary_hue="blue", secondary_hue="cyan")
with gr.Blocks(theme=theme, title="NeoRefacer - AI Refacer") as demo:
with open("icon.png", "rb") as f:
icon_data = base64.b64encode(f.read()).decode()
icon_html = f''
with gr.Row():
gr.Markdown(f"""