|
import gradio as gr |
|
import subprocess |
|
import os |
|
from datetime import datetime |
|
from huggingface_hub import login, hf_hub_download, upload_file, HfApi |
|
|
|
|
|
REPO_ID = os.environ.get("HFPATH") |
|
HF_TOKEN = os.environ.get("MAGIC") |
|
login(HF_TOKEN) |
|
|
|
|
|
api = HfApi() |
|
|
|
|
|
def download_from_hf(subfolder): |
|
downloaded_files = [] |
|
|
|
|
|
all_files = api.list_repo_files(repo_id=REPO_ID,repo_type="dataset") |
|
|
|
|
|
for file in all_files: |
|
if file.startswith(subfolder + "/"): |
|
|
|
downloaded_file = hf_hub_download(repo_id=REPO_ID, filename=file,repo_type="dataset" ) |
|
downloaded_files.append(downloaded_file) |
|
|
|
return downloaded_files |
|
|
|
def upload_to_hf(filepath): |
|
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=path_in_repo, |
|
repo_id=REPO_ID, |
|
repo_type="dataset" |
|
) |
|
return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{path_in_repo}" |
|
|
|
def run_scripts(subfolder, source): |
|
outputfile = [] |
|
target_files = download_from_hf(subfolder) |
|
for target_file in target_files: |
|
target_extension = os.path.splitext(target_file)[-1] |
|
|
|
timestamp = datetime.now().strftime("%Y%m%d%H%M%S") |
|
output_path = f"output_{timestamp}{target_extension}" |
|
cmd1 = ["python3", "run.py", "-s", source.name, "-t", target_file, "-o", output_path, "--frame-processor", "face_swapper", '--many-faces'] |
|
subprocess.run(cmd1) |
|
outputfile.append(output_path) |
|
print(output_path) |
|
upload_to_hf(output_path) |
|
return outputfile |
|
|
|
iface = gr.Interface( |
|
fn=run_scripts, |
|
inputs=[ |
|
"text", |
|
"file" |
|
], |
|
outputs="files", |
|
title="Face Swapper", |
|
description="Enter a subfolder name from the dataset and upload a source image to swap faces.", |
|
live=False |
|
) |
|
|
|
iface.launch() |
|
|