Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- gradio_hf_push_app_http.py +62 -0
gradio_hf_push_app_http.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import HfApi
|
4 |
+
import os
|
5 |
+
import shutil
|
6 |
+
|
7 |
+
# Initialize Hugging Face API
|
8 |
+
api = HfApi()
|
9 |
+
|
10 |
+
# Function to clean directory if exists and recreate it
|
11 |
+
def clean_directory(directory):
|
12 |
+
if os.path.exists(directory):
|
13 |
+
shutil.rmtree(directory) # Remove the entire directory
|
14 |
+
os.makedirs(directory) # Recreate an empty directory
|
15 |
+
|
16 |
+
# Function to upload and push to Hugging Face
|
17 |
+
def push_to_huggingface(repo_name, file_or_directory, hf_token):
|
18 |
+
# Ensure correct repo name format
|
19 |
+
if not "/" in repo_name:
|
20 |
+
repo_name = f"{hf_token.split(':')[0]}/{repo_name}" # Assume the namespace is the user's namespace
|
21 |
+
|
22 |
+
# Step 1: Handle directories or files
|
23 |
+
target_dir = f"./{repo_name}"
|
24 |
+
if os.path.isdir(file_or_directory):
|
25 |
+
shutil.copytree(file_or_directory, target_dir, dirs_exist_ok=True)
|
26 |
+
else:
|
27 |
+
os.makedirs(target_dir, exist_ok=True)
|
28 |
+
shutil.copy(file_or_directory.name, os.path.join(target_dir, os.path.basename(file_or_directory.name)))
|
29 |
+
|
30 |
+
# Step 2: Check if the repository already exists, create if not
|
31 |
+
try:
|
32 |
+
api.create_repo(repo_name, token=hf_token)
|
33 |
+
except Exception as e:
|
34 |
+
if "already created" in str(e):
|
35 |
+
print(f"Repository '{repo_name}' already exists, skipping creation.")
|
36 |
+
else:
|
37 |
+
raise e
|
38 |
+
|
39 |
+
# Step 3: Push files using HTTP-based upload
|
40 |
+
if os.path.isdir(target_dir):
|
41 |
+
api.upload_folder(folder_path=target_dir, path_in_repo="", repo_id=repo_name, token=hf_token)
|
42 |
+
else:
|
43 |
+
api.upload_file(path_or_fileobj=target_dir, path_in_repo=os.path.basename(file_or_directory.name), repo_id=repo_name, token=hf_token)
|
44 |
+
|
45 |
+
return f"Repository '{repo_name}' has been pushed to Hugging Face Hub successfully."
|
46 |
+
|
47 |
+
# Gradio Interface
|
48 |
+
interface = gr.Interface(
|
49 |
+
fn=push_to_huggingface,
|
50 |
+
inputs=[
|
51 |
+
"text", # Repository name
|
52 |
+
"file", # File or directory
|
53 |
+
"text" # Hugging Face Token
|
54 |
+
],
|
55 |
+
outputs="text",
|
56 |
+
title="Push File or Directory to Hugging Face",
|
57 |
+
description="Upload any file or directory and push it to the Hugging Face Hub."
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the interface
|
61 |
+
if __name__ == "__main__":
|
62 |
+
interface.launch()
|