rootlocalghost commited on
Commit
7bc319a
Β·
verified Β·
1 Parent(s): 608dc33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -74
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from huggingface_hub import HfApi, CommitOperationDelete
3
 
4
  def fetch_files(source_repo, hf_token):
5
  # Input validation
@@ -9,133 +9,133 @@ def fetch_files(source_repo, hf_token):
9
  if not source_repo.strip():
10
  yield gr.update(), gr.update(), "Error: Source repository name is required.", []
11
  return
12
-
13
  api = HfApi(token=hf_token.strip())
14
-
15
  try:
16
- # Initial status update before fetching
17
  yield gr.update(), gr.update(), f"Status: Fetching file list from '{source_repo}'...", []
18
-
19
- # Fetch the flat list of files from the repository
20
  files = api.list_repo_files(repo_id=source_repo.strip(), repo_type="model")
21
  files.sort()
22
-
23
- # Build a visual Linux-like tree representation for the CheckboxGroup
24
  choices = []
25
  for f in files:
26
- parts = f.split('/')
27
  if len(parts) == 1:
28
  display = f"πŸ“„ {f}"
29
  else:
30
  folder_path = "/".join(parts[:-1])
31
  display = f"πŸ“ {folder_path}/ β”œβ”€β”€ πŸ“„ {parts[-1]}"
32
-
33
- # Gradio allows (display_name, value) tuples for CheckboxGroup choices
34
  choices.append((display, f))
35
-
36
  yield (
37
- gr.update(visible=True),
38
- gr.update(choices=choices, value=files),
39
  f"Success: Fetched {len(files)} files. Uncheck the ones you do not want to copy.",
40
- files
41
  )
42
-
43
  except Exception as e:
44
  yield gr.update(visible=False), gr.update(), f"An error occurred: {str(e)}", []
45
 
46
 
47
- def clone_repository(source_repo, target_repo, hf_token, selected_files, all_files):
48
  if not target_repo.strip():
49
  yield "Error: Target repository name is required."
50
  return
51
-
 
 
 
52
  api = HfApi(token=hf_token.strip())
53
-
 
 
54
  try:
55
- yield f"Status: Initiating server-side clone of '{source_repo}' to '{target_repo}'..."
56
-
57
- # 1. Duplicate the entire repo first (Server-side, almost instant, 0 disk space)
58
- api.duplicate_repo(
59
- from_id=source_repo.strip(),
60
- to_id=target_repo.strip(),
61
- repo_type="model"
62
  )
63
-
64
- # 2. Determine which files to delete based on what the user unchecked
65
- files_to_delete = set(all_files) - set(selected_files)
66
-
67
- if files_to_delete:
68
- yield f"Status: Deleting {len(files_to_delete)} unselected files from '{target_repo}'..."
69
-
70
- # Use 'path_in_repo' instead of 'path' to comply with the HF SDK
71
- operations = [CommitOperationDelete(path_in_repo=f) for f in files_to_delete]
72
-
73
- # Execute deletion in a single commit natively on HF servers
74
- api.create_commit(
75
- repo_id=target_repo.strip(),
76
- repo_type="model",
77
- operations=operations,
78
- commit_message="Clean up: Remove unselected files during selective clone"
79
  )
80
-
81
- yield f"Success! Selective clone completed. View your model here: https://huggingface.co/{target_repo.strip()}"
82
-
 
 
 
 
 
 
 
 
 
 
 
83
  except Exception as e:
84
  yield f"An error occurred: {str(e)}"
85
 
86
 
87
- # Notice: The 'theme' parameter has been removed from Blocks to satisfy Gradio 6.0 specs
88
  with gr.Blocks(title="Selective HF Model Cloner") as demo:
89
  gr.Markdown("# πŸ“‘ Selective Hugging Face Model Cloner")
90
- gr.Markdown("Fetch files from a model repository, uncheck what you don't need, and clone via high-speed server-side duplication.")
91
-
 
 
92
  with gr.Row():
93
  hf_token = gr.Textbox(
94
- label="Hugging Face Access Token",
95
- placeholder="hf_...",
96
  type="password",
97
- info="Requires 'Write' permissions."
98
  )
99
-
100
  with gr.Row():
101
  source_repo = gr.Textbox(
102
- label="Source Model Repository",
103
- placeholder="e.g., meta-llama/Llama-3.2-1B"
104
  )
105
  fetch_btn = gr.Button("1. Fetch Files", variant="secondary")
106
-
107
- # We use a Gradio State to quietly keep track of the complete list of files fetched
108
  all_files_state = gr.State([])
109
-
110
- # This group remains hidden until files are successfully fetched
111
  with gr.Group(visible=False) as selection_group:
