File size: 2,364 Bytes
2338bc5
b8fccd7
 
ffc7490
d4925d2
2338bc5
ffc7490
 
b8fccd7
7932dff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffc7490
7932dff
ffc7490
 
b8fccd7
ffc7490
b8fccd7
 
ffc7490
b8fccd7
ffc7490
 
2338bc5
4843bdd
d4925d2
 
1f259e9
ffc7490
4843bdd
2338bc5
d4925d2
 
2338bc5
 
d4925d2
2338bc5
d4925d2
b8fccd7
1f259e9
d4925d2
4843bdd
 
 
 
2338bc5
d4925d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import shutil
import zipfile
from huggingface_hub import snapshot_download
import gradio as gr

def download_huggingface_repo(repo_url):
    # Extract the repo ID from the URL
    try:
        if "huggingface.co/" not in repo_url:
            raise ValueError("Invalid URL: The provided URL does not belong to Hugging Face.")
        
        repo_id = repo_url.replace("https://huggingface.co/", "").strip()
        
        # Detect repo type (spaces, models, or datasets)
        if repo_id.startswith("spaces/"):
            repo_type = "space"
            repo_id = repo_id.replace("spaces/", "")
        else:
            repo_type = None  # Default type (models or datasets)

        output_dir = "repo"
        zip_file = "repo.zip"
        
        # Clean up previous files
        if os.path.exists(output_dir):
            shutil.rmtree(output_dir)
        if os.path.exists(zip_file):
            os.remove(zip_file)
        
        # Download the repository using Hugging Face Hub
        snapshot_download(repo_id, local_dir=output_dir, repo_type=repo_type)
        
        # Create a zip file of the downloaded repository
        with zipfile.ZipFile(zip_file, "w", zipfile.ZIP_DEFLATED) as zipf:
            for root, _, files in os.walk(output_dir):
                for file in files:
                    file_path = os.path.join(root, file)
                    arcname = os.path.relpath(file_path, output_dir)
                    zipf.write(file_path, arcname)
        
        return zip_file, "Repository downloaded and zipped successfully."
    except Exception as e:
        return None, f"Error: {str(e)}"

# Gradio interface
def handle_download(repo_url):
    file_path, message = download_huggingface_repo(repo_url)
    return file_path, message

with gr.Blocks() as demo:
    gr.Markdown("## Clone and Download Hugging Face Repository")
    
    with gr.Row():
        repo_url_input = gr.Textbox(label="Hugging Face Repo URL", placeholder="Enter the repository URL")
    
    with gr.Row():
        download_button = gr.Button("Clone and Download")
    
    with gr.Row():
        output_file = gr.File(label="Download Zip File")
        output_message = gr.Textbox(label="Status", interactive=False)

    download_button.click(handle_download, inputs=[repo_url_input], outputs=[output_file, output_message])

demo.launch()