Ii
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from refacer import Refacer
|
| 3 |
-
import argparse
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
-
import tempfile
|
| 7 |
-
import shutil
|
| 8 |
|
| 9 |
# Hugging Face URL to download the model
|
| 10 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
|
@@ -27,20 +25,8 @@ def download_model():
|
|
| 27 |
# Download the model when the script runs
|
| 28 |
download_model()
|
| 29 |
|
| 30 |
-
# Argument parser
|
| 31 |
-
parser = argparse.ArgumentParser(description='Refacer')
|
| 32 |
-
parser.add_argument("--max_num_faces", type=int, help="Max number of faces on UI", default=5)
|
| 33 |
-
parser.add_argument("--force_cpu", help="Force CPU mode", default=False, action="store_true")
|
| 34 |
-
parser.add_argument("--share_gradio", help="Share Gradio", default=False, action="store_true")
|
| 35 |
-
parser.add_argument("--server_name", type=str, help="Server IP address", default="127.0.0.1")
|
| 36 |
-
parser.add_argument("--server_port", type=int, help="Server port", default=7860)
|
| 37 |
-
parser.add_argument("--colab_performance", help="Use in colab for better performance", default=False, action="store_true")
|
| 38 |
-
args = parser.parse_args()
|
| 39 |
-
|
| 40 |
# Initialize the Refacer class
|
| 41 |
-
refacer = Refacer(force_cpu=
|
| 42 |
-
|
| 43 |
-
num_faces = args.max_num_faces
|
| 44 |
|
| 45 |
# Run function for refacing video
|
| 46 |
def run(*vars):
|
|
@@ -61,11 +47,19 @@ def run(*vars):
|
|
| 61 |
# Call refacer to process video and get refaced video path
|
| 62 |
refaced_video_path = refacer.reface(video_path, faces) # Get refaced video path
|
| 63 |
print(f"Refaced video can be found at {refaced_video_path}")
|
| 64 |
-
|
| 65 |
-
#
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Prepare Gradio components
|
|
|
|
| 69 |
origin = []
|
| 70 |
destination = []
|
| 71 |
thresholds = []
|
|
@@ -92,4 +86,4 @@ with gr.Blocks() as demo:
|
|
| 92 |
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
| 93 |
|
| 94 |
# Launch the Gradio app
|
| 95 |
-
demo.queue().launch(show_error=True,
|
|
|
|
| 1 |
+
import io
|
| 2 |
import gradio as gr
|
| 3 |
from refacer import Refacer
|
|
|
|
| 4 |
import os
|
| 5 |
import requests
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Hugging Face URL to download the model
|
| 8 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
|
|
|
| 25 |
# Download the model when the script runs
|
| 26 |
download_model()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# Initialize the Refacer class
|
| 29 |
+
refacer = Refacer(force_cpu=False, colab_performance=False)
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Run function for refacing video
|
| 32 |
def run(*vars):
|
|
|
|
| 47 |
# Call refacer to process video and get refaced video path
|
| 48 |
refaced_video_path = refacer.reface(video_path, faces) # Get refaced video path
|
| 49 |
print(f"Refaced video can be found at {refaced_video_path}")
|
| 50 |
+
|
| 51 |
+
# Convert the output video to memory buffer
|
| 52 |
+
video_buffer = io.BytesIO()
|
| 53 |
+
with open(refaced_video_path, "rb") as f:
|
| 54 |
+
video_buffer.write(f.read())
|
| 55 |
+
|
| 56 |
+
# Rewind the buffer to the beginning
|
| 57 |
+
video_buffer.seek(0)
|
| 58 |
+
|
| 59 |
+
return video_buffer # Gradio will handle the video display
|
| 60 |
|
| 61 |
# Prepare Gradio components
|
| 62 |
+
num_faces = 5
|
| 63 |
origin = []
|
| 64 |
destination = []
|
| 65 |
thresholds = []
|
|
|
|
| 86 |
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
| 87 |
|
| 88 |
# Launch the Gradio app
|
| 89 |
+
demo.queue().launch(show_error=True, server_name="0.0.0.0", server_port=7860)
|