File size: 1,891 Bytes
2338bc5
b8fccd7
 
d4925d2
2338bc5
b8fccd7
 
 
1f259e9
4843bdd
b8fccd7
 
 
 
 
d4925d2
b8fccd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2338bc5
4843bdd
d4925d2
 
1f259e9
b8fccd7
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
import os
import shutil
import zipfile
import gradio as gr

def clone_and_zip_with_git(repo_url):
    # Directory and file settings
    clone_dir = "cloned_repo"
    zip_file = "repo.zip"

    # Clean up old directories and files if they exist
    if os.path.exists(clone_dir):
        shutil.rmtree(clone_dir)
    if os.path.exists(zip_file):
        os.remove(zip_file)

    try:
        # Run git clone command
        os.system(f"git clone {repo_url} {clone_dir}")

        # Check if the clone_dir exists
        if not os.path.exists(clone_dir):
            return None, "Error: Cloning the repository failed. Please check the URL."

        # Create a zip file from the cloned directory
        with zipfile.ZipFile(zip_file, "w", zipfile.ZIP_DEFLATED) as zipf:
            for root, _, files in os.walk(clone_dir):
                for file in files:
                    file_path = os.path.join(root, file)
                    arcname = os.path.relpath(file_path, clone_dir)
                    zipf.write(file_path, arcname)

        return zip_file, "Repository cloned and zipped successfully."
    except Exception as e:
        return None, f"Error: {str(e)}"

# Gradio interface
def handle_download(repo_url):
    file_path, message = clone_and_zip_with_git(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()