Ii
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,11 @@ 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"
|
9 |
-
model_path = "/home/user/app/inswapper_128.onnx" #
|
10 |
|
11 |
# Function to download the model if not exists
|
12 |
def download_model():
|
@@ -44,8 +45,14 @@ def run(*vars):
|
|
44 |
'threshold': thresholds[k]
|
45 |
})
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Call refacer to process video and get refaced video path
|
48 |
-
|
49 |
print(f"Refaced video can be found at {refaced_video_path}")
|
50 |
|
51 |
# Convert the output video to memory buffer
|
@@ -85,5 +92,20 @@ with gr.Blocks() as demo:
|
|
85 |
# Click event: Refacing the video and showing the refaced video in Gradio
|
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)
|
|
|
3 |
from refacer import Refacer
|
4 |
import os
|
5 |
import requests
|
6 |
+
from huggingface_hub import HfApi
|
7 |
|
8 |
# Hugging Face URL to download the model
|
9 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
10 |
+
model_path = "/home/user/app/inswapper_128.onnx" # Absolute path for the model in your environment
|
11 |
|
12 |
# Function to download the model if not exists
|
13 |
def download_model():
|
|
|
45 |
'threshold': thresholds[k]
|
46 |
})
|
47 |
|
48 |
+
# Specify the output path for the refaced video
|
49 |
+
output_dir = "/home/user/app/out"
|
50 |
+
if not os.path.exists(output_dir):
|
51 |
+
os.makedirs(output_dir)
|
52 |
+
refaced_video_path = os.path.join(output_dir, "refaced_video.mp4")
|
53 |
+
|
54 |
# Call refacer to process video and get refaced video path
|
55 |
+
refacer.reface(video_path, faces, output_path=refaced_video_path)
|
56 |
print(f"Refaced video can be found at {refaced_video_path}")
|
57 |
|
58 |
# Convert the output video to memory buffer
|
|
|
92 |
# Click event: Refacing the video and showing the refaced video in Gradio
|
93 |
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
94 |
|
95 |
+
# Function to upload the refaced video to Hugging Face Spaces
|
96 |
+
def upload_to_hf(video_path):
|
97 |
+
api = HfApi()
|
98 |
+
repo_id = "your-username/your-space-name" # Replace with your actual repository name
|
99 |
+
api.upload_file(
|
100 |
+
path_or_fileobj=video_path,
|
101 |
+
path_in_repo="out/refaced_video.mp4",
|
102 |
+
repo_id=repo_id,
|
103 |
+
repo_type="space"
|
104 |
+
)
|
105 |
+
print("Refaced video uploaded to Hugging Face Spaces.")
|
106 |
+
|
107 |
+
# Call the upload function after refacing is complete
|
108 |
+
upload_to_hf(refaced_video_path)
|
109 |
+
|
110 |
# Launch the Gradio app
|
111 |
demo.queue().launch(show_error=True, server_name="0.0.0.0", server_port=7860)
|