Tata / app.py
mr2along's picture
Update app.py
bccaf9c verified
import gradio as gr
import subprocess
import os
from datetime import datetime
from huggingface_hub import login, hf_hub_download, upload_file, HfApi
# --- Config ---
REPO_ID = os.environ.get("HFPATH")
HF_TOKEN = os.environ.get("MAGIC")
login(HF_TOKEN)
# --- Download function ---
api = HfApi()
# --- Download function ---
def download_from_hf(subfolder):
downloaded_files = []
# List all files in the repository
all_files = api.list_repo_files(repo_id=REPO_ID,repo_type="dataset")
# Filter files that start with the specified subfolder
for file in all_files:
if file.startswith(subfolder + "/"):
# Download the file
downloaded_file = hf_hub_download(repo_id=REPO_ID, filename=file,repo_type="dataset" )
downloaded_files.append(downloaded_file)
return downloaded_files
# --- Upload function ---
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]
#filename = os.path.splitext(target_file)[0]
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()