soiz1 commited on
Commit
ecd93dc
·
verified ·
1 Parent(s): c4bcd75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -62
app.py CHANGED
@@ -1,68 +1,38 @@
1
- import gradio as gr
2
- import zipfile
3
  import os
4
- import shutil
5
- from datasets import load_dataset
6
- import uuid
7
-
8
- def zip_folder(dataset_name, folder_name, output_zip):
9
- try:
10
- print(f"ロード対象データセット: {dataset_name}")
11
- dataset = load_dataset(dataset_name)
12
- except Exception as e:
13
- return f"データセットをロードできませんでした: {str(e)}"
14
-
15
- if not hasattr(dataset, "save_to_disk"):
16
- return f"データセット '{dataset_name}' は save_to_disk に対応していません。"
17
-
18
- temp_dir = f"temp_dataset_{uuid.uuid4().hex}"
19
- os.makedirs(temp_dir, exist_ok=True)
20
 
21
- try:
22
- dataset.save_to_disk(temp_dir)
23
- folder_path = os.path.join(temp_dir, folder_name)
 
 
 
24
 
25
- if not os.path.exists(folder_path):
26
- available_folders = os.listdir(temp_dir)
27
- return f"指定されたフォルダ '{folder_name}' は見つかりません。利用可能なフォルダ: {available_folders}"
28
 
29
- with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
 
 
 
30
  for root, dirs, files in os.walk(folder_path):
31
  for file in files:
32
- file_path = os.path.join(root, file)
33
- arcname = os.path.relpath(file_path, start=folder_path)
34
- zipf.write(file_path, arcname)
35
-
36
- except Exception as e:
37
- return f"エラーが発生しました: {str(e)}"
38
- finally:
39
- shutil.rmtree(temp_dir)
40
-
41
- return output_zip
42
-
43
- def gradio_interface(dataset_name, folder_name):
44
- output_zip = "output.zip"
45
- result = zip_folder(dataset_name, folder_name, output_zip)
46
-
47
- if not os.path.exists(output_zip):
48
- return result
49
-
50
- return output_zip
51
-
52
- description = "Hugging Faceデータセット内の指定フォルダをZIP形式でダウンロード"
53
- iface = gr.Interface(
54
- fn=gradio_interface,
55
- inputs=[
56
- gr.Textbox(label="Hugging Faceデータセット名", placeholder="例: squad"),
57
- gr.Textbox(label="フォルダ名", placeholder="例: train"),
58
- ],
59
- outputs=[
60
- gr.Textbox(label="結果"), # エラーメッセージ用
61
- gr.File(label="ダウンロード: ZIPファイル"),
62
- ],
63
- title="Hugging FaceフォルダZIP変換",
64
- description=description
65
- )
66
-
67
- if __name__ == "__main__":
68
- iface.launch()
 
 
 
1
  import os
2
+ import zipfile
3
+ from huggingface_hub import hf_hub_download
4
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # Hugging Faceからデータセットをダウンロードする関数
7
+ def download_and_zip(repo_id, folder_name):
8
+ # Hugging Face Hubから対象ファイルをダウンロード
9
+ dataset_path = hf_hub_download(repo_id=repo_id)
10
+
11
+ folder_path = os.path.join(dataset_path, folder_name)
12
 
13
+ # ZIP圧縮の保存先パス
14
+ zip_path = f"{folder_name}.zip"
 
15
 
16
+ # フォルダが存在するかをチェック
17
+ if os.path.exists(folder_path) and os.path.isdir(folder_path):
18
+ # フォルダをZIP圧縮する
19
+ with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
20
  for root, dirs, files in os.walk(folder_path):
21
  for file in files:
22
+ zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), folder_path))
23
+ return zip_path
24
+ else:
25
+ return "指定されたフォルダが見つかりません"
26
+
27
+ # Gradioインターフェースの作成
28
+ def interface(repo_id, folder_name):
29
+ return download_and_zip(repo_id, folder_name)
30
+
31
+ # Gradioインターフェースのセットアップ
32
+ gr.Interface(
33
+ fn=interface,
34
+ inputs=["text", "text"],
35
+ outputs="file",
36
+ live=True,
37
+ description="Hugging Faceのデータセットの指定フォルダをZIPに圧縮します。"
38
+ ).launch()