Spaces:
Paused
Paused
Upload 2 files
Browse files
README.md
CHANGED
|
@@ -13,19 +13,20 @@ startup_duration_timeout: 1h
|
|
| 13 |
|
| 14 |
Docker Space for swapping a face from an uploaded image onto a target video.
|
| 15 |
|
| 16 |
-
## Setup
|
| 17 |
-
|
| 18 |
-
- Create a new Space with Docker SDK.
|
| 19 |
-
- Add the files from this repo.
|
| 20 |
-
- Set hardware to a paid GPU such as A10G small.
|
| 21 |
-
- Add `HF_TOKEN` if your model repo is gated or private.
|
| 22 |
-
|
| 23 |
## Environment variables
|
| 24 |
|
| 25 |
-
- `MODEL_REPO_ID`:
|
| 26 |
-
- `MODEL_FILENAME`:
|
| 27 |
- `MODEL_REVISION`: optional branch, tag, or commit hash
|
| 28 |
-
- `HF_TOKEN`:
|
| 29 |
-
- `FACE_MODEL_NAME`:
|
| 30 |
-
- `DETECTION_SIZE`:
|
| 31 |
- `MAX_FRAMES`: optional frame cap for testing, default `0`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
Docker Space for swapping a face from an uploaded image onto a target video.
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
## Environment variables
|
| 17 |
|
| 18 |
+
- `MODEL_REPO_ID`: default `ezioruan/inswapper_128.onnx`
|
| 19 |
+
- `MODEL_FILENAME`: default `inswapper_128.onnx`
|
| 20 |
- `MODEL_REVISION`: optional branch, tag, or commit hash
|
| 21 |
+
- `HF_TOKEN`: recommended for gated/private repos and better Hub rate limits
|
| 22 |
+
- `FACE_MODEL_NAME`: default `buffalo_l`
|
| 23 |
+
- `DETECTION_SIZE`: default `640`
|
| 24 |
- `MAX_FRAMES`: optional frame cap for testing, default `0`
|
| 25 |
+
- `FORCE_CPU`: set to `1` to force CPU mode for debugging
|
| 26 |
+
|
| 27 |
+
## Deploy
|
| 28 |
+
|
| 29 |
+
1. Create a new Space with Docker SDK.
|
| 30 |
+
2. Add the files from this repo.
|
| 31 |
+
3. Select a paid GPU hardware tier such as A10G small.
|
| 32 |
+
4. Add `HF_TOKEN` if you use a private or rate-limited model repo.
|
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from pathlib import Path
|
|
| 6 |
|
| 7 |
import cv2
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
from insightface.app import FaceAnalysis
|
| 11 |
from insightface.model_zoo import get_model
|
|
@@ -23,6 +24,7 @@ HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKE
|
|
| 23 |
FACE_MODEL_NAME = os.environ.get("FACE_MODEL_NAME", "buffalo_l")
|
| 24 |
DETECTION_SIZE = int(os.environ.get("DETECTION_SIZE", "640"))
|
| 25 |
MAX_FRAMES = int(os.environ.get("MAX_FRAMES", "0"))
|
|
|
|
| 26 |
|
| 27 |
face_analyzer = None
|
| 28 |
face_swapper = None
|
|
@@ -35,7 +37,16 @@ def ensure_ffmpeg():
|
|
| 35 |
|
| 36 |
|
| 37 |
def get_execution_providers():
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
def ensure_swap_model() -> str:
|
|
@@ -97,6 +108,12 @@ def swap_frame(frame_bgr, source_face, analyzer, swapper):
|
|
| 97 |
|
| 98 |
|
| 99 |
def process_video(face_image_path, video_path, progress=gr.Progress(track_tqdm=False)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
ensure_ffmpeg()
|
| 101 |
analyzer, swapper = load_models()
|
| 102 |
src = cv2.imread(face_image_path)
|
|
@@ -136,12 +153,18 @@ def process_video(face_image_path, video_path, progress=gr.Progress(track_tqdm=F
|
|
| 136 |
cap.release()
|
| 137 |
writer.release()
|
| 138 |
mux_audio(silent_video, audio_file, final_video)
|
| 139 |
-
return final_video, f"Done. Processed {idx} frame(s)."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
-
with gr.Blocks(
|
| 143 |
gr.Markdown("""# Video FaceSwap GPU
|
| 144 |
Upload a source face image and a target video. The app downloads the swap model from the Hugging Face Hub at runtime, then swaps the main source face onto detected faces in the video.""")
|
|
|
|
| 145 |
with gr.Row():
|
| 146 |
face_image = gr.Image(type="filepath", label="Source face image")
|
| 147 |
target_video = gr.Video(label="Target video")
|
|
@@ -151,4 +174,4 @@ Upload a source face image and a target video. The app downloads the swap model
|
|
| 151 |
run_btn.click(fn=process_video, inputs=[face_image, target_video], outputs=[output_video, status], api_name="faceswap_video")
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.queue(max_size=8).launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 6 |
|
| 7 |
import cv2
|
| 8 |
import gradio as gr
|
| 9 |
+
import onnxruntime as ort
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
from insightface.app import FaceAnalysis
|
| 12 |
from insightface.model_zoo import get_model
|
|
|
|
| 24 |
FACE_MODEL_NAME = os.environ.get("FACE_MODEL_NAME", "buffalo_l")
|
| 25 |
DETECTION_SIZE = int(os.environ.get("DETECTION_SIZE", "640"))
|
| 26 |
MAX_FRAMES = int(os.environ.get("MAX_FRAMES", "0"))
|
| 27 |
+
FORCE_CPU = os.environ.get("FORCE_CPU") == "1"
|
| 28 |
|
| 29 |
face_analyzer = None
|
| 30 |
face_swapper = None
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
def get_execution_providers():
|
| 40 |
+
providers = []
|
| 41 |
+
if not FORCE_CPU:
|
| 42 |
+
providers.append("CUDAExecutionProvider")
|
| 43 |
+
providers.append("CPUExecutionProvider")
|
| 44 |
+
return providers
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def provider_status():
|
| 48 |
+
available = ort.get_available_providers()
|
| 49 |
+
return available, ("CUDAExecutionProvider" in available)
|
| 50 |
|
| 51 |
|
| 52 |
def ensure_swap_model() -> str:
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
def process_video(face_image_path, video_path, progress=gr.Progress(track_tqdm=False)):
|
| 111 |
+
available_providers, cuda_available = provider_status()
|
| 112 |
+
if FORCE_CPU:
|
| 113 |
+
raise RuntimeError(f"FORCE_CPU=1 is set. Available providers: {available_providers}")
|
| 114 |
+
if not cuda_available:
|
| 115 |
+
raise RuntimeError(f"CUDAExecutionProvider is not available. Available providers: {available_providers}")
|
| 116 |
+
|
| 117 |
ensure_ffmpeg()
|
| 118 |
analyzer, swapper = load_models()
|
| 119 |
src = cv2.imread(face_image_path)
|
|
|
|
| 153 |
cap.release()
|
| 154 |
writer.release()
|
| 155 |
mux_audio(silent_video, audio_file, final_video)
|
| 156 |
+
return final_video, f"Done. Processed {idx} frame(s). Providers: {available_providers}"
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def startup_message():
|
| 160 |
+
available, cuda_ok = provider_status()
|
| 161 |
+
return f"ONNX Runtime providers: {available} | CUDA available: {cuda_ok}"
|
| 162 |
|
| 163 |
|
| 164 |
+
with gr.Blocks(title="Video FaceSwap GPU") as demo:
|
| 165 |
gr.Markdown("""# Video FaceSwap GPU
|
| 166 |
Upload a source face image and a target video. The app downloads the swap model from the Hugging Face Hub at runtime, then swaps the main source face onto detected faces in the video.""")
|
| 167 |
+
status_banner = gr.Textbox(label="Startup status", value=startup_message(), interactive=False)
|
| 168 |
with gr.Row():
|
| 169 |
face_image = gr.Image(type="filepath", label="Source face image")
|
| 170 |
target_video = gr.Video(label="Target video")
|
|
|
|
| 174 |
run_btn.click(fn=process_video, inputs=[face_image, target_video], outputs=[output_video, status], api_name="faceswap_video")
|
| 175 |
|
| 176 |
if __name__ == "__main__":
|
| 177 |
+
demo.queue(max_size=8).launch(server_name="0.0.0.0", server_port=7860, share=False)
|