mr2along commited on
Commit
b579390
·
verified ·
1 Parent(s): fa256ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -2,8 +2,9 @@ import gradio as gr
2
  import subprocess
3
  import os
4
  from datetime import datetime
5
- from huggingface_hub import login, hf_hub_download, upload_file, HfApi
6
-
 
7
  # --- Config ---
8
  REPO_ID = os.environ.get("HFPATH")
9
  HF_TOKEN = os.environ.get("MAGIC")
@@ -39,11 +40,24 @@ def upload_to_hf(filepath):
39
  repo_type="dataset"
40
  )
41
  return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{path_in_repo}"
 
 
 
 
 
 
 
42
 
43
  def run_scripts(subfolder, source):
44
  outputfile = []
45
  target_files = download_from_hf(subfolder)
46
- for target_file in target_files:
 
 
 
 
 
 
47
  target_extension = os.path.splitext(target_file)[-1]
48
  #filename = os.path.splitext(target_file)[0]
49
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
@@ -52,9 +66,14 @@ def run_scripts(subfolder, source):
52
  subprocess.run(cmd1)
53
  outputfile.append(output_path)
54
  print(output_path)
55
- upload_to_hf(output_path)
56
- return outputfile
 
 
57
 
 
 
 
58
  iface = gr.Interface(
59
  fn=run_scripts,
60
  inputs=[
 
2
  import subprocess
3
  import os
4
  from datetime import datetime
5
+ from huggingface_hub import login, hf_hub_download, upload_file, HfApi, upload_folder
6
+ import tempfile
7
+ import shutil
8
  # --- Config ---
9
  REPO_ID = os.environ.get("HFPATH")
10
  HF_TOKEN = os.environ.get("MAGIC")
 
40
  repo_type="dataset"
41
  )
42
  return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{path_in_repo}"
43
+ def upload_to_fd(path):
44
+ SUBFOLDER = datetime.now().strftime("%Y%m%d")
45
+ upload_folder(folder_path=path,
46
+ path_in_repo=SUBFOLDER,
47
+ repo_id=REPO_ID,
48
+ repo_type="dataset")
49
+ return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{SUBFOLDER}"
50
 
51
  def run_scripts(subfolder, source):
52
  outputfile = []
53
  target_files = download_from_hf(subfolder)
54
+
55
+
56
+ # Tạo thư mục tạm và lưu file đầu ra vào đó
57
+ with tempfile.TemporaryDirectory() as temp_dir:
58
+ print(f"Thư mục tạm thời được tạo: {temp_dir}")
59
+
60
+ for target_file in target_files:
61
  target_extension = os.path.splitext(target_file)[-1]
62
  #filename = os.path.splitext(target_file)[0]
63
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
 
66
  subprocess.run(cmd1)
67
  outputfile.append(output_path)
68
  print(output_path)
69
+ # Sao chép file vào thư mục tạm
70
+ destination_file = os.path.join(temp_dir, os.path.basename(output_path))
71
+ shutil.copy(output_path, destination_file)
72
+ print(f"Đã sao chép {output_path} vào {destination_file}")
73
 
74
+ # Tải lên thư mục tạm lên Hugging Face
75
+ upload_to_fd(temp_dir)
76
+ return outputfile
77
  iface = gr.Interface(
78
  fn=run_scripts,
79
  inputs=[