NeoPy commited on
Commit
d8aa317
·
verified ·
1 Parent(s): 297c557

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +247 -31
app.py CHANGED
@@ -3,46 +3,262 @@ from huggingface_hub import HfApi
3
  from git import Repo
4
  import uuid
5
  from slugify import slugify
 
 
6
 
7
- def clone(
8
  profile: gr.OAuthProfile,
9
  oauth_token: gr.OAuthToken,
10
- repo_git,
11
- repo_hf,
12
- sdk_type
 
13
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  folder = str(uuid.uuid4())
15
- cloned_repo = Repo.clone_from(
16
- repo_git,
17
- folder
18
- )
19
-
20
- #Upload to HF
21
- api = HfApi(token=oauth_token.token)
22
- api.create_repo(
23
- f"{profile.username}/{slugify(repo_hf)}",
24
- repo_type="space",
25
- space_sdk=sdk_type
26
- )
27
- api.upload_folder(
28
- folder_path=folder,
29
- repo_id=f"{profile.username}/{slugify(repo_hf)}",
30
- repo_type="space",
31
- )
32
- return f"https://huggingface.co/spaces/{profile.username}/{slugify(repo_hf)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- with gr.Blocks() as demo:
 
 
 
 
36
  gr.LoginButton()
 
37
  with gr.Row():
38
- btn = gr.Button("Bring over!")
39
- with gr.Column():
40
- repo_git = gr.Textbox(label="GitHub Repository")
41
- repo_hf = gr.Textbox(label="Hugging Face Space name")
42
- sdk_choices = gr.Radio(["gradio", "streamlit", "docker", "static"], label="SDK Choices")
43
- output = gr.Textbox(label="Output repo")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- btn.click(fn=clone, inputs=[repo_git, repo_hf, sdk_choices], outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- demo.launch(theme="NeoPy/Soft")
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from git import Repo
4
  import uuid
5
  from slugify import slugify
6
+ import os
7
+ import shutil
8
 
9
+ def clone_and_upload(
10
  profile: gr.OAuthProfile,
11
  oauth_token: gr.OAuthToken,
12
+ repo_git: str,
13
+ repo_type: str,
14
+ repo_hf: str,
15
+ sdk_type: str = None
16
  ):
17
+ """
18
+ Clone a Git repository and upload it to Hugging Face as a specified type.
19
+
20
+ Args:
21
+ profile: Gradio OAuth profile with user info
22
+ oauth_token: OAuth token for authentication
23
+ repo_git: Git repository URL to clone
24
+ repo_type: Type of repository to create on HF (space, model, dataset)
25
+ repo_hf: Name for the Hugging Face repository
26
+ sdk_type: SDK type for spaces (gradio, streamlit, docker, static)
27
+
28
+ Returns:
29
+ URL of the created Hugging Face repository
30
+ """
31
  folder = str(uuid.uuid4())
32
+
33
+ try:
34
+ # Clone the Git repository
35
+ cloned_repo = Repo.clone_from(repo_git, folder)
36
+
37
+ # Initialize API with token
38
+ api = HfApi(token=oauth_token.token)
39
+
40
+ # Create slugified repository name
41
+ repo_name = slugify(repo_hf)
42
+ repo_id = f"{profile.username}/{repo_name}"
43
+
44
+ # Prepare repository creation parameters
45
+ create_kwargs = {
46
+ "repo_id": repo_id,
47
+ "repo_type": repo_type,
48
+ }
49
+
50
+ # Add space_sdk only for spaces
51
+ if repo_type == "space" and sdk_type:
52
+ create_kwargs["space_sdk"] = sdk_type
53
+
54
+ # Create repository on Hugging Face
55
+ api.create_repo(**create_kwargs)
56
+
57
+ # Upload the folder content
58
+ if repo_type == "dataset":
59
+ # For datasets, we typically want to preserve the folder structure
60
+ api.upload_folder(
61
+ folder_path=folder,
62
+ repo_id=repo_id,
63
+ repo_type=repo_type,
64
+ commit_message="Initial upload from Git repository"
65
+ )
66
+ else:
67
+ # For models and spaces, upload the entire folder
68
+ api.upload_folder(
69
+ folder_path=folder,
70
+ repo_id=repo_id,
71
+ repo_type=repo_type,
72
+ commit_message="Initial upload from Git repository"
73
+ )
74
+
75
+ # Return appropriate URL based on repo type
76
+ if repo_type == "space":
77
+ return f"https://huggingface.co/spaces/{profile.username}/{repo_name}"
78
+ elif repo_type == "model":
79
+ return f"https://huggingface.co/{profile.username}/{repo_name}"
80
+ elif repo_type == "dataset":
81
+ return f"https://huggingface.co/datasets/{profile.username}/{repo_name}"
82
+ else:
83
+ return f"https://huggingface.co/{profile.username}/{repo_name}"
84
+
85
+ except Exception as e:
86
+ return f"Error: {str(e)}"
87
+ finally:
88
+ # Clean up the cloned directory
89
+ if os.path.exists(folder):
90
+ shutil.rmtree(folder)
91
 
92
+ def toggle_sdk_visibility(repo_type):
93
+ """
94
+ Toggle visibility of SDK choices based on repo type.
95
+
96
+ Args:
97
+ repo_type: Selected repository type
98
+
99
+ Returns:
100
+ Gradio update object for SDK choices visibility
101
+ """
102
+ if repo_type == "space":
103
+ return gr.Radio(visible=True, interactive=True)
104
+ else:
105
+ return gr.Radio(visible=False, interactive=False)
106
 
107
+ with gr.Blocks(theme="NeoPy/Soft") as demo:
108
+ gr.Markdown("# 🚀 Multi-Type Repository Importer")
109
+ gr.Markdown("Clone Git repositories and upload them to Hugging Face as Spaces, Models, or Datasets")
110
+
111
+ # Authentication
112
  gr.LoginButton()
113
+
114
  with gr.Row():
115
+ with gr.Column(scale=2):
116
+ # Input section
117
+ gr.Markdown("## Input Configuration")
118
+
119
+ repo_git = gr.Textbox(
120
+ label="Git Repository URL",
121
+ placeholder="https://github.com/username/repository.git",
122
+ info="Enter the Git repository URL to clone"
123
+ )
124
+
125
+ repo_type = gr.Dropdown(
126
+ choices=["space", "model", "dataset"],
127
+ value="space",
128
+ label="Hugging Face Repository Type",
129
+ info="Select the type of repository to create on Hugging Face"
130
+ )
131
+
132
+ repo_hf = gr.Textbox(
133
+ label="Hugging Face Repository Name",
134
+ placeholder="my-awesome-project",
135
+ info="Choose a name for your Hugging Face repository"
136
+ )
137
+
138
+ # Conditional SDK selection (only for spaces)
139
+ sdk_choices = gr.Radio(
140
+ choices=["gradio", "streamlit", "docker", "static"],
141
+ value="gradio",
142
+ label="Space SDK",
143
+ info="Select the SDK for your Space (only applicable for Spaces)",
144
+ visible=True
145
+ )
146
+
147
+ # Update SDK visibility based on repo type
148
+ repo_type.change(
149
+ fn=toggle_sdk_visibility,
150
+ inputs=repo_type,
151
+ outputs=sdk_choices
152
+ )
153
+
154
+ # Submit button
155
+ submit_btn = gr.Button(
156
+ "🚀 Clone & Upload to Hugging Face",
157
+ variant="primary",
158
+ size="lg"
159
+ )
160
+
161
+ with gr.Column(scale=1):
162
+ # Info section
163
+ gr.Markdown("## 📚 Repository Type Guide")
164
+
165
+ with gr.Accordion("Spaces", open=False):
166
+ gr.Markdown("""
167
+ **Spaces** are for hosting ML demos and apps.
168
+ - Requires SDK (Gradio, Streamlit, Docker, or static)
169
+ - Automatically deploys your application
170
+ - Public URL for sharing demos
171
+ """)
172
+
173
+ with gr.Accordion("Models", open=False):
174
+ gr.Markdown("""
175
+ **Models** are for hosting ML models.
176
+ - Use for model weights and configurations
177
+ - Compatible with Hugging Face Transformers
178
+ - Version control for model files
179
+ """)
180
+
181
+ with gr.Accordion("Datasets", open=False):
182
+ gr.Markdown("""
183
+ **Datasets** are for hosting data collections.
184
+ - Store and version datasets
185
+ - Use with Hugging Face Datasets library
186
+ - Share datasets with the community
187
+ """)
188
 
189
+ # Output section
190
+ gr.Markdown("## Output")
191
+ output = gr.Textbox(
192
+ label="Result",
193
+ placeholder="Your Hugging Face repository URL will appear here...",
194
+ interactive=False
195
+ )
196
+
197
+ # Status tracking
198
+ status = gr.Textbox(
199
+ label="Status",
200
+ value="Ready to import",
201
+ interactive=False,
202
+ visible=True
203
+ )
204
+
205
+ def process_upload(profile, oauth_token, repo_git, repo_type, repo_hf, sdk_type):
206
+ if not all([repo_git, repo_hf]):
207
+ return "Please fill in all required fields", "Error: Missing required fields"
208
+
209
+ try:
210
+ status_update = f"Cloning {repo_git}..."
211
+ yield status_update, ""
212
+
213
+ result = clone_and_upload(
214
+ profile=profile,
215
+ oauth_token=oauth_token,
216
+ repo_git=repo_git,
217
+ repo_type=repo_type,
218
+ repo_hf=repo_hf,
219
+ sdk_type=sdk_type
220
+ )
221
+
222
+ if result.startswith("Error:"):
223
+ return result, f"Failed: {result}"
224
+ else:
225
+ return result, f"✅ Successfully created! Repository is being processed..."
226
+
227
+ except Exception as e:
228
+ return f"Error: {str(e)}", f"Failed: {str(e)}"
229
+
230
+ # Connect the button click event
231
+ submit_btn.click(
232
+ fn=process_upload,
233
+ inputs=[gr.State(), gr.State(), repo_git, repo_type, repo_hf, sdk_choices],
234
+ outputs=[output, status]
235
+ )
236
+
237
+ # Examples section
238
+ gr.Markdown("## 💡 Examples")
239
 
240
+ with gr.Row():
241
+ with gr.Column():
242
+ gr.Examples(
243
+ examples=[
244
+ ["https://github.com/gradio-app/gradio.git", "space", "gradio-demo", "gradio"],
245
+ ["https://github.com/huggingface/transformers.git", "model", "my-transformers", ""],
246
+ ["https://github.com/huggingface/datasets.git", "dataset", "my-dataset", ""]
247
+ ],
248
+ inputs=[repo_git, repo_type, repo_hf, sdk_choices],
249
+ label="Quick Examples"
250
+ )
251
 
252
+ # Footer
253
+ gr.Markdown("---")
254
+ gr.Markdown(
255
+ """
256
+ <div style='text-align: center'>
257
+ <p>This tool helps you import Git repositories to Hugging Face Hub</p>
258
+ <p>Supports: Spaces (with various SDKs), Models, and Datasets</p>
259
+ </div>
260
+ """
261
+ )
262
+
263
+ if __name__ == "__main__":
264
+ demo.launch()