MySafeCode commited on
Commit
69cab8c
Β·
verified Β·
1 Parent(s): a74a49b

Update app.py

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