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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -1,29 +1,42 @@
1
  import os
2
- import requests
 
3
  import gradio as gr
4
 
5
- def download_and_save_repo(repo_url):
6
- if not repo_url.endswith(".git"):
7
- return None, "Error: The provided URL does not point to a Git repository."
8
-
9
- zip_url = repo_url.replace(".git", "/archive/refs/heads/main.zip")
10
  zip_file = "repo.zip"
11
 
12
- try:
13
- response = requests.get(zip_url, stream=True)
14
- response.raise_for_status()
15
-
16
- with open(zip_file, "wb") as f:
17
- for chunk in response.iter_content(chunk_size=1024):
18
- f.write(chunk)
19
 
20
- return zip_file, "Repository downloaded and zipped successfully."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  except Exception as e:
22
  return None, f"Error: {str(e)}"
23
 
24
  # Gradio interface
25
  def handle_download(repo_url):
26
- file_path, message = download_and_save_repo(repo_url)
27
  return file_path, message
28
 
29
  with gr.Blocks() as demo:
@@ -33,7 +46,7 @@ with gr.Blocks() as demo:
33
  repo_url_input = gr.Textbox(label="Hugging Face Repo URL", placeholder="Enter the repository URL")
34
 
35
  with gr.Row():
36
- download_button = gr.Button("Download Repository as Zip")
37
 
38
  with gr.Row():
39
  output_file = gr.File(label="Download Zip File")
 
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:
 
46
  repo_url_input = gr.Textbox(label="Hugging Face Repo URL", placeholder="Enter the repository URL")
47
 
48
  with gr.Row():
49
+ download_button = gr.Button("Clone and Download")
50
 
51
  with gr.Row():
52
  output_file = gr.File(label="Download Zip File")