rootlocalghost commited on
Commit
f3073eb
·
verified ·
1 Parent(s): 7bc319a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -28
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import gradio as gr
2
- from huggingface_hub import CommitOperationCopy, HfApi
 
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
- api = HfApi(token=hf_token.strip())
 
51
  src_clean = source_repo.strip()
52
  tgt_clean = target_repo.strip()
53
 
54
  try:
55
- # Step 1: Ensure target repo exists (create private or public target model)
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 # Recommended default for gated clones
62
  )
63
 
64
- # Step 2: Build server-side copy operations for selected files
65
- yield f"Status: Preparing server-side copy for {len(selected_files)} files..."
66
- operations = [
67
- CommitOperationCopy(
68
- src_path_in_repo=f,
69
- path_in_repo=f,
70
- src_repo_id=src_clean,
71
- src_repo_type="model",
72
- )
73
- for f in selected_files
74
- ]
75
-
76
- # Step 3: Execute commit natively on Hugging Face servers
77
- yield f"Status: Committing {len(operations)} files directly to '{tgt_clean}'..."
78
- api.create_commit(
79
- repo_id=tgt_clean,
80
- repo_type="model",
81
- operations=operations,
82
- commit_message=f"Selective clone of {len(selected_files)} files from {src_clean}",
83
- )
 
84
 
85
- yield f"Success! Transferred {len(selected_files)} files. View model: https://huggingface.co/{tgt_clean}"
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
- "Fetch files from any accessible source repo (including gated models) and copy selected files directly on the server without downloading weights."
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 and accepted terms on gated models.",
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():