File size: 1,671 Bytes
21f0a5b
 
c24c808
21f0a5b
52dcdf4
76de74c
21f0a5b
dc946da
fbe2814
76de74c
33b7d02
 
 
76de74c
dc946da
76de74c
 
3e37595
 
76de74c
 
3e37595
76de74c
 
 
 
67c5bb4
349ba46
c24c808
 
 
 
 
 
 
 
 
 
76de74c
c24c808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
import subprocess
import shutil
import os
from datetime import datetime
from huggingface_hub import login, upload_file

# --- Config ---
REPO_ID = os.environ.get("HFPATH")
HF_TOKEN = os.environ.get("MAGIC")
#print(HF_TOKEN)
#if not HF_TOKEN:
#    print("⚠️ Không tìm thấy biến môi trường 'MAGIC'")
login(HF_TOKEN)
# --- 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/{filename}"

def run_scripts(target, source):
    outputfile=[]
    for target_file in target :
        target_extension = os.path.splitext(target_file.name)[-1]
        target_name = os.path.splitext(target_file.name)[1]
        timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
        output_path1 = "output" + timestamp + target_extension
        cmd1 = ["python3", "run.py", "-s", source.name, "-t", target_file.name, "-o", output_path1, "--frame-processor", "face_swapper","face_enhancer",'--many-faces']
        subprocess.run(cmd1)
        outputfile.append(output_path1)
        print(output_path1)
        upload_to_hf(output_path1)
    return outputfile

iface = gr.Interface(
    fn=run_scripts,
    inputs=[
        "files",
        "file"
    ],
    outputs="files",
    title="Face swapper",
    description="Upload a target image/video and a source image to swap faces.",
    live=False
)

iface.launch()