import gradio as gr from huggingface_hub import HfApi import os # Initialize HfApi api = HfApi() def upload_file_to_hub(file_path, repo_id, path_in_repo, token, repo_type, commit_message): """ Upload a single file to Hugging Face Hub """ if not file_path: return "❌ Please select a file to upload" if not repo_id: return "❌ Please enter a repository ID" if not token: return "❌ Please enter your Hugging Face token" try: # Upload file api.upload_file( path_or_fileobj=file_path, path_in_repo=path_in_repo or os.path.basename(file_path), repo_id=repo_id, repo_type=repo_type, token=token, commit_message=commit_message ) return f"✅ Successfully uploaded file to {repo_id}/{path_in_repo or os.path.basename(file_path)}" except Exception as e: return f"❌ Error uploading file: {str(e)}" def upload_folder_to_hub(folder_path, repo_id, folder_path_in_repo, token, repo_type, commit_message, ignore_patterns): """ Upload a folder to Hugging Face Hub """ if not folder_path: return "❌ Please enter a folder path" if not repo_id: return "❌ Please enter a repository ID" if not token: return "❌ Please enter your Hugging Face token" if not os.path.isdir(folder_path): return f"❌ The path '{folder_path}' is not a valid directory" try: # Parse ignore patterns ignore_list = None if ignore_patterns.strip(): ignore_list = [pattern.strip() for pattern in ignore_patterns.split(",")] # Upload folder api.upload_folder( folder_path=folder_path, path_in_repo=folder_path_in_repo, repo_id=repo_id, repo_type=repo_type, token=token, commit_message=commit_message, ignore_patterns=ignore_list ) return f"✅ Successfully uploaded folder to {repo_id}/{folder_path_in_repo or ''}" except Exception as e: return f"❌ Error uploading folder: {str(e)}" # Create Gradio interface with gr.Blocks(title="Hugging Face Hub Uploader", theme=gr.themes.Soft()) as demo: gr.HTML("""

🤗 Hugging Face Hub Uploader
Built with anycoder

""") gr.Markdown(""" Upload files or folders to your Hugging Face repositories easily. **Get your token from [Hugging Face Settings](https://huggingface.co/settings/tokens)** """) with gr.Tab("📁 Upload File"): with gr.Row(): with gr.Column(): file_input = gr.File( label="Select File", file_count="single", type="filepath" ) repo_id_file = gr.Textbox( label="Repository ID", placeholder="username/repository-name", info="The repository ID (e.g., 'username/my-model')" ) path_in_repo_file = gr.Textbox( label="Path in Repository", placeholder="README.md", info="Where to store the file in the repo. If empty, uses the original filename" ) repo_type_file = gr.Radio( choices=["model", "dataset", "space"], value="model", label="Repository Type" ) commit_message_file = gr.Textbox( label="Commit Message (optional)", placeholder="Add new file", value="Upload file via Gradio app" ) token_file = gr.Textbox( label="Hugging Face Token", type="password", placeholder="hf_...", info="Your Hugging Face access token" ) upload_file_btn = gr.Button("Upload File", variant="primary") with gr.Column(): file_output = gr.Textbox( label="Upload Status", lines=8, interactive=False ) upload_file_btn.click( fn=upload_file_to_hub, inputs=[file_input, repo_id_file, path_in_repo_file, token_file, repo_type_file, commit_message_file], outputs=file_output ) with gr.Tab("📂 Upload Folder"): with gr.Row(): with gr.Column(): folder_input = gr.Textbox( label="Folder Path", placeholder="/path/to/local/folder", info="Absolute path to the folder you want to upload" ) repo_id_folder = gr.Textbox( label="Repository ID", placeholder="username/repository-name", info="The repository ID (e.g., 'username/my-dataset')" ) folder_path_in_repo = gr.Textbox( label="Folder Path in Repository", placeholder="data/", info="Where to store the folder in the repo. If empty, uploads to root" ) repo_type_folder = gr.Radio( choices=["model", "dataset", "space"], value="dataset", label="Repository Type" ) commit_message_folder = gr.Textbox( label="Commit Message (optional)", placeholder="Add new folder", value="Upload folder via Gradio app" ) ignore_patterns = gr.Textbox( label="Ignore Patterns (optional)", placeholder="*.pyc, __pycache__, .git", info="Comma-separated patterns to ignore (e.g., '*.pyc, __pycache__')" ) token_folder = gr.Textbox( label="Hugging Face Token", type="password", placeholder="hf_...", info="Your Hugging Face access token" ) upload_folder_btn = gr.Button("Upload Folder", variant="primary") with gr.Column(): folder_output = gr.Textbox( label="Upload Status", lines=8, interactive=False ) upload_folder_btn.click( fn=upload_folder_to_hub, inputs=[folder_input, repo_id_folder, folder_path_in_repo, token_folder, repo_type_folder, commit_message_folder, ignore_patterns], outputs=folder_output ) if __name__ == "__main__": demo.launch()