112
  gr.Markdown("### πŸ—‚οΈ Repository Files")
113
- gr.Markdown("*All files are selected by default. Uncheck any files you do **not** want to carry over.*")
114
-
115
  file_checkboxes = gr.CheckboxGroup(label="Repository Tree", interactive=True)
116
-
117
  target_repo = gr.Textbox(
118
- label="Target Model Repository",
119
- placeholder="e.g., your-username/my-custom-llama"
120
  )
121
- clone_btn = gr.Button("2. Clone Selected Files", variant="primary")
122
-
123
  output_logs = gr.Textbox(label="Execution Status / Logs", interactive=False)
124
-
125
- # Trigger Step 1: File Fetching
126
  fetch_btn.click(
127
  fn=fetch_files,
128
  inputs=[source_repo, hf_token],
129
- outputs=[selection_group, file_checkboxes, output_logs, all_files_state]
130
  )
131
-
132
- # Trigger Step 2: Cloning Process
133
  clone_btn.click(
134
- fn=clone_repository,
135
- inputs=[source_repo, target_repo, hf_token, file_checkboxes, all_files_state],
136
- outputs=output_logs
137
  )
138
 
139
  if __name__ == "__main__":
140
- # The theme parameter is now passed into launch() per Gradio 6.0 rules
141
  demo.queue().launch(theme=gr.themes.Soft())
 
1
  import gradio as gr
2
+ from huggingface_hub import CommitOperationCopy, HfApi
3
 
4
  def fetch_files(source_repo, hf_token):
5
  # Input validation
 
9
  if not source_repo.strip():
10
  yield gr.update(), gr.update(), "Error: Source repository name is required.", []
11
  return
12
+
13
  api = HfApi(token=hf_token.strip())
14
+
15
  try:
 
16
  yield gr.update(), gr.update(), f"Status: Fetching file list from '{source_repo}'...", []
17
+
 
18
  files = api.list_repo_files(repo_id=source_repo.strip(), repo_type="model")
19
  files.sort()
20
+
 
21
  choices = []
22
  for f in files:
23
+ parts = f.split("/")
24
  if len(parts) == 1:
25
  display = f"πŸ“„ {f}"
26
  else:
27
  folder_path = "/".join(parts[:-1])
28
  display = f"πŸ“ {folder_path}/ β”œβ”€β”€ πŸ“„ {parts[-1]}"
 
 
29
  choices.append((display, f))
30
+
31
  yield (
32
+ gr.update(visible=True),
33
+ gr.update(choices=choices, value=files),
34
  f"Success: Fetched {len(files)} files. Uncheck the ones you do not want to copy.",
35
+ files,
36
  )
37
+
38
  except Exception as e:
39
  yield gr.update(visible=False), gr.update(), f"An error occurred: {str(e)}", []
40
 
41
 
42
+ def copy_repository_files(source_repo, target_repo, hf_token, selected_files):
43
  if not target_repo.strip():
44
  yield "Error: Target repository name is required."
45
  return
46
+ if not 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)}"
89
 
90
 
 
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():
98
  hf_token = gr.Textbox(
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():
106
  source_repo = gr.Textbox(
107
+ label="Source Model Repository",
108
+ placeholder="e.g., black-forest-labs/flux-3-image-klein",
109
  )
110
  fetch_btn = gr.Button("1. Fetch Files", variant="secondary")
111
+
 
112
  all_files_state = gr.State([])
113
+
 
114
  with gr.Group(visible=False) as selection_group:
115
  gr.Markdown("### πŸ—‚οΈ Repository Files")
116
+ gr.Markdown("*Uncheck files you want to omit from the destination repository.*")
117
+
118
  file_checkboxes = gr.CheckboxGroup(label="Repository Tree", interactive=True)
119
+
120
  target_repo = gr.Textbox(
121
+ label="Target Model Repository",
122
+ placeholder="e.g., your-username/my-custom-model",
123
  )
124
+ clone_btn = gr.Button("2. Copy Selected Files", variant="primary")
125
+
126
  output_logs = gr.Textbox(label="Execution Status / Logs", interactive=False)
127
+
 
128
  fetch_btn.click(
129
  fn=fetch_files,
130
  inputs=[source_repo, hf_token],
131
+ outputs=[selection_group, file_checkboxes, output_logs, all_files_state],
132
  )
133
+
 
134
  clone_btn.click(
135
+ fn=copy_repository_files,
136
+ inputs=[source_repo, target_repo, hf_token, file_checkboxes],
137
+ outputs=output_logs,
138
  )
139
 
140
  if __name__ == "__main__":
 
141
  demo.queue().launch(theme=gr.themes.Soft())