Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,134 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import roop.globals
|
| 3 |
-
import roop.core
|
| 4 |
-
import roop.processors.frame.core
|
| 5 |
-
import os
|
| 6 |
-
import shutil
|
| 7 |
-
|
| 8 |
-
# Ensure roop thinks it's headless so it doesn't try to launch its own GUI
|
| 9 |
-
roop.globals.headless = True
|
| 10 |
-
roop.globals.execution_providers = ['cpu'] # Default to CPU, can be changed
|
| 11 |
-
roop.globals.execution_threads = 1
|
| 12 |
-
roop.globals.frame_processors = ['face_swapper']
|
| 13 |
-
roop.globals.keep_fps = True
|
| 14 |
-
roop.globals.keep_frames = False
|
| 15 |
-
roop.globals.skip_audio = False
|
| 16 |
-
roop.globals.many_faces = False
|
| 17 |
-
roop.globals.reference_face_position = 0
|
| 18 |
-
roop.globals.reference_frame_number = 0
|
| 19 |
-
roop.globals.similar_face_distance = 0.85
|
| 20 |
-
roop.globals.temp_frame_format = 'png'
|
| 21 |
-
roop.globals.temp_frame_quality = 100
|
| 22 |
-
roop.globals.output_video_encoder = 'libx264'
|
| 23 |
-
roop.globals.output_video_quality = 35
|
| 24 |
-
roop.globals.max_memory = None
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def swap_face(source_file, target_file, enable_enhancer):
|
| 28 |
-
if source_file is None or target_file is None:
|
| 29 |
-
return None, None
|
| 30 |
-
|
| 31 |
-
source_path = source_file if isinstance(source_file, str) else source_file.name
|
| 32 |
-
target_path = target_file if isinstance(target_file, str) else target_file.name
|
| 33 |
-
|
| 34 |
-
# Determine output path based on target extension
|
| 35 |
-
target_name, target_ext = os.path.splitext(os.path.basename(target_path))
|
| 36 |
-
output_ext = target_ext
|
| 37 |
-
is_video_output = False
|
| 38 |
-
|
| 39 |
-
if target_ext.lower() in ['.jpg', '.jpeg', '.png', '.bmp', '.webp']:
|
| 40 |
-
output_ext = '.png'
|
| 41 |
-
is_video_output = False
|
| 42 |
-
elif target_ext.lower() in ['.mp4', '.avi', '.mov', '.mkv']:
|
| 43 |
-
output_ext = '.mp4'
|
| 44 |
-
is_video_output = True
|
| 45 |
-
|
| 46 |
-
output_path = os.path.join(os.getcwd(), f"output_{target_name}{output_ext}")
|
| 47 |
-
|
| 48 |
-
print(f"Source: {source_path}")
|
| 49 |
-
print(f"Target: {target_path}")
|
| 50 |
-
print(f"Output: {output_path}")
|
| 51 |
-
print(f"Enhancer Enabled: {enable_enhancer}")
|
| 52 |
-
|
| 53 |
-
# Set globals
|
| 54 |
-
roop.globals.source_path = source_path
|
| 55 |
-
roop.globals.target_path = target_path
|
| 56 |
-
roop.globals.output_path = output_path
|
| 57 |
-
roop.globals.execution_providers = ['cpu']
|
| 58 |
-
|
| 59 |
-
# Configure processors
|
| 60 |
-
processors = ['face_swapper']
|
| 61 |
-
if enable_enhancer:
|
| 62 |
-
processors.append('face_enhancer')
|
| 63 |
-
roop.globals.frame_processors = processors
|
| 64 |
-
|
| 65 |
-
# CRITICAL FIX: Clear the cache of frame processors so it reloads based on the new list
|
| 66 |
-
# invalidating the previous run's selection (e.g. if enhancer was disabled previously)
|
| 67 |
-
roop.processors.frame.core.FRAME_PROCESSORS_MODULES = []
|
| 68 |
-
|
| 69 |
-
# Use correct provider name to avoid ONNX Runtime warnings
|
| 70 |
-
roop.globals.execution_providers = ['CPUExecutionProvider']
|
| 71 |
-
|
| 72 |
-
try:
|
| 73 |
-
roop.core.start()
|
| 74 |
-
if os.path.exists(output_path):
|
| 75 |
-
if is_video_output:
|
| 76 |
-
return None, output_path
|
| 77 |
-
else:
|
| 78 |
-
return output_path, None
|
| 79 |
-
else:
|
| 80 |
-
print("Error: Output file not created.")
|
| 81 |
-
return None, None
|
| 82 |
-
except Exception as e:
|
| 83 |
-
print(f"Error during processing: {e}")
|
| 84 |
-
return None, None
|
| 85 |
-
|
| 86 |
-
# Define the interface
|
| 87 |
-
with gr.Blocks(title="Roop Web UI") as demo:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import roop.globals
|
| 3 |
+
import roop.core
|
| 4 |
+
import roop.processors.frame.core
|
| 5 |
+
import os
|
| 6 |
+
import shutil
|
| 7 |
+
|
| 8 |
+
# Ensure roop thinks it's headless so it doesn't try to launch its own GUI
|
| 9 |
+
roop.globals.headless = True
|
| 10 |
+
roop.globals.execution_providers = ['cpu'] # Default to CPU, can be changed
|
| 11 |
+
roop.globals.execution_threads = 1
|
| 12 |
+
roop.globals.frame_processors = ['face_swapper']
|
| 13 |
+
roop.globals.keep_fps = True
|
| 14 |
+
roop.globals.keep_frames = False
|
| 15 |
+
roop.globals.skip_audio = False
|
| 16 |
+
roop.globals.many_faces = False
|
| 17 |
+
roop.globals.reference_face_position = 0
|
| 18 |
+
roop.globals.reference_frame_number = 0
|
| 19 |
+
roop.globals.similar_face_distance = 0.85
|
| 20 |
+
roop.globals.temp_frame_format = 'png'
|
| 21 |
+
roop.globals.temp_frame_quality = 100
|
| 22 |
+
roop.globals.output_video_encoder = 'libx264'
|
| 23 |
+
roop.globals.output_video_quality = 35
|
| 24 |
+
roop.globals.max_memory = None
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def swap_face(source_file, target_file, enable_enhancer):
|
| 28 |
+
if source_file is None or target_file is None:
|
| 29 |
+
return None, None
|
| 30 |
+
|
| 31 |
+
source_path = source_file if isinstance(source_file, str) else source_file.name
|
| 32 |
+
target_path = target_file if isinstance(target_file, str) else target_file.name
|
| 33 |
+
|
| 34 |
+
# Determine output path based on target extension
|
| 35 |
+
target_name, target_ext = os.path.splitext(os.path.basename(target_path))
|
| 36 |
+
output_ext = target_ext
|
| 37 |
+
is_video_output = False
|
| 38 |
+
|
| 39 |
+
if target_ext.lower() in ['.jpg', '.jpeg', '.png', '.bmp', '.webp']:
|
| 40 |
+
output_ext = '.png'
|
| 41 |
+
is_video_output = False
|
| 42 |
+
elif target_ext.lower() in ['.mp4', '.avi', '.mov', '.mkv']:
|
| 43 |
+
output_ext = '.mp4'
|
| 44 |
+
is_video_output = True
|
| 45 |
+
|
| 46 |
+
output_path = os.path.join(os.getcwd(), f"output_{target_name}{output_ext}")
|
| 47 |
+
|
| 48 |
+
print(f"Source: {source_path}")
|
| 49 |
+
print(f"Target: {target_path}")
|
| 50 |
+
print(f"Output: {output_path}")
|
| 51 |
+
print(f"Enhancer Enabled: {enable_enhancer}")
|
| 52 |
+
|
| 53 |
+
# Set globals
|
| 54 |
+
roop.globals.source_path = source_path
|
| 55 |
+
roop.globals.target_path = target_path
|
| 56 |
+
roop.globals.output_path = output_path
|
| 57 |
+
roop.globals.execution_providers = ['cpu']
|
| 58 |
+
|
| 59 |
+
# Configure processors
|
| 60 |
+
processors = ['face_swapper']
|
| 61 |
+
if enable_enhancer:
|
| 62 |
+
processors.append('face_enhancer')
|
| 63 |
+
roop.globals.frame_processors = processors
|
| 64 |
+
|
| 65 |
+
# CRITICAL FIX: Clear the cache of frame processors so it reloads based on the new list
|
| 66 |
+
# invalidating the previous run's selection (e.g. if enhancer was disabled previously)
|
| 67 |
+
roop.processors.frame.core.FRAME_PROCESSORS_MODULES = []
|
| 68 |
+
|
| 69 |
+
# Use correct provider name to avoid ONNX Runtime warnings
|
| 70 |
+
roop.globals.execution_providers = ['CPUExecutionProvider']
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
roop.core.start()
|
| 74 |
+
if os.path.exists(output_path):
|
| 75 |
+
if is_video_output:
|
| 76 |
+
return None, output_path
|
| 77 |
+
else:
|
| 78 |
+
return output_path, None
|
| 79 |
+
else:
|
| 80 |
+
print("Error: Output file not created.")
|
| 81 |
+
return None, None
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error during processing: {e}")
|
| 84 |
+
return None, None
|
| 85 |
+
|
| 86 |
+
# Define the interface
|
| 87 |
+
with gr.Blocks(title="Roop Web UI") as demo:
|
| 88 |
+
|
| 89 |
+
# Custom Banner for AI Guruji
|
| 90 |
+
gr.HTML("""
|
| 91 |
+
<div style="background-color: #0b0f19; padding: 20px; text-align: center; border-radius: 10px; margin-bottom: 20px;">
|
| 92 |
+
<img src="https://aiguruji97-facefusion-by-ai-guruji.hf.space/gradio_api/file=/tmp/gradio/0c458ca1af97a9ebe14f501ce21d2ace96f2de6fba733486558e7639f8497832/logo.png" alt="AI Guruji Logo" style="width: 120px; height: 120px; border-radius: 50%; display: block; margin: 0 auto;">
|
| 93 |
+
<h1 style="color: white; font-family: sans-serif; margin-top: 10px; font-size: 28px; font-weight: bold;">Al Guruji</h1>
|
| 94 |
+
<div style="margin-top: 15px; display: flex; justify-content: center; gap: 10px;">
|
| 95 |
+
<a href="https://www.youtube.com/@AI_Guruji_97" target="_blank" style="background-color: #ff0000; color: white; padding: 8px 20px; border-radius: 20px; text-decoration: none; font-weight: bold; font-family: sans-serif;">YouTube</a>
|
| 96 |
+
<a href="https://buymeacoffee.com/bdas7906" target="_blank" style="background-color: #FFDD00; color: black; padding: 8px 20px; border-radius: 20px; text-decoration: none; font-weight: bold; font-family: sans-serif;">Buy me a coffee</a>
|
| 97 |
+
<a href="https://www.instagram.com/aiguruji97/" target="_blank" style="background-color: #E1306C; color: white; padding: 8px 20px; border-radius: 20px; text-decoration: none; font-weight: bold; font-family: sans-serif;">Instagram</a>
|
| 98 |
+
</div>
|
| 99 |
+
<div style="margin-top: 20px; color: white; font-family: sans-serif; font-size: 14px;">
|
| 100 |
+
<p>If multiple users are using this space (3-4 users), generation might be slow.</p>
|
| 101 |
+
<p><strong>Duplicate this Space</strong> for faster generation without waiting!</p>
|
| 102 |
+
<p style="margin-top: 10px; font-size: 16px;">❤️ <strong>Please Like this Space if you find it useful!</strong> ❤️</p>
|
| 103 |
+
<div style="display: flex; justify-content: center; align-items: center; margin-top: 10px;">
|
| 104 |
+
<a href="https://huggingface.co/spaces/huggingface/facefusion?duplicate=true" target="_blank" style="display: inline-flex; align-items: center; background-color: white; color: black; padding: 5px 15px; border-radius: 6px; text-decoration: none; font-weight: bold; border: 1px solid #ccc;">
|
| 105 |
+
<img src="https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-md-dark.svg" alt="Duplicate this Space" style="height: 20px; margin-right: 5px;">
|
| 106 |
+
Duplicate Space
|
| 107 |
+
</a>
|
| 108 |
+
</div>
|
| 109 |
+
</div>
|
| 110 |
+
</div>
|
| 111 |
+
""")
|
| 112 |
+
|
| 113 |
+
# gr.Markdown("# Roop Face Swap Web Interface")
|
| 114 |
+
gr.Markdown("### Upload a source face and a target image/video to swap faces.")
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column():
|
| 118 |
+
source_input = gr.Image(type="filepath", label="Source Face (Image)")
|
| 119 |
+
target_input = gr.File(label="Target (Image or Video)", file_types=["image", "video"])
|
| 120 |
+
enable_enhancer = gr.Checkbox(label="Enable Face Enhancer (GFPGAN)", value=False)
|
| 121 |
+
submit_btn = gr.Button("Swap Face", variant="primary")
|
| 122 |
+
|
| 123 |
+
with gr.Column():
|
| 124 |
+
result_image = gr.Image(label="Result Image", interactive=False)
|
| 125 |
+
result_video = gr.Video(label="Result Video", interactive=False, format="mp4")
|
| 126 |
+
|
| 127 |
+
submit_btn.click(
|
| 128 |
+
fn=swap_face,
|
| 129 |
+
inputs=[source_input, target_input, enable_enhancer],
|
| 130 |
+
outputs=[result_image, result_video]
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
if __name__ == "__main__":
|
| 134 |
+
demo.launch(inbrowser=True)
|