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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -31
app.py CHANGED
@@ -1,43 +1,57 @@
1
- import gradio as gr
2
  import os
3
  import shutil
4
- import tempfile
 
5
  from git import Repo
6
 
7
- def download_and_zip_repo(huggingface_url):
 
 
 
 
 
 
 
 
 
 
8
  try:
9
- # 一時ディレクトリを作成
10
- with tempfile.TemporaryDirectory() as temp_dir:
11
- # リポジトリをクローン
12
- Repo.clone_from(huggingface_url, temp_dir)
13
-
14
- # ZIPファイルのパスを指定
15
- zip_path = os.path.join(temp_dir, "repository.zip")
16
-
17
- # クローンしたリポジトリをZIP形式で圧縮
18
- shutil.make_archive(zip_path.replace(".zip", ""), 'zip', temp_dir)
19
-
20
- # 圧縮したZIPファイルを返す
21
- return zip_path
22
  except Exception as e:
23
- return str(e)
 
 
 
 
 
 
 
 
24
 
25
- # Gradioインターフェースの定義
26
- with gr.Blocks() as app:
27
- gr.Markdown("## Hugging Face リポジトリ ダウンローダー")
28
- gr.Markdown("Hugging FaceのリポジトリURLを入力して、ZIPファイルをダウンロードできます。")
29
 
30
  with gr.Row():
31
- input_url = gr.Textbox(label="Hugging Face リポジトリのURLを入力")
32
- download_button = gr.Button("ダウンロード")
33
 
34
- output_file = gr.File(label="ダウンロードされたZIPファイル")
 
 
 
 
35
 
36
- download_button.click(
37
- fn=download_and_zip_repo,
38
- inputs=input_url,
39
- outputs=output_file
40
- )
41
 
42
- # アプリを起動
43
- app.launch()
 
 
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:
40
+ return result
41
 
42
+ with gr.Blocks() as demo:
43
+ gr.Markdown("## Clone and Download Hugging Face Repository")
 
 
44
 
45
  with gr.Row():
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()