Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import
|
|
|
|
| 3 |
|
| 4 |
def fetch_files(source_repo, hf_token):
|
| 5 |
-
# Input validation
|
| 6 |
if not hf_token.strip():
|
| 7 |
yield gr.update(), gr.update(), "Error: Hugging Face Access Token is required.", []
|
| 8 |
return
|
|
@@ -47,42 +47,44 @@ def copy_repository_files(source_repo, target_repo, hf_token, selected_files):
|
|
| 47 |
yield "Error: At least one file must be selected to copy."
|
| 48 |
return
|
| 49 |
|
| 50 |
-
|
|
|
|
| 51 |
src_clean = source_repo.strip()
|
| 52 |
tgt_clean = target_repo.strip()
|
| 53 |
|
| 54 |
try:
|
| 55 |
-
# Step 1: Ensure
|
| 56 |
yield f"Status: Ensuring destination repository '{tgt_clean}' exists..."
|
| 57 |
api.create_repo(
|
| 58 |
repo_id=tgt_clean,
|
| 59 |
repo_type="model",
|
| 60 |
exist_ok=True,
|
| 61 |
-
private=True
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
-
yield f"Success! Transferred {
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
yield f"An error occurred: {str(e)}"
|
|
@@ -91,7 +93,7 @@ def copy_repository_files(source_repo, target_repo, hf_token, selected_files):
|
|
| 91 |
with gr.Blocks(title="Selective HF Model Cloner") as demo:
|
| 92 |
gr.Markdown("# 📑 Selective Hugging Face Model Cloner")
|
| 93 |
gr.Markdown(
|
| 94 |
-
"
|
| 95 |
)
|
| 96 |
|
| 97 |
with gr.Row():
|
|
@@ -99,7 +101,7 @@ with gr.Blocks(title="Selective HF Model Cloner") as demo:
|
|
| 99 |
label="Hugging Face Access Token",
|
| 100 |
placeholder="hf_...",
|
| 101 |
type="password",
|
| 102 |
-
info="Requires 'Write' permissions on your account
|
| 103 |
)
|
| 104 |
|
| 105 |
with gr.Row():
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import HfApi, hf_hub_url
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
def fetch_files(source_repo, hf_token):
|
|
|
|
| 6 |
if not hf_token.strip():
|
| 7 |
yield gr.update(), gr.update(), "Error: Hugging Face Access Token is required.", []
|
| 8 |
return
|
|
|
|
| 47 |
yield "Error: At least one file must be selected to copy."
|
| 48 |
return
|
| 49 |
|
| 50 |
+
token = hf_token.strip()
|
| 51 |
+
api = HfApi(token=token)
|
| 52 |
src_clean = source_repo.strip()
|
| 53 |
tgt_clean = target_repo.strip()
|
| 54 |
|
| 55 |
try:
|
| 56 |
+
# Step 1: Ensure destination repo exists
|
| 57 |
yield f"Status: Ensuring destination repository '{tgt_clean}' exists..."
|
| 58 |
api.create_repo(
|
| 59 |
repo_id=tgt_clean,
|
| 60 |
repo_type="model",
|
| 61 |
exist_ok=True,
|
| 62 |
+
private=True
|
| 63 |
)
|
| 64 |
|
| 65 |
+
headers = {"Authorization": f"Bearer {token}"}
|
| 66 |
+
total_files = len(selected_files)
|
| 67 |
+
|
| 68 |
+
# Step 2: Stream each file directly from source URL to target repo without writing to disk
|
| 69 |
+
for idx, file_path in enumerate(selected_files, start=1):
|
| 70 |
+
yield f"Status: Transferring ({idx}/{total_files}): {file_path}..."
|
| 71 |
+
|
| 72 |
+
file_url = hf_hub_url(repo_id=src_clean, filename=file_path, repo_type="model")
|
| 73 |
+
|
| 74 |
+
# Stream download so large files don't exhaust system RAM
|
| 75 |
+
with requests.get(file_url, headers=headers, stream=True) as response:
|
| 76 |
+
response.raise_for_status()
|
| 77 |
+
|
| 78 |
+
# Upload streamed raw response directly into the target repository
|
| 79 |
+
api.upload_file(
|
| 80 |
+
path_or_fileobj=response.raw,
|
| 81 |
+
path_in_repo=file_path,
|
| 82 |
+
repo_id=tgt_clean,
|
| 83 |
+
repo_type="model",
|
| 84 |
+
commit_message=f"Copy {file_path} from {src_clean}"
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
yield f"Success! Transferred {total_files} files. View model: https://huggingface.co/{tgt_clean}"
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
yield f"An error occurred: {str(e)}"
|
|
|
|
| 93 |
with gr.Blocks(title="Selective HF Model Cloner") as demo:
|
| 94 |
gr.Markdown("# 📑 Selective Hugging Face Model Cloner")
|
| 95 |
gr.Markdown(
|
| 96 |
+
"Stream-transfer selected files from gated repositories to your own model repository without writing to local disk."
|
| 97 |
)
|
| 98 |
|
| 99 |
with gr.Row():
|
|
|
|
| 101 |
label="Hugging Face Access Token",
|
| 102 |
placeholder="hf_...",
|
| 103 |
type="password",
|
| 104 |
+
info="Requires 'Write' permissions on your account.",
|
| 105 |
)
|
| 106 |
|
| 107 |
with gr.Row():
|