|
|
import gradio as gr |
|
|
import subprocess |
|
|
import os |
|
|
import shutil |
|
|
import tempfile |
|
|
from datetime import datetime |
|
|
from huggingface_hub import ( |
|
|
login, |
|
|
hf_hub_download, |
|
|
upload_file, |
|
|
upload_folder, |
|
|
HfApi, |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
REPO_ID = os.getenv("HFPATH") |
|
|
HF_TOKEN = os.getenv("MAGIC") |
|
|
login(HF_TOKEN) |
|
|
|
|
|
api = HfApi() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def download_from_hf(subfolder: str): |
|
|
"""Trả về danh sách đường dẫn cục bộ của các file trong `subfolder`.""" |
|
|
downloaded_files = [] |
|
|
|
|
|
|
|
|
all_files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset") |
|
|
|
|
|
|
|
|
for file in all_files: |
|
|
if file.startswith(f"{subfolder}/"): |
|
|
local_path = hf_hub_download( |
|
|
repo_id=REPO_ID, |
|
|
filename=file, |
|
|
repo_type="dataset", |
|
|
) |
|
|
downloaded_files.append(local_path) |
|
|
|
|
|
return downloaded_files |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upload_to_hf(filepath: str): |
|
|
filename = os.path.basename(filepath) |
|
|
subfolder = datetime.now().strftime("%Y%m%d") |
|
|
path_in_repo = f"{subfolder}/{filename}" |
|
|
|
|
|
upload_file( |
|
|
path_or_fileobj=filepath, |
|
|
path_in_repo=api, |
|
|
repo_id=REPO_ID, |
|
|
repo_type="dataset", |
|
|
) |
|
|
return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{path_in_repo}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upload_folder_to_hf(folder_path: str): |
|
|
subfolder = datetime.now().strftime("%Y%m%d") |
|
|
upload_folder( |
|
|
folder_path=folder_path, |
|
|
path_in_repo=subfolder, |
|
|
repo_id=REPO_ID, |
|
|
repo_type="dataset", |
|
|
) |
|
|
return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{subfolder}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_scripts(subfolder: str, source): |
|
|
""" |
|
|
1. Tải về các file mục tiêu từ `subfolder`. |
|
|
2. Chạy `run.py` cho mỗi file, tạo file output. |
|
|
3. Sao chép các output vào thư mục tạm. |
|
|
4. Tải lên thư mục tạm lên Hugging Face. |
|
|
Trả về danh sách tên file output. |
|
|
""" |
|
|
output_files = [] |
|
|
target_files = download_from_hf(subfolder) |
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir: |
|
|
print(f"Thư mục tạm được tạo: {temp_dir}") |
|
|
|
|
|
for target_file in target_files: |
|
|
|
|
|
_, ext = os.path.splitext(target_file) |
|
|
timestamp = datetime.now().strftime("%Y%m%d%H%M%S") |
|
|
output_path = f"output_{timestamp}{ext}" |
|
|
|
|
|
|
|
|
cmd = [ |
|
|
"python3", |
|
|
"run.py", |
|
|
"-s", |
|
|
source.name, |
|
|
"-t", |
|
|
target_file, |
|
|
"-o", |
|
|
output_path, |
|
|
"--frame-processor", |
|
|
"face_swapper", |
|
|
"--many-faces", |
|
|
] |
|
|
|
|
|
|
|
|
subprocess.run(cmd, check=True) |
|
|
|
|
|
output_files.append(output_path) |
|
|
print(f"Tạo: {output_path}") |
|
|
|
|
|
|
|
|
dest_path = os.path.join(temp_dir, os.path.basename(output_path)) |
|
|
shutil.copy(output_path, dest_path) |
|
|
print(f"Đã sao chép {output_path} → {dest_path}") |
|
|
|
|
|
|
|
|
upload_folder_to_hf(temp_dir) |
|
|
|
|
|
return output_files |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=run_scripts, |
|
|
inputs=[ |
|
|
"text", |
|
|
"file", |
|
|
], |
|
|
outputs="files", |
|
|
title="Face Swapper", |
|
|
description=( |
|
|
"Nhập tên subfolder trong dataset và tải lên ảnh nguồn để thực hiện " |
|
|
"đổi mặt." |
|
|
), |
|
|
live=False, |
|
|
) |
|
|
|
|
|
iface.launch() |
|
|
|