Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,41 @@
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import zipfile
|
|
|
4 |
import gradio as gr
|
5 |
|
6 |
-
def
|
7 |
-
#
|
8 |
-
|
|
|
|
|
9 |
zip_file = "repo.zip"
|
10 |
-
|
11 |
-
# Clean up
|
12 |
-
if os.path.exists(
|
13 |
-
shutil.rmtree(
|
14 |
if os.path.exists(zip_file):
|
15 |
os.remove(zip_file)
|
16 |
-
|
17 |
try:
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
#
|
22 |
-
if not os.path.exists(clone_dir):
|
23 |
-
return None, "Error: Cloning the repository failed. Please check the URL."
|
24 |
-
|
25 |
-
# Create a zip file from the cloned directory
|
26 |
with zipfile.ZipFile(zip_file, "w", zipfile.ZIP_DEFLATED) as zipf:
|
27 |
-
for root, _, files in os.walk(
|
28 |
for file in files:
|
29 |
file_path = os.path.join(root, file)
|
30 |
-
arcname = os.path.relpath(file_path,
|
31 |
zipf.write(file_path, arcname)
|
32 |
-
|
33 |
-
return zip_file, "Repository
|
34 |
except Exception as e:
|
35 |
return None, f"Error: {str(e)}"
|
36 |
|
37 |
# Gradio interface
|
38 |
def handle_download(repo_url):
|
39 |
-
file_path, message =
|
40 |
return file_path, message
|
41 |
|
42 |
with gr.Blocks() as demo:
|
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import zipfile
|
4 |
+
from huggingface_hub import snapshot_download
|
5 |
import gradio as gr
|
6 |
|
7 |
+
def download_huggingface_repo(repo_url):
|
8 |
+
# Extract the repo ID from the URL
|
9 |
+
repo_id = repo_url.replace("https://huggingface.co/", "").strip()
|
10 |
+
|
11 |
+
output_dir = "repo"
|
12 |
zip_file = "repo.zip"
|
13 |
+
|
14 |
+
# Clean up previous files
|
15 |
+
if os.path.exists(output_dir):
|
16 |
+
shutil.rmtree(output_dir)
|
17 |
if os.path.exists(zip_file):
|
18 |
os.remove(zip_file)
|
19 |
+
|
20 |
try:
|
21 |
+
# Download the repository using Hugging Face Hub
|
22 |
+
snapshot_download(repo_id, local_dir=output_dir)
|
23 |
+
|
24 |
+
# Create a zip file of the downloaded repository
|
|
|
|
|
|
|
|
|
25 |
with zipfile.ZipFile(zip_file, "w", zipfile.ZIP_DEFLATED) as zipf:
|
26 |
+
for root, _, files in os.walk(output_dir):
|
27 |
for file in files:
|
28 |
file_path = os.path.join(root, file)
|
29 |
+
arcname = os.path.relpath(file_path, output_dir)
|
30 |
zipf.write(file_path, arcname)
|
31 |
+
|
32 |
+
return zip_file, "Repository downloaded and zipped successfully."
|
33 |
except Exception as e:
|
34 |
return None, f"Error: {str(e)}"
|
35 |
|
36 |
# Gradio interface
|
37 |
def handle_download(repo_url):
|
38 |
+
file_path, message = download_huggingface_repo(repo_url)
|
39 |
return file_path, message
|
40 |
|
41 |
with gr.Blocks() as demo:
|