Spaces:
Build error
Build error
File size: 16,830 Bytes
91a2973 15389cd 91a2973 ecb100d 91a2973 ecb100d 91a2973 ecb100d 91a2973 ecb100d 91a2973 ecb100d 91a2973 ecb100d 91a2973 15389cd 2296256 15389cd 2296256 15389cd 193fbf7 15389cd 2296256 15389cd 2296256 15389cd 193fbf7 15389cd 193fbf7 15389cd 193fbf7 15389cd 193fbf7 15389cd 2296256 15389cd 2296256 15389cd 2296256 15389cd 2296256 193fbf7 15389cd 193fbf7 15389cd 193fbf7 15389cd 2296256 15389cd 2296256 15389cd 193fbf7 15389cd 2296256 15389cd 2296256 15389cd 2296256 15389cd 2296256 15389cd 193fbf7 15389cd 193fbf7 15389cd 193fbf7 15389cd 2296256 15389cd 08168f4 15389cd fd64207 15389cd ecb100d 15389cd ecb100d 15389cd 5ab9dca 15389cd 5ab9dca 15389cd 5ab9dca 15389cd 5ab9dca 15389cd 5ab9dca 15389cd 5ab9dca 15389cd 5ab9dca 15389cd ecb100d 15389cd fd64207 91a2973 fd64207 15389cd | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | import numpy as np
import gradio as gr
import roop.globals
from roop.core import (
start,
decode_execution_providers,
suggest_max_memory,
suggest_execution_threads,
)
from roop.processors.frame.core import get_frame_processors_modules
from roop.utilities import normalize_output_path
import os
from PIL import Image
import time
def swap_face(source_file, target_file, doFaceEnhancer, video_quality):
source_path = "input.jpg"
target_path = "target.mp4"
# Save source image
source_image = Image.fromarray(source_file)
source_image.save(source_path)
# Copy the target video to the working directory
os.rename(target_file, target_path)
print("source_path: ", source_path)
print("target_path: ", target_path)
roop.globals.video_quality = video_quality
roop.globals.source_path = source_path
roop.globals.target_path = target_path
output_path = "output.mp4"
roop.globals.output_path = normalize_output_path(
roop.globals.source_path, roop.globals.target_path, output_path
)
if doFaceEnhancer:
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
else:
roop.globals.frame_processors = ["face_swapper"]
roop.globals.headless = True
roop.globals.keep_fps = True
roop.globals.keep_audio = True
roop.globals.keep_frames = False
roop.globals.many_faces = False
roop.globals.video_encoder = "libx264"
roop.globals.video_quality = 10
roop.globals.max_memory = suggest_max_memory()
roop.globals.execution_providers = decode_execution_providers(["cuda"])
roop.globals.execution_threads = suggest_execution_threads()
print(
"start process",
roop.globals.source_path,
roop.globals.target_path,
roop.globals.output_path,
)
for frame_processor in get_frame_processors_modules(
roop.globals.frame_processors
):
if not frame_processor.pre_check():
return
start()
return output_path
# Custom CSS for neon style with animated online indicator
custom_css = """
:root {
--neon-primary: #00f3ff;
--neon-secondary: #ff00ff;
--neon-accent: #00ff87;
--neon-warning: #ffcc00;
--dark-bg: #0a0a1a;
--dark-panel: #13132b;
--darker-panel: #0c0c1f;
--text-primary: #ffffff;
--text-secondary: #a0a0c0;
}
body {
background: var(--dark-bg) !important;
color: var(--text-primary) !important;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
}
.gr-block {
background: var(--dark-panel) !important;
border-radius: 12px !important;
border: 1px solid rgba(0, 243, 255, 0.2) !important;
box-shadow: 0 0 15px rgba(0, 243, 255, 0.1) !important;
}
.gr-box {
border-color: rgba(0, 243, 255, 0.3) !important;
color: var(--text-primary) !important;
background: rgba(10, 10, 26, 0.7) !important;
}
h1, h2, h3, h4, label, .gr-label {
color: var(--text-primary) !important;
text-shadow: 0 0 5px rgba(0, 243, 255, 0.5);
}
.gr-button {
background: linear-gradient(45deg, var(--neon-primary), var(--neon-secondary)) !important;
color: black !important;
border: none !important;
border-radius: 8px !important;
padding: 12px 28px !important;
font-weight: 600 !important;
text-transform: uppercase !important;
letter-spacing: 1px !important;
box-shadow: 0 0 10px var(--neon-primary), 0 0 20px rgba(0, 243, 255, 0.3) !important;
transition: all 0.3s ease !important;
width: 100% !important;
margin: 10px 0 !important;
}
.gr-button:not(:disabled):hover {
transform: translateY(-2px);
box-shadow: 0 0 15px var(--neon-primary), 0 0 30px rgba(0, 243, 255, 0.5) !important;
}
.gr-button:disabled {
background: #4b5563 !important;
box-shadow: none !important;
}
.status-indicator {
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
margin-right: 10px;
background-color: var(--neon-accent);
box-shadow: 0 0 0 0 rgba(0, 255, 135, 0.7);
animation: pulse 2s infinite;
}
@keyframes pulse {
0% {
box-shadow: 0 0 0 0 rgba(0, 255, 135, 0.7);
}
70% {
box-shadow: 0 0 0 10px rgba(0, 255, 135, 0);
}
100% {
box-shadow: 0 0 0 0 rgba(0, 255, 135, 0);
}
}
.status-text {
color: var(--text-secondary);
font-size: 0.9rem;
display: flex;
align-items: center;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 2rem;
padding: 1.5rem;
background: linear-gradient(90deg, rgba(0,243,255,0.1) 0%, rgba(255,0,255,0.1) 100%);
border-radius: 12px;
border: 1px solid rgba(0, 243, 255, 0.3);
box-shadow: 0 0 20px rgba(0, 243, 255, 0.2);
}
.title-section h1 {
margin-bottom: 0.25rem;
font-weight: 800;
background: linear-gradient(45deg, var(--neon-primary), var(--neon-secondary));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 0 0 10px rgba(0, 243, 255, 0.5);
}
.title-section p {
color: var(--text-secondary);
margin: 0;
}
.instructions {
background: linear-gradient(90deg, rgba(0,243,255,0.05) 0%, rgba(255,0,255,0.05) 100%) !important;
padding: 1.5rem !important;
margin-bottom: 2rem !important;
border: 1px solid rgba(0, 243, 255, 0.2) !important;
}
.instructions h3 {
margin-top: 0;
margin-bottom: 0.75rem;
color: var(--neon-primary) !important;
}
.instructions ul {
margin-bottom: 0;
padding-left: 1.5rem;
}
.instructions li {
margin-bottom: 0.5rem;
color: var(--text-secondary);
}
.instructions li:last-child {
margin-bottom: 0;
}
.footer {
text-align: center;
margin-top: 2rem;
padding-top: 1.5rem;
border-top: 1px solid rgba(0, 243, 255, 0.2);
color: var(--text-secondary);
font-size: 0.875rem;
}
.image-container {
border: 2px solid rgba(0, 243, 255, 0.3);
border-radius: 12px;
padding: 8px;
background: rgba(0, 0, 0, 0.2);
margin-bottom: 1.5rem;
box-shadow: 0 0 15px rgba(0, 243, 255, 0.1);
}
.image-container .gr-label {
background: rgba(0, 243, 255, 0.1);
padding: 8px 12px;
border-radius: 8px;
margin-bottom: 10px;
display: inline-block;
}
.control-panel {
background: linear-gradient(90deg, rgba(0,243,255,0.08) 0%, rgba(255,0,255,0.08) 100%) !important;
padding: 1.5rem !important;
border: 1px solid rgba(0, 243, 255, 0.3) !important;
border-radius: 12px !important;
margin-bottom: 1.5rem !important;
}
.status-panel {
background: var(--darker-panel) !important;
padding: 1.5rem !important;
border: 1px solid rgba(0, 255, 135, 0.3) !important;
border-radius: 12px !important;
box-shadow: 0 0 15px rgba(0, 255, 135, 0.1) !important;
}
.status-header {
display: flex;
align-items: center;
margin-bottom: 1rem;
padding-bottom: 0.5rem;
border-bottom: 1px solid rgba(0, 255, 135, 0.2);
}
.status-content {
min-height: 100px;
}
.output-highlight {
border: 2px solid var(--neon-accent) !important;
box-shadow: 0 0 20px rgba(0, 255, 135, 0.3) !important;
}
.upload-text {
color: var(--text-secondary);
text-align: center;
padding: 20px;
}
.progress-bar {
height: 6px;
background: rgba(0, 243, 255, 0.2);
border-radius: 3px;
margin: 10px 0;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, var(--neon-primary), var(--neon-accent));
border-radius: 3px;
width: 0%;
transition: width 0.3s ease;
}
.control-item {
margin-bottom: 1rem;
padding: 1rem;
background: rgba(0, 0, 0, 0.2);
border-radius: 8px;
border: 1px solid rgba(0, 243, 255, 0.1);
}
.control-item:last-child {
margin-bottom: 0;
}
.download-btn {
background: linear-gradient(45deg, var(--neon-accent), #00cc70) !important;
margin-top: 15px !important;
}
.video-container {
border: 2px solid rgba(0, 243, 255, 0.3);
border-radius: 12px;
padding: 8px;
background: rgba(0, 0, 0, 0.2);
margin-bottom: 1.5rem;
box-shadow: 0 0 15px rgba(0, 243, 255, 0.1);
}
"""
# Gradio UI with neon style theme
with gr.Blocks(
css=custom_css,
title="Neon Face Swap AI",
theme=gr.themes.Default(primary_hue="cyan", secondary_hue="pink")
) as demo:
# Header section with title and animated status indicator
with gr.Row(elem_classes="header"):
with gr.Column(scale=3):
with gr.Row(elem_classes="title-section"):
gr.Markdown("""
# π AI FACE SWAPPER
### Next-Generation AI Face Swapping Technology
""")
with gr.Column(scale=1):
with gr.Row():
gr.Markdown("""
<div class="status-text">
<div class="status-indicator"></div>
System Online
</div>
""")
# Main content - unique arrangement
with gr.Row():
# Left column - inputs
with gr.Column(scale=1, min_width=400):
gr.Markdown("### π€ SOURCE IMAGE")
with gr.Group(elem_classes="image-container"):
source_image = gr.Image(
label="Upload Source Face",
type="numpy",
height=250,
elem_classes="gr-box"
)
gr.Markdown("### π― TARGET VIDEO")
with gr.Group(elem_classes="video-container"):
target_video = gr.Video(
label="Upload Target Video",
height=250,
elem_classes="gr-box"
)
# Middle column - controls and status (separated)
with gr.Column(scale=1, min_width=350):
# PROCESSING CONTROLS - Separate Panel
with gr.Group(elem_classes="control-panel"):
gr.Markdown("### βοΈ PROCESSING CONTROLS")
with gr.Group(elem_classes="control-item"):
face_enhancer = gr.Checkbox(
label="Enable Face Enhancer",
value=True,
info="Improves face quality but increases processing time"
)
with gr.Group(elem_classes="control-item"):
gr.Markdown("**Video Quality**")
video_quality = gr.Slider(
minimum=0,
maximum=100,
step=1,
value=18,
label="Output Quality (0=Best, 100=Worst)",
info="Lower values give better quality but larger file size"
)
with gr.Group(elem_classes="control-item"):
submit = gr.Button(
"π START SWAP",
variant="primary"
)
# STATUS - Separate Panel
with gr.Group(elem_classes="status-panel"):
with gr.Column():
gr.Markdown("""
<div class="status-header">
<h4 style="margin: 0;">π SYSTEM STATUS</h4>
</div>
""")
with gr.Group(elem_classes="status-content"):
# Progress bar
gr.Markdown("""
<div class="progress-bar">
<div class="progress-fill" id="progress-fill"></div>
</div>
""")
# Status text
info_text = gr.Textbox(
label="Current Status",
value="π’ Ready to process video",
interactive=False,
lines=3
)
# Additional status info
with gr.Row():
gr.Markdown("**GPU:** β
Active")
gr.Markdown("**Memory:** π‘ Stable")
# Right column - output
with gr.Column(scale=1, min_width=400):
gr.Markdown("### π₯ OUTPUT RESULT")
with gr.Group(elem_classes="video-container output-highlight"):
output_video = gr.Video(
label="Swapped Result",
interactive=False,
height=450,
elem_classes="gr-box"
)
# Download button (will be shown after processing)
download_btn = gr.Button(
"πΎ DOWNLOAD RESULT",
visible=False,
elem_classes="download-btn"
)
# Add a file component for downloading
download_file = gr.File(
label="Download Result",
visible=False,
interactive=False
)
# Instructions at the bottom
with gr.Row():
with gr.Column():
gr.Markdown("""
<div class="instructions">
<h3>π HOW TO USE</h3>
<ul>
<li><strong>Source Face:</strong> Select an image containing the face you want to use</li>
<li><strong>Target Video:</strong> Select a video where you want to place the source face</li>
<li><strong>Face Enhancer:</strong> Enable for higher quality results (takes longer)</li>
<li><strong>Video Quality:</strong> Adjust quality vs file size trade-off</li>
<li>Click <strong>START SWAP</strong> to begin processing</li>
<li>Monitor progress in the <strong>SYSTEM STATUS</strong> panel</li>
<li>Download your result when processing is complete</li>
</ul>
</div>
""")
# Footer
gr.Markdown("""
<div class="footer">
<p>Powered by Roop β’ Face Swapper AI v2.0 β’ GPU Accelerated</p>
</div>
""")
# Function to return file path for download
def get_output_file(output_path):
if output_path and os.path.exists(output_path):
return output_path, gr.update(visible=True, value=output_path)
return None, gr.update(visible=False)
# Enhanced submit function with status updates
def process_swap(source_file, target_file, doFaceEnhancer, video_quality_val):
if source_file is None or target_file is None:
yield "β Please upload both source image and target video", None, gr.update(visible=False), None, gr.update(visible=False)
return
yield "π‘ Processing: Analyzing faces and detecting features...", None, gr.update(visible=False), None, gr.update(visible=False)
time.sleep(1)
yield "π‘ Processing: Extracting facial landmarks and matching features...", None, gr.update(visible=False), None, gr.update(visible=False)
time.sleep(1)
yield "π‘ Processing: Swapping faces and applying transformations...", None, gr.update(visible=False), None, gr.update(visible=False)
time.sleep(1)
if doFaceEnhancer:
yield "π‘ Processing: Enhancing face quality and refining details...", None, gr.update(visible=False), None, gr.update(visible=False)
time.sleep(1)
result = swap_face(source_file, target_file, doFaceEnhancer, video_quality_val)
if result is None:
yield "β Error: Failed to process video. Please try again with different files.", None, gr.update(visible=False), None, gr.update(visible=False)
else:
file_path, file_update = get_output_file(result)
yield "β
Processing complete! Result is ready for download.", result, gr.update(visible=True), file_path, file_update
# Connect the button with enhanced function
submit.click(
fn=process_swap,
inputs=[source_image, target_video, face_enhancer, video_quality],
outputs=[info_text, output_video, download_btn, download_file]
)
# Download button functionality - show the file download component
def show_download():
return gr.update(visible=True)
download_btn.click(
fn=show_download,
inputs=None,
outputs=download_file
)
if __name__ == "__main__":
demo.launch(share=False, server_name="0.0.0.0") |