Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
|
|
|
| 3 |
import os
|
| 4 |
from datetime import datetime
|
| 5 |
-
from huggingface_hub import login, upload_file
|
| 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 |
-
for tgt in target_files:
|
| 35 |
-
tgt_path = tgt.name if hasattr(tgt, "name") else str(tgt)
|
| 36 |
-
tgt_ext = os.path.splitext(tgt_path)[-1]
|
| 37 |
-
ts = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 38 |
-
out_path = f"output_{ts}{tgt_ext}"
|
| 39 |
-
|
| 40 |
-
cmd = [
|
| 41 |
-
"python3", "run.py",
|
| 42 |
-
"-s", source_file.name if hasattr(source_file, "name") else str(source_file),
|
| 43 |
-
"-t", tgt_path,
|
| 44 |
-
"-o", out_path,
|
| 45 |
-
"--frame-processor", "face_swapper", "face_enhancer",
|
| 46 |
-
"--many-faces"
|
| 47 |
-
]
|
| 48 |
-
|
| 49 |
-
try:
|
| 50 |
-
res = subprocess.run(cmd, capture_output=True, text=True)
|
| 51 |
-
if res.returncode != 0:
|
| 52 |
-
logs += f"❌ Lỗi xử lý {tgt_path}:\n{res.stderr}\n"
|
| 53 |
-
continue
|
| 54 |
-
output_files.append(out_path)
|
| 55 |
-
url = upload_to_huggingface(out_path)
|
| 56 |
-
logs += f"✅ {out_path} → [Uploaded]({url})\n"
|
| 57 |
-
except Exception as e:
|
| 58 |
-
logs += f"❌ Exception khi xử lý {tgt_path}: {str(e)}\n"
|
| 59 |
-
|
| 60 |
-
return output_files, logs
|
| 61 |
-
|
| 62 |
-
# --- UI Gradio v4.44.1 ---
|
| 63 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 64 |
-
demo.title = "Face Swapper Tool"
|
| 65 |
-
|
| 66 |
-
gr.Markdown("# 🤖 Face Swapper")
|
| 67 |
-
gr.Markdown("Upload ảnh/video cần swap và ảnh khuôn mặt để ghép")
|
| 68 |
-
|
| 69 |
-
with gr.Row():
|
| 70 |
-
target_input = gr.File(
|
| 71 |
-
file_types=[("image",), ("video",)],
|
| 72 |
-
file_count="multiple",
|
| 73 |
-
label="Ảnh/Video Target"
|
| 74 |
-
)
|
| 75 |
-
source_input = gr.File(
|
| 76 |
-
file_types=[("image",)],
|
| 77 |
-
label="Ảnh Source"
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
run_btn = gr.Button("🔄 Chạy Face Swap")
|
| 81 |
-
result_gallery = gr.Files(label="📂 File kết quả")
|
| 82 |
-
logs_output = gr.Markdown() # Không có label
|
| 83 |
-
|
| 84 |
-
run_btn.click(fn=run_scripts, inputs=[target_input, source_input], outputs=[result_gallery, logs_output])
|
| 85 |
-
|
| 86 |
-
demo.launch(
|
| 87 |
-
share=True,
|
| 88 |
-
server_name="0.0.0.0",
|
| 89 |
-
server_port=7860,
|
| 90 |
-
ssr_mode=False
|
| 91 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
+
import shutil
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
+
def run_scripts(target, source, use_face_enhancer):
|
| 8 |
+
outputfile=[]
|
| 9 |
+
for target_file in target :
|
| 10 |
+
target_extension = os.path.splitext(target_file.name)[-1]
|
| 11 |
+
target_name = os.path.splitext(target_file.name)[1]
|
| 12 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 13 |
+
output_path1 = "output" + timestamp + target_extension
|
| 14 |
+
cmd1 = ["python3", "run.py", "-s", source.name, "-t", target_file.name, "-o", output_path1, "--frame-processor", "face_swapper","face_enhancer",'--many-faces']
|
| 15 |
+
subprocess.run(cmd1)
|
| 16 |
+
outputfile.append(output_path1)
|
| 17 |
+
print(output_path1)
|
| 18 |
+
return outputfile
|
| 19 |
+
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
fn=run_scripts,
|
| 22 |
+
inputs=[
|
| 23 |
+
"files",
|
| 24 |
+
"file"
|
| 25 |
+
],
|
| 26 |
+
outputs="files",
|
| 27 |
+
title="Face swapper",
|
| 28 |
+
description="Upload a target image/video and a source image to swap faces.",
|
| 29 |
+
live=False
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|