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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -51
app.py CHANGED
@@ -3,76 +3,52 @@ import zipfile
3
  import os
4
  import shutil
5
  from datasets import load_dataset
 
6
 
7
  def zip_folder(dataset_name, folder_name, output_zip):
8
- """
9
- 指定されたHugging Faceデータセット内のフォルダをZIPに圧縮する。
10
-
11
- Args:
12
- dataset_name (str): データセット名。
13
- folder_name (str): 圧縮するフォルダの名前。
14
- output_zip (str): 出力するZIPファイル名。
15
-
16
- Returns:
17
- str: ZIPファイルのパス。
18
- """
19
  try:
20
- # データセットをロード
21
  print(f"ロード対象データセット: {dataset_name}")
22
  dataset = load_dataset(dataset_name)
23
- except FileNotFoundError:
24
- return f"指定されたデータセット '{dataset_name}' が見つかりません。"
25
  except Exception as e:
26
- return f"エラーが発生しました: {str(e)}"
27
 
28
- # 一時ディレクトリに保存
29
- temp_dir = "temp_dataset"
30
- if os.path.exists(temp_dir):
31
- shutil.rmtree(temp_dir)
32
- os.makedirs(temp_dir)
33
 
34
- # データを保存
35
- dataset.save_to_disk(temp_dir)
36
 
37
- # フォルダパスを指定
38
- folder_path = os.path.join(temp_dir, folder_name)
39
- if not os.path.exists(folder_path):
40
- shutil.rmtree(temp_dir)
41
- return f"指定されたフォルダ {folder_name} が見つかりませんでした。"
 
 
42
 
43
- # ZIPファイルに圧縮
44
- with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
45
- for root, dirs, files in os.walk(folder_path):
46
- for file in files:
47
- file_path = os.path.join(root, file)
48
- arcname = os.path.relpath(file_path, start=folder_path)
49
- zipf.write(file_path, arcname)
50
 
51
- # 一時ディレクトリを削除
52
- shutil.rmtree(temp_dir)
 
 
53
 
54
  return output_zip
55
 
56
  def gradio_interface(dataset_name, folder_name):
57
- """
58
- Gradioインターフェース用関数。
59
-
60
- Args:
61
- dataset_name (str): データセット名。
62
- folder_name (str): フォルダ名。
63
-
64
- Returns:
65
- file: ZIPファイル。
66
- """
67
  output_zip = "output.zip"
68
  result = zip_folder(dataset_name, folder_name, output_zip)
69
 
70
- if not os.path.exists(result):
71
- return f"エラー: {result}"
72
 
73
- return result
74
 
75
- # Gradio UI
76
  description = "Hugging Faceデータセット内の指定フォルダをZIP形式でダウンロード"
77
  iface = gr.Interface(
78
  fn=gradio_interface,
@@ -80,7 +56,10 @@ iface = gr.Interface(
80
  gr.Textbox(label="Hugging Faceデータセット名", placeholder="例: squad"),
81
  gr.Textbox(label="フォルダ名", placeholder="例: train"),
82
  ],
83
- outputs=gr.File(label="ダウンロード: ZIPファイル"),
 
 
 
84
  title="Hugging FaceフォルダZIP変換",
85
  description=description
86
  )
 
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,
 
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
  )