soiz1 commited on
Commit
1f259e9
·
verified ·
1 Parent(s): d4925d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -1,39 +1,33 @@
1
  import os
2
- import shutil
3
- import zipfile
4
  import gradio as gr
5
- from git import Repo
6
 
7
- def clone_and_zip_repo(repo_url):
8
- # Directory settings
9
- clone_dir = "cloned_repo"
10
- zip_file = "repo.zip"
11
 
12
- # Clean up old directories and files if they exist
13
- if os.path.exists(clone_dir):
14
- shutil.rmtree(clone_dir)
15
- if os.path.exists(zip_file):
16
- os.remove(zip_file)
17
 
 
18
  try:
19
- # Clone the repository
20
- Repo.clone_from(repo_url, clone_dir)
 
21
 
22
- # Create a zip file
23
- with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) as zipf:
24
- for root, _, files in os.walk(clone_dir):
25
- for file in files:
26
- file_path = os.path.join(root, file)
27
- arcname = os.path.relpath(file_path, clone_dir)
28
- zipf.write(file_path, arcname)
29
 
30
  return zip_file
31
  except Exception as e:
32
  return f"Error: {str(e)}"
33
 
34
  # Gradio interface
35
- def download_repo(repo_url):
36
- result = clone_and_zip_repo(repo_url)
37
  if result.endswith(".zip"):
38
  return result
39
  else:
@@ -46,12 +40,12 @@ 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 = gr.File(label="Download Zip File")
53
-
54
- download_button.click(download_repo, inputs=[repo_url_input], outputs=[output])
55
 
56
  # Run the Gradio app
57
  demo.launch()
 
1
  import os
2
+ import requests
 
3
  import gradio as gr
 
4
 
5
+ def download_and_save_repo(repo_url):
6
+ # 対応するzip URLを生成(GitHubの場合に応用可能)
7
+ if not repo_url.endswith(".git"):
8
+ return "Error: The provided URL does not point to a Git repository."
9
 
10
+ # .git を削除してzip形式のURLに変換
11
+ zip_url = repo_url.replace(".git", "/archive/refs/heads/main.zip")
 
 
 
12
 
13
+ zip_file = "repo.zip"
14
  try:
15
+ # リポジトリのzipファイルをダウンロード
16
+ response = requests.get(zip_url, stream=True)
17
+ response.raise_for_status() # HTTPエラーをチェック
18
 
19
+ # ダウンロードしたファイルを保存
20
+ with open(zip_file, "wb") as f:
21
+ for chunk in response.iter_content(chunk_size=1024):
22
+ f.write(chunk)
 
 
 
23
 
24
  return zip_file
25
  except Exception as e:
26
  return f"Error: {str(e)}"
27
 
28
  # Gradio interface
29
+ def handle_download(repo_url):
30
+ result = download_and_save_repo(repo_url)
31
  if result.endswith(".zip"):
32
  return result
33
  else:
 
40
  repo_url_input = gr.Textbox(label="Hugging Face Repo URL", placeholder="Enter the repository URL")
41
 
42
  with gr.Row():
43
+ download_button = gr.Button("Download Repository as Zip")
44
+
45
  with gr.Row():
46
  output = gr.File(label="Download Zip File")
47
+
48
+ download_button.click(handle_download, inputs=[repo_url_input], outputs=[output])
49
 
50
  # Run the Gradio app
51
  demo.launch()