Spaces:
mr2along
/
Runtime error

mr2along commited on
Commit
f6306b2
verified
1 Parent(s): 3e37595

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -3,15 +3,29 @@ import subprocess
3
  import shutil
4
  import os
5
  from datetime import datetime
6
- from huggingface_hub import login, upload_file
7
 
8
  # --- Config ---
9
  REPO_ID = "mr2along/Picture"
10
  HF_TOKEN = os.environ.get("MAGIC")
11
- #print(HF_TOKEN)
12
- #if not HF_TOKEN:
13
- # print("鈿狅笍 Kh么ng t矛m th岷 bi岷縩 m么i tr瓢峄漬g 'MAGIC'")
14
  login(HF_TOKEN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # --- Upload function ---
16
  def upload_to_hf(filepath):
17
  filename = os.path.basename(filepath)
@@ -25,14 +39,15 @@ def upload_to_hf(filepath):
25
  )
26
  return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{filename}"
27
 
28
- def run_scripts(target, source):
29
- outputfile=[]
30
- for target_file in target :
31
- target_extension = os.path.splitext(target_file.name)[-1]
32
- target_name = os.path.splitext(target_file.name)[1]
 
33
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
34
  output_path1 = "output" + timestamp + target_extension
35
- cmd1 = ["python3", "run.py", "-s", source.name, "-t", target_file.name, "-o", output_path1, "--frame-processor", "face_swapper","face_enhancer",'--many-faces']
36
  subprocess.run(cmd1)
37
  outputfile.append(output_path1)
38
  print(output_path1)
@@ -42,13 +57,13 @@ def run_scripts(target, source):
42
  iface = gr.Interface(
43
  fn=run_scripts,
44
  inputs=[
45
- "files",
46
  "file"
47
  ],
48
  outputs="files",
49
  title="Face swapper",
50
- description="Upload a target image/video and a source image to swap faces.",
51
  live=False
52
  )
53
 
54
- iface.launch()
 
3
  import shutil
4
  import os
5
  from datetime import datetime
6
+ from huggingface_hub import login, download_file, list_datasets, upload_file
7
 
8
  # --- Config ---
9
  REPO_ID = "mr2along/Picture"
10
  HF_TOKEN = os.environ.get("MAGIC")
 
 
 
11
  login(HF_TOKEN)
12
+
13
+ # --- Download function ---
14
+ def download_from_hf(subfolder):
15
+ downloaded_files = []
16
+ files = list_datasets(REPO_ID)
17
+ for file in files:
18
+ if file.filename.startswith(subfolder + "/"):
19
+ filename = file.filename.split("/")[-1]
20
+ download_file(
21
+ path_or_url=file.filename,
22
+ repo_id=REPO_ID,
23
+ repo_type="dataset",
24
+ local_path=filename
25
+ )
26
+ downloaded_files.append(filename)
27
+ return downloaded_files
28
+
29
  # --- Upload function ---
30
  def upload_to_hf(filepath):
31
  filename = os.path.basename(filepath)
 
39
  )
40
  return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{filename}"
41
 
42
+ def run_scripts(subfolder, source):
43
+ outputfile = []
44
+ target_files = download_from_hf(subfolder)
45
+ for target_file in target_files:
46
+ target_extension = os.path.splitext(target_file)[-1]
47
+ target_name = os.path.splitext(target_file)[1]
48
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
49
  output_path1 = "output" + timestamp + target_extension
50
+ cmd1 = ["python3", "run.py", "-s", source.name, "-t", target_file, "-o", output_path1, "--frame-processor", "face_swapper","face_enhancer",'--many-faces']
51
  subprocess.run(cmd1)
52
  outputfile.append(output_path1)
53
  print(output_path1)
 
57
  iface = gr.Interface(
58
  fn=run_scripts,
59
  inputs=[
60
+ gr.Textbox(label="Target subfolder"),
61
  "file"
62
  ],
63
  outputs="files",
64
  title="Face swapper",
65
+ description="Enter a subfolder name from the dataset and upload a source image to swap faces.",
66
  live=False
67
  )
68
 
69
+ iface.launch()