Jonny001 commited on
Commit
3528d04
Β·
verified Β·
1 Parent(s): fd787c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -32
app.py CHANGED
@@ -1,14 +1,17 @@
1
  import os
 
 
2
  import gradio as gr
3
  from huggingface_hub import snapshot_download, model_info, dataset_info, space_info
4
  from huggingface_hub.utils import RepositoryNotFoundError, HFValidationError
5
 
6
- # Allow only safe folders
7
- BASE_DOWNLOAD_DIR = "/home/user/downloads"
8
- os.makedirs(BASE_DOWNLOAD_DIR, exist_ok=True)
 
 
9
 
10
  def detect_repo_type(repo_id):
11
- """Try detecting the repo type automatically."""
12
  try:
13
  model_info(repo_id)
14
  return "model"
@@ -26,55 +29,60 @@ def detect_repo_type(repo_id):
26
  pass
27
  return None
28
 
29
- def download_hf_repo(repo_id, subfolder):
 
 
 
 
30
  repo_id = repo_id.strip()
31
- if not repo_id or "/" not in repo_id:
32
- return "❌ Please enter a valid Hugging Face repo ID (e.g. `username/repo_name`)"
33
 
34
- target_dir = os.path.join(BASE_DOWNLOAD_DIR, subfolder.strip() or repo_id.replace("/", "_"))
35
- os.makedirs(target_dir, exist_ok=True)
36
 
37
  repo_type = detect_repo_type(repo_id)
38
  if not repo_type:
39
- return f"❌ Repository '{repo_id}' not found as model, dataset, or space."
 
 
 
40
 
41
  try:
42
  log = f"πŸ” Detected repo type: {repo_type}\n"
43
  log += f"⬇️ Downloading to: {target_dir}...\n"
44
- output_path = snapshot_download(
 
45
  repo_id=repo_id,
46
  repo_type=repo_type,
47
  local_dir=target_dir,
48
  local_dir_use_symlinks=False
49
  )
50
- log += f"βœ… Download complete!\n\nSaved at:\n`{output_path}`"
51
- return log
 
 
 
 
 
52
  except Exception as e:
53
- return f"❌ Error occurred:\n{str(e)}"
54
 
55
- with gr.Blocks(title="Hugging Face Repo Downloader") as demo:
56
- gr.Markdown("## πŸ€– Hugging Face Repository Downloader")
57
- gr.Markdown("Download entire models, datasets, or spaces from the Hugging Face Hub with automatic type detection.")
58
 
59
  with gr.Row():
60
- repo_id_input = gr.Textbox(
61
- label="Hugging Face Repository",
62
- placeholder="e.g. NorthSage/Facefusion-NSFW",
63
- lines=1
64
- )
65
- subfolder_input = gr.Textbox(
66
- label="Download Folder Name (optional)",
67
- placeholder="e.g. facefusion",
68
- lines=1
69
- )
70
 
71
- download_btn = gr.Button("πŸš€ Start Download")
72
- output_log = gr.Textbox(label="Download Log", lines=15, interactive=False)
 
73
 
74
  download_btn.click(
75
  fn=download_hf_repo,
76
- inputs=[repo_id_input, subfolder_input],
77
- outputs=output_log
78
  )
79
 
80
- demo.launch()
 
1
  import os
2
+ import shutil
3
+ import zipfile
4
  import gradio as gr
5
  from huggingface_hub import snapshot_download, model_info, dataset_info, space_info
6
  from huggingface_hub.utils import RepositoryNotFoundError, HFValidationError
7
 
8
+ # Setup base directory
9
+ BASE_DIR = "/home/user/downloads"
10
+ ZIP_DIR = "/home/user/zips"
11
+ os.makedirs(BASE_DIR, exist_ok=True)
12
+ os.makedirs(ZIP_DIR, exist_ok=True)
13
 
14
  def detect_repo_type(repo_id):
 
15
  try:
16
  model_info(repo_id)
17
  return "model"
 
29
  pass
30
  return None
31
 
32
+ def zip_folder(source_folder, output_zip):
33
+ shutil.make_archive(output_zip.replace('.zip', ''), 'zip', source_folder)
34
+ return output_zip
35
+
36
+ def download_hf_repo(repo_id, folder_name):
37
  repo_id = repo_id.strip()
38
+ folder_name = folder_name.strip() or repo_id.replace("/", "_")
 
39
 
40
+ if not repo_id or "/" not in repo_id:
41
+ return "❌ Please enter a valid Hugging Face repo ID.", None
42
 
43
  repo_type = detect_repo_type(repo_id)
44
  if not repo_type:
45
+ return f"❌ Repository '{repo_id}' not found.", None
46
+
47
+ target_dir = os.path.join(BASE_DIR, folder_name)
48
+ os.makedirs(target_dir, exist_ok=True)
49
 
50
  try:
51
  log = f"πŸ” Detected repo type: {repo_type}\n"
52
  log += f"⬇️ Downloading to: {target_dir}...\n"
53
+
54
+ snapshot_download(
55
  repo_id=repo_id,
56
  repo_type=repo_type,
57
  local_dir=target_dir,
58
  local_dir_use_symlinks=False
59
  )
60
+
61
+ # ZIP the folder
62
+ zip_path = os.path.join(ZIP_DIR, f"{folder_name}.zip")
63
+ zip_folder(target_dir, zip_path)
64
+
65
+ log += f"βœ… Download complete! Repository saved and zipped.\n\n"
66
+ return log, zip_path
67
  except Exception as e:
68
+ return f"❌ Error occurred:\n{str(e)}", None
69
 
70
+ with gr.Blocks(title="HF RepoSnap") as demo:
71
+ gr.Markdown("## πŸ€– HF Downloader")
72
+ gr.Markdown("Download any Hugging Face model, dataset, or space and export it as a .zip file for local use.")
73
 
74
  with gr.Row():
75
+ repo_input = gr.Textbox(label="Hugging Face Repository", placeholder="e.g. NorthSage/Facefusion-NSFW")
76
+ folder_input = gr.Textbox(label="Download Folder Name (optional)", placeholder="e.g. facefusion")
 
 
 
 
 
 
 
 
77
 
78
+ download_btn = gr.Button("πŸš€ Download & Zip Repository")
79
+ log_output = gr.Textbox(label="Status Log", lines=12, interactive=False)
80
+ file_output = gr.File(label="πŸ“¦ Download Zipped Repository")
81
 
82
  download_btn.click(
83
  fn=download_hf_repo,
84
+ inputs=[repo_input, folder_input],
85
+ outputs=[log_output, file_output]
86
  )
87
 
88
+ demo.launch()