Spaces:
Running
Running
Update app.py
Browse files
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 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
repo_id = repo_id.strip()
|
| 31 |
-
|
| 32 |
-
return "β Please enter a valid Hugging Face repo ID (e.g. `username/repo_name`)"
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
repo_type = detect_repo_type(repo_id)
|
| 38 |
if not repo_type:
|
| 39 |
-
return f"β Repository '{repo_id}' not found
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
try:
|
| 42 |
log = f"π Detected repo type: {repo_type}\n"
|
| 43 |
log += f"β¬οΈ Downloading to: {target_dir}...\n"
|
| 44 |
-
|
|
|
|
| 45 |
repo_id=repo_id,
|
| 46 |
repo_type=repo_type,
|
| 47 |
local_dir=target_dir,
|
| 48 |
local_dir_use_symlinks=False
|
| 49 |
)
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
-
return f"β Error occurred:\n{str(e)}"
|
| 54 |
|
| 55 |
-
with gr.Blocks(title="
|
| 56 |
-
gr.Markdown("## π€
|
| 57 |
-
gr.Markdown("Download
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
-
|
| 61 |
-
|
| 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("π
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
download_btn.click(
|
| 75 |
fn=download_hf_repo,
|
| 76 |
-
inputs=[
|
| 77 |
-
outputs=
|
| 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()
|