Alissonerdx's picture
update
0e18837
import gradio as gr
import spaces
import torch
from diffusers import DiffusionPipeline
from diffusers.utils import load_image
from PIL import Image
import numpy as np
from typing import Optional, Tuple, List
# Model configurations
BASE_MODEL = "Qwen/Qwen-Image-Edit-2509" # Qwen Image Edit model
BFS_LORA = "Alissonerdx/BFS-Best-Face-Swap"
BFS_LORA_FILENAME = "bfs_head_v3_qwen_image_edit_2509.safetensors" # Qwen-specific version
ANGLES_LORA = "dx8152/Qwen-Edit-2509-Multiple-angles"
SKIN_LORA = "tlennon-ie/qwen-edit-skin"
# Fixed prompt for head swap
FIXED_PROMPT = """head_swap: start with Picture 1 as the base image, keeping its lighting, environment, and background. remove the head from Picture 1 completely and replace it with the head from Picture 2. ensure the head and body have correct anatomical proportions, and blend the skin tones, shadows, and lighting naturally so the final result appears as one coherent, realistic person."""
DEFAULT_NEGATIVE_PROMPT = "bad quality, worst quality, low resolution, blur, distortion, unnatural blending, cartoon, illustration, painting"
# Cache for loaded pipe
pipe_cache = None
# FunΓ§Γ£o auxiliar para redimensionar mantendo aspect ratio
def smart_resize(image, target_long_edge=1024):
width, height = image.size
# Calcular nova proporΓ§Γ£o mantendo o aspect ratio
if width > height:
new_width = target_long_edge
new_height = int(height * (target_long_edge / width))
else:
new_height = target_long_edge
new_width = int(width * (target_long_edge / height))
# Arredondar para mΓΊltiplos de 32 (necessΓ‘rio para o modelo)
new_width = (new_width // 32) * 32
new_height = (new_height // 32) * 32
# Redimensionar usando LANCZOS para alta qualidade
resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
return resized_image, new_width, new_height
@spaces.GPU(duration=300)
def face_swap(
body_image,
face_image,
custom_prompt_addon,
bfs_lora_scale,
angles_lora_scale,
skin_lora_scale,
enable_angles_lora,
enable_skin_lora,
num_inference_steps,
guidance_scale,
seed
):
"""
Perform head swap using Qwen-Image-Edit with multiple LoRAs
"""
# Validate inputs
if body_image is None or face_image is None:
raise gr.Error("Please provide both body (Picture 1) and face (Picture 2) images")
# Set seed for reproducibility
if seed != -1:
torch.manual_seed(seed)
generator = torch.Generator(device="cuda").manual_seed(seed)
else:
generator = None
try:
global pipe_cache
# Load the pipeline (only once)
if pipe_cache is None:
print(f"Loading pipeline: {BASE_MODEL}")
pipe_cache = DiffusionPipeline.from_pretrained(
BASE_MODEL,
torch_dtype=torch.bfloat16, # Qwen uses bfloat16
device_map="cuda"
)
pipe = pipe_cache
# Prepare the LoRA adapters list
adapters = []
adapter_weights = []
# Unload existing adapters to start fresh for this request
try:
pipe.unload_lora_weights()
except:
pass
# Always load BFS Face Swap LoRA (Qwen-specific version)
if bfs_lora_scale > 0:
print(f"Loading BFS Face Swap LoRA (Qwen version) with scale {bfs_lora_scale}")
try:
pipe.load_lora_weights(
BFS_LORA,
weight_name=BFS_LORA_FILENAME,
adapter_name="bfs_face_swap"
)
adapters.append("bfs_face_swap")
adapter_weights.append(bfs_lora_scale)
except Exception as e:
print(f"Warning: Could not load BFS LoRA: {e}")
gr.Warning(f"BFS Face Swap LoRA could not be loaded: {e}")
# Load Multiple Angles LoRA if enabled
if enable_angles_lora and angles_lora_scale > 0:
print(f"Loading Multiple Angles LoRA with scale {angles_lora_scale}")
try:
pipe.load_lora_weights(
ANGLES_LORA,
adapter_name="angles"
)
adapters.append("angles")
adapter_weights.append(angles_lora_scale)
except Exception as e:
print(f"Warning: Could not load Angles LoRA: {e}")
gr.Warning(f"Multiple Angles LoRA could not be loaded: {e}")
# Load Skin LoRA if enabled
if enable_skin_lora and skin_lora_scale > 0:
print(f"Loading Skin LoRA with scale {skin_lora_scale}")
try:
pipe.load_lora_weights(
SKIN_LORA,
adapter_name="skin"
)
adapters.append("skin")
adapter_weights.append(skin_lora_scale)
except Exception as e:
print(f"Warning: Could not load Skin LoRA: {e}")
gr.Warning(f"Skin LoRA could not be loaded: {e}")
# Set the active adapters
if len(adapters) > 0:
if len(adapters) == 1:
pipe.set_adapters(adapters[0], adapter_weights=adapter_weights[0])
else:
pipe.set_adapters(adapters, adapter_weights=adapter_weights)
print(f"Active LoRAs: {adapters} with weights {adapter_weights}")
# Prepare images
body_img_pil = Image.fromarray(body_image).convert("RGB")
face_img_pil = Image.fromarray(face_image).convert("RGB")
# --- LΓ“GICA DE REDIMENSIONAMENTO INTELIGENTE ---
# Define o tamanho alvo baseado no maior lado (1024 Γ© um bom equilΓ­brio, pode subir para 1280)
# Isso corrige a distorΓ§Γ£o mantendo o aspect ratio correto
TARGET_RESOLUTION = 1024
body_resized, target_w, target_h = smart_resize(body_img_pil, target_long_edge=TARGET_RESOLUTION)
# Opcional: redimensionar a face para nΓ£o ficar gigante ou minΓΊscula comparada ao corpo
face_resized, _, _ = smart_resize(face_img_pil, target_long_edge=TARGET_RESOLUTION)
print(f"Original size: {body_img_pil.size} | Generation Target: {target_w}x{target_h}")
# Combine fixed prompt with any additional instructions
final_prompt = FIXED_PROMPT
if custom_prompt_addon and custom_prompt_addon.strip():
final_prompt = f"{FIXED_PROMPT} {custom_prompt_addon}"
print(f"Using prompt: {final_prompt[:100]}...")
# Qwen Image Edit uses a list for inputs: [body, face]
input_images_list = [body_resized, face_resized]
# Generate the head swap
result = pipe(
image=input_images_list,
prompt=final_prompt,
negative_prompt=DEFAULT_NEGATIVE_PROMPT,
true_cfg_scale=guidance_scale,
height=target_h, # FORÇA A ALTURA CORRETA
width=target_w, # FORÇA A LARGURA CORRETA
num_inference_steps=num_inference_steps,
generator=generator
).images[0]
# Create status message
active_loras = []
if bfs_lora_scale > 0:
active_loras.append(f"BFS-Qwen-v3({bfs_lora_scale:.2f})")
if enable_angles_lora and angles_lora_scale > 0:
active_loras.append(f"Angles({angles_lora_scale:.2f})")
if enable_skin_lora and skin_lora_scale > 0:
active_loras.append(f"Skin({skin_lora_scale:.2f})")
status = f"βœ… Head swap completed ({target_w}x{target_h}) | Active LoRAs: {', '.join(active_loras) if active_loras else 'None'}"
return result, status
except Exception as e:
print(f"Error: {str(e)}")
error_img = Image.new('RGB', (512, 512), color=(200, 50, 50))
return error_img, f"❌ Error: {str(e)}"
# Create the Gradio interface
with gr.Blocks(title="BFS-Best Face Swap with Qwen", theme=gr.themes.Soft(), css="""
.container {max-width: 1200px; margin: auto;}
.image-container {border-radius: 10px; border: 2px dashed #ccc;}
.fixed-prompt {background-color: #000000; padding: 10px; border-radius: 5px; font-family: monospace; color: #00ff00;}
.lora-info {background-color: #000000; padding: 8px; border-radius: 5px; margin: 5px 0; font-size: 0.9em; color: white;}
.footer-link {text-decoration: none !important; color: #5865F2 !important; font-weight: bold;}
.footer-link:hover {text-decoration: underline !important;}
""") as demo:
gr.Markdown(
"""
# 🎭 BFS - Best Face Swap with Qwen-Image-Edit-2509
This interface uses:
- **Base Model**: Qwen-Image-Edit-2509
- **Primary LoRA**: BFS-Best Face Swap v3 (Qwen-optimized: `bfs_head_v3_qwen_image_edit_2509.safetensors`)
- **Enhancement LoRAs**: Multiple Angles & Skin Blending
"""
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### πŸ“₯ Input Images")
with gr.Row():
body_image = gr.Image(
label="πŸ‘€ Picture 1: Body/Base Image",
type="numpy",
height=300,
elem_classes="image-container"
)
face_image = gr.Image(
label="😊 Picture 2: Head/Face to Swap",
type="numpy",
height=300,
elem_classes="image-container"
)
gr.Markdown("### 🎯 Fixed Head Swap Prompt")
gr.Markdown(
f'<div class="fixed-prompt">{FIXED_PROMPT}</div>',
elem_classes="fixed-prompt"
)
custom_prompt_addon = gr.Textbox(
label="Additional Instructions (Optional)",
placeholder="Add any extra details or style instructions...",
value="",
lines=2
)
with gr.Accordion("πŸŽ›οΈ LoRA Controls", open=True):
gr.Markdown("#### BFS Face Swap LoRA (Primary)")
gr.Markdown(
'<div class="lora-info">πŸ“Œ Using: bfs_head_v3_qwen_image_edit_2509.safetensors</div>',
elem_classes="lora-info"
)
bfs_lora_scale = gr.Slider(
minimum=0.0,
maximum=1.5,
step=0.05,
value=1.0,
label="BFS Face Swap Strength (Qwen v3)",
info="Main face swapping LoRA optimized for Qwen - set to 0 to disable"
)
gr.Markdown("#### Enhancement LoRAs")
with gr.Row():
enable_angles_lora = gr.Checkbox(
label="Enable Multiple Angles LoRA",
value=True,
info="Improves head angle matching"
)
angles_lora_scale = gr.Slider(
minimum=0.0,
maximum=1.5,
step=0.05,
value=0.7,
label="Multiple Angles Strength",
interactive=True
)
with gr.Row():
enable_skin_lora = gr.Checkbox(
label="Enable Skin Blending LoRA",
value=True,
info="Improves skin tone matching"
)
skin_lora_scale = gr.Slider(
minimum=0.0,
maximum=1.5,
step=0.05,
value=0.6,
label="Skin Blending Strength",
interactive=True
)
with gr.Accordion("βš™οΈ Generation Settings", open=False):
num_inference_steps = gr.Slider(
minimum=10,
maximum=100,
step=5,
value=30,
label="Inference Steps",
info="Higher = better quality but slower"
)
guidance_scale = gr.Slider(
minimum=1.0,
maximum=20.0,
step=0.5,
value=5.0,
label="Guidance Scale (CFG)",
info="How closely to follow the prompt"
)
seed = gr.Number(
value=-1,
label="Seed",
info="Use -1 for random, or specific number for reproducible results",
precision=0
)
generate_btn = gr.Button("🎨 Generate Head Swap", variant="primary", size="lg")
with gr.Column(scale=1):
gr.Markdown("### πŸ“€ Output")
output_image = gr.Image(
label="Result",
type="pil",
interactive=False,
height=500
)
status_text = gr.Textbox(
label="Status",
interactive=False,
max_lines=2,
value="Ready to process..."
)
gr.Markdown(
"""
### πŸ’‘ Quick Tips:
- **Picture 1**: Body/environment to keep
- **Picture 2**: Face/head to transplant
- **BFS Strength**: 0.8-1.2 for best results
- **Angles LoRA**: Helps with different head angles
- **Skin LoRA**: Smooths skin tone transitions
"""
)
# Interaction logic for enabling/disabling LoRA controls
def toggle_angles(enabled):
return gr.update(interactive=enabled)
def toggle_skin(enabled):
return gr.update(interactive=enabled)
enable_angles_lora.change(
fn=toggle_angles,
inputs=enable_angles_lora,
outputs=angles_lora_scale
)
enable_skin_lora.change(
fn=toggle_skin,
inputs=enable_skin_lora,
outputs=skin_lora_scale
)
# Event handlers
generate_btn.click(
fn=face_swap,
inputs=[
body_image,
face_image,
custom_prompt_addon,
bfs_lora_scale,
angles_lora_scale,
skin_lora_scale,
enable_angles_lora,
enable_skin_lora,
num_inference_steps,
guidance_scale,
seed
],
outputs=[output_image, status_text]
)
gr.Markdown(
"""
---
### πŸ“š Documentation
**Model Chain:**
1. **Qwen-Image-Edit-2509**: Advanced image editing base model
2. **BFS-Best Face Swap v3**: Primary face swapping LoRA
3. **Multiple Angles**: Improves head angle matching
4. **Skin Blending**: Natural skin tone transitions
**LoRA Settings Guide:**
- **All at 0**: Uses only base Qwen model
- **BFS only (1.0)**: Basic face swap
- **BFS + Angles**: Better angle matching
- **BFS + Skin**: Better skin blending
- **All enabled**: Maximum quality (slower)
### πŸ”— Resources:
- [Qwen-Image-Edit-2509](https://huggingface.co/Qwen/Qwen-Image-Edit-2509)
- [BFS-Best Face Swap](https://huggingface.co/Alissonerdx/BFS-Best-Face-Swap)
- [Multiple Angles LoRA](https://huggingface.co/dx8152/Qwen-Edit-2509-Multiple-angles)
- [Skin Blending LoRA](https://huggingface.co/tlennon-ie/qwen-edit-skin)
"""
)
gr.HTML(
"""
<div style="text-align: center; margin-top: 40px; padding: 20px; border-top: 1px solid #ccc;">
<a href="https://buymeacoffee.com/nrdx" target="_blank" style="display: inline-block; margin-bottom: 10px;">
<img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=nrdx&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff" alt="Buy Me A Coffee" height="40">
</a>
<div style="display: flex; justify-content: center; gap: 20px; margin-top: 10px; font-weight: bold;">
<a href="https://discord.gg/uYu3KzJcKB" target="_blank" class="footer-link">
πŸ‡§πŸ‡· Discord Toca da IA
</a>
<span>|</span>
<a href="https://discord.gg/ThrfwKcr3F" target="_blank" class="footer-link">
πŸ‡§πŸ‡· Discord Hoje na IA
</a>
</div>
</div>
"""
)
# Launch the app
if __name__ == "__main__":
demo.queue(max_size=10)
demo.launch()