Upload folder using huggingface_hub
Browse files- gradio_hf_push_app.py +41 -0
gradio_hf_push_app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import zipfile
|
| 5 |
+
from huggingface_hub import HfApi, Repository
|
| 6 |
+
|
| 7 |
+
# Initialize Hugging Face API
|
| 8 |
+
api = HfApi()
|
| 9 |
+
|
| 10 |
+
# Function to upload and push to Hugging Face
|
| 11 |
+
def push_to_huggingface(repo_name, zip_file, hf_token):
|
| 12 |
+
# Step 1: Unzip the directory
|
| 13 |
+
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
|
| 14 |
+
unzip_dir = f"./{repo_name}"
|
| 15 |
+
zip_ref.extractall(unzip_dir)
|
| 16 |
+
|
| 17 |
+
# Step 2: Create repository on Hugging Face
|
| 18 |
+
api.create_repo(repo_name, token=hf_token)
|
| 19 |
+
|
| 20 |
+
# Step 3: Push the unzipped directory to Hugging Face
|
| 21 |
+
repo = Repository(local_dir=unzip_dir, clone_from=f"{hf_token}/{repo_name}")
|
| 22 |
+
repo.push_to_hub()
|
| 23 |
+
|
| 24 |
+
return f"Repository '{repo_name}' has been pushed to Hugging Face Hub successfully."
|
| 25 |
+
|
| 26 |
+
# Gradio Interface
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=push_to_huggingface,
|
| 29 |
+
inputs=[
|
| 30 |
+
"text", # Repository name
|
| 31 |
+
"file", # Zipped directory
|
| 32 |
+
"text" # Hugging Face Token
|
| 33 |
+
],
|
| 34 |
+
outputs="text",
|
| 35 |
+
title="Push Directory to Hugging Face",
|
| 36 |
+
description="Upload a zipped directory and push it to the Hugging Face Hub."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Launch the interface
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
interface.launch()
|