jcplus/forker
1
1import gradio as gr2from huggingface_hub import create_repo, upload_file, whoami, Repository3import os4import shutil5 6def duplicate(source_repo, dst_repo, token, repo_type, progress=gr.Progress()):7 # Get username from token8 username = whoami(token=token)["name"]9 10 progress(0, desc="Starting duplication process")11 logs = ["Starting duplication process..."]12 13 # Create the destination repo14 progress(0.1, desc="Creating destination repository")15 logs.append("Creating destination repository")16 if repo_type in ["space", "dataset"]:17 url = create_repo(18 repo_id=dst_repo,19 token=token,20 repo_type=repo_type,21 space_sdk="gradio" if repo_type == "space" else None,22 private=False23 )24 else:25 url = create_repo(26 repo_id=dst_repo,27 token=token,28 private=False29 )30 31 # Clone source repo32 progress(0.2, desc="Cloning source repository")33 logs.append("Cloning source repository")34 endpoint = "huggingface.co/"35 if repo_type in ["space", "dataset"]:36 endpoint += f"{repo_type}/"37 full_path = f"https://{username}:{token}@{endpoint}{source_repo}"38 local_dir = f"hub/{source_repo}"39 40 repo = Repository(41 local_dir=local_dir,42 clone_from=full_path,43 repo_type=repo_type if repo_type in ["space", "dataset"] else None,44 token=token45 )46 47 # Get list of files to upload48 files_to_upload = []49 for root, _, files in os.walk(local_dir):50 if not root.startswith(".") and ".git" not in root:51 for f in files:52 if not f.startswith("."):53 files_to_upload.append((root, f))54 55 # Upload files with individual progress56 progress(0.3, desc="Preparing to upload files")57 logs.append(f"Found {len(files_to_upload)} files to upload")58 59 for i, (root, f) in enumerate(files_to_upload):60 file_progress = (i + 1) / len(files_to_upload)61 overall_progress = 0.3 + (0.6 * file_progress) # 0.3 to 0.9 range for upload62 directory_path_in_repo = "/".join(root.split("/")[2:])63 path_in_repo = os.path.join(directory_path_in_repo, f)64 local_file_path = os.path.join(local_dir, path_in_repo)65 66 progress(67 (overall_progress, [68 (file_progress, f"Uploading {f}"),69 ]),70 desc=f"Uploading file {i+1}/{len(files_to_upload)}"71 )72 logs.append(f"Uploaded {f} to {path_in_repo}")73 74 upload_file(75 path_or_fileobj=local_file_path,76 path_in_repo=path_in_repo,77 repo_id=dst_repo,78 token=token,79 repo_type=repo_type if repo_type != "model" else None80 )81 82 # Clean up83 progress(0.95, desc="Cleaning up temporary files")84 logs.append("Cleaning up temporary files")85 shutil.rmtree(local_dir, ignore_errors=True)86 87 progress(1.0, desc="Completed")88 logs.append("Duplication completed successfully")89 90 return (91 f"Find your repo <a href='{url}' target='_blank' style='text-decoration:underline'>here</a>",92 "\n".join(logs)93 )94 95# Custom CSS for yellow progress bars96css = """97.progress-container:nth-child(4) .progress-bar {98 background-color: yellow !important;99}100"""101 102# Updated Gradio interface with progress panel103with gr.Blocks(title="Duplicate your repo!", css=css) as interface:104 gr.Markdown("""105 # Duplicate your repo!106 Duplicate a Hugging Face repository! You need a write token from https://hf.co/settings/tokens.107 This Space is an experimental demo.108 """)109 110 with gr.Row():111 # Left panel - Inputs112 with gr.Column(scale=2):113 source_input = gr.Textbox(114 label="Source repository",115 placeholder="e.g. osanseviero/src"116 )117 dest_input = gr.Textbox(118 label="Destination repository",119 placeholder="e.g. osanseviero/dst"120 )121 token_input = gr.Textbox(122 label="Write access token",123 type="password",124 placeholder="Your HF token"125 )126 repo_type_input = gr.Dropdown(127 choices=["model", "dataset", "space"],128 label="Repository type",129 value="model"130 )131 submit_btn = gr.Button("Duplicate")132 output = gr.HTML(label="Result")133 134 # Right panel - Progress135 with gr.Column(scale=1):136 with gr.Group():137 gr.Markdown("## Progress")138 gr.Markdown("### Overall Progress")139 global_progress = gr.Progress()140 gr.Markdown("### File Progress")141 with gr.Column(variant="panel", elem_classes="progress-container"):142 file_progress = gr.Progress()143 log_output = gr.Textbox(label="Progress Log", lines=10)144 145 gr.Markdown("""146 Find your write token at 147 [token settings](https://huggingface.co/settings/tokens)148 """)149 150 submit_btn.click(151 fn=duplicate,152 inputs=[source_input, dest_input, token_input, repo_type_input],153 outputs=[output, log_output]154 )155 156interface.launch()