soiz1 commited on
Commit
ffc7490
·
verified ·
1 Parent(s): b8fccd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -1,42 +1,41 @@
1
  import os
2
  import shutil
3
  import zipfile
 
4
  import gradio as gr
5
 
6
- def clone_and_zip_with_git(repo_url):
7
- # Directory and file settings
8
- clone_dir = "cloned_repo"
 
 
9
  zip_file = "repo.zip"
10
-
11
- # Clean up old directories and files if they exist
12
- if os.path.exists(clone_dir):
13
- shutil.rmtree(clone_dir)
14
  if os.path.exists(zip_file):
15
  os.remove(zip_file)
16
-
17
  try:
18
- # Run git clone command
19
- os.system(f"git clone {repo_url} {clone_dir}")
20
-
21
- # Check if the clone_dir exists
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(clone_dir):
28
  for file in files:
29
  file_path = os.path.join(root, file)
30
- arcname = os.path.relpath(file_path, clone_dir)
31
  zipf.write(file_path, arcname)
32
-
33
- return zip_file, "Repository cloned and zipped successfully."
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 = clone_and_zip_with_git(repo_url)
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: