File size: 7,309 Bytes
489f4d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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("""

    <h1 style='text-align: center;'>

        πŸ€— Hugging Face Hub Uploader

        <br>

        <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank' style='font-size: 14px; color: #667085;'>Built with anycoder</a>

    </h1>

    """)
    
    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()