MySafeCode commited on
Commit
e5917c4
Β·
verified Β·
1 Parent(s): b9d3c21

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +191 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import HfApi
3
+ import os
4
+
5
+ # Initialize HfApi
6
+ api = HfApi()
7
+
8
+ def upload_file_to_hub(file_path, repo_id, path_in_repo, token, repo_type, commit_message):
9
+ """
10
+ Upload a single file to Hugging Face Hub
11
+ """
12
+ if not file_path:
13
+ return "❌ Please select a file to upload"
14
+
15
+ if not repo_id:
16
+ return "❌ Please enter a repository ID"
17
+
18
+ if not token:
19
+ return "❌ Please enter your Hugging Face token"
20
+
21
+ try:
22
+ # Upload file
23
+ api.upload_file(
24
+ path_or_fileobj=file_path,
25
+ path_in_repo=path_in_repo or os.path.basename(file_path),
26
+ repo_id=repo_id,
27
+ repo_type=repo_type,
28
+ token=token,
29
+ commit_message=commit_message
30
+ )
31
+ return f"βœ… Successfully uploaded file to {repo_id}/{path_in_repo or os.path.basename(file_path)}"
32
+ except Exception as e:
33
+ return f"❌ Error uploading file: {str(e)}"
34
+
35
+ def upload_folder_to_hub(folder_path, repo_id, folder_path_in_repo, token, repo_type, commit_message, ignore_patterns):
36
+ """
37
+ Upload a folder to Hugging Face Hub
38
+ """
39
+ if not folder_path:
40
+ return "❌ Please enter a folder path"
41
+
42
+ if not repo_id:
43
+ return "❌ Please enter a repository ID"
44
+
45
+ if not token:
46
+ return "❌ Please enter your Hugging Face token"
47
+
48
+ if not os.path.isdir(folder_path):
49
+ return f"❌ The path '{folder_path}' is not a valid directory"
50
+
51
+ try:
52
+ # Parse ignore patterns
53
+ ignore_list = None
54
+ if ignore_patterns.strip():
55
+ ignore_list = [pattern.strip() for pattern in ignore_patterns.split(",")]
56
+
57
+ # Upload folder
58
+ api.upload_folder(
59
+ folder_path=folder_path,
60
+ path_in_repo=folder_path_in_repo,
61
+ repo_id=repo_id,
62
+ repo_type=repo_type,
63
+ token=token,
64
+ commit_message=commit_message,
65
+ ignore_patterns=ignore_list
66
+ )
67
+ return f"βœ… Successfully uploaded folder to {repo_id}/{folder_path_in_repo or ''}"
68
+ except Exception as e:
69
+ return f"❌ Error uploading folder: {str(e)}"
70
+
71
+ # Create Gradio interface
72
+ with gr.Blocks(title="Hugging Face Hub Uploader", theme=gr.themes.Soft()) as demo:
73
+ gr.HTML("""
74
+ <h1 style='text-align: center;'>
75
+ πŸ€— Hugging Face Hub Uploader
76
+ <br>
77
+ <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank' style='font-size: 14px; color: #667085;'>Built with anycoder</a>
78
+ </h1>
79
+ """)
80
+
81
+ gr.Markdown("""
82
+ Upload files or folders to your Hugging Face repositories easily.
83
+
84
+ **Get your token from [Hugging Face Settings](https://huggingface.co/settings/tokens)**
85
+ """)
86
+
87
+ with gr.Tab("πŸ“ Upload File"):
88
+ with gr.Row():
89
+ with gr.Column():
90
+ file_input = gr.File(
91
+ label="Select File",
92
+ file_count="single",
93
+ type="filepath"
94
+ )
95
+ repo_id_file = gr.Textbox(
96
+ label="Repository ID",
97
+ placeholder="username/repository-name",
98
+ info="The repository ID (e.g., 'username/my-model')"
99
+ )
100
+ path_in_repo_file = gr.Textbox(
101
+ label="Path in Repository",
102
+ placeholder="README.md",
103
+ info="Where to store the file in the repo. If empty, uses the original filename"
104
+ )
105
+ repo_type_file = gr.Radio(
106
+ choices=["model", "dataset", "space"],
107
+ value="model",
108
+ label="Repository Type"
109
+ )
110
+ commit_message_file = gr.Textbox(
111
+ label="Commit Message (optional)",
112
+ placeholder="Add new file",
113
+ value="Upload file via Gradio app"
114
+ )
115
+ token_file = gr.Textbox(
116
+ label="Hugging Face Token",
117
+ type="password",
118
+ placeholder="hf_...",
119
+ info="Your Hugging Face access token"
120
+ )
121
+ upload_file_btn = gr.Button("Upload File", variant="primary")
122
+
123
+ with gr.Column():
124
+ file_output = gr.Textbox(
125
+ label="Upload Status",
126
+ lines=8,
127
+ interactive=False
128
+ )
129
+
130
+ upload_file_btn.click(
131
+ fn=upload_file_to_hub,
132
+ inputs=[file_input, repo_id_file, path_in_repo_file, token_file, repo_type_file, commit_message_file],
133
+ outputs=file_output
134
+ )
135
+
136
+ with gr.Tab("πŸ“‚ Upload Folder"):
137
+ with gr.Row():
138
+ with gr.Column():
139
+ folder_input = gr.Textbox(
140
+ label="Folder Path",
141
+ placeholder="/path/to/local/folder",
142
+ info="Absolute path to the folder you want to upload"
143
+ )
144
+ repo_id_folder = gr.Textbox(
145
+ label="Repository ID",
146
+ placeholder="username/repository-name",
147
+ info="The repository ID (e.g., 'username/my-dataset')"
148
+ )
149
+ folder_path_in_repo = gr.Textbox(
150
+ label="Folder Path in Repository",
151
+ placeholder="data/",
152
+ info="Where to store the folder in the repo. If empty, uploads to root"
153
+ )
154
+ repo_type_folder = gr.Radio(
155
+ choices=["model", "dataset", "space"],
156
+ value="dataset",
157
+ label="Repository Type"
158
+ )
159
+ commit_message_folder = gr.Textbox(
160
+ label="Commit Message (optional)",
161
+ placeholder="Add new folder",
162
+ value="Upload folder via Gradio app"
163
+ )
164
+ ignore_patterns = gr.Textbox(
165
+ label="Ignore Patterns (optional)",
166
+ placeholder="*.pyc, __pycache__, .git",
167
+ info="Comma-separated patterns to ignore (e.g., '*.pyc, __pycache__')"
168
+ )
169
+ token_folder = gr.Textbox(
170
+ label="Hugging Face Token",
171
+ type="password",
172
+ placeholder="hf_...",
173
+ info="Your Hugging Face access token"
174
+ )
175
+ upload_folder_btn = gr.Button("Upload Folder", variant="primary")
176
+
177
+ with gr.Column():
178
+ folder_output = gr.Textbox(
179
+ label="Upload Status",
180
+ lines=8,
181
+ interactive=False
182
+ )
183
+
184
+ upload_folder_btn.click(
185
+ fn=upload_folder_to_hub,
186
+ inputs=[folder_input, repo_id_folder, folder_path_in_repo, token_folder, repo_type_folder, commit_message_folder, ignore_patterns],
187
+ outputs=folder_output
188
+ )
189
+
190
+ if __name__ == "__main__":
191
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ huggingface_hub