Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
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 |
+
# データセットをロード
|
20 |
+
dataset = load_dataset(dataset_name)
|
21 |
+
|
22 |
+
# 一時ディレクトリに保存
|
23 |
+
temp_dir = "temp_dataset"
|
24 |
+
if os.path.exists(temp_dir):
|
25 |
+
shutil.rmtree(temp_dir)
|
26 |
+
os.makedirs(temp_dir)
|
27 |
+
|
28 |
+
# データを保存
|
29 |
+
dataset.save_to_disk(temp_dir)
|
30 |
+
|
31 |
+
# フォルダパスを指定
|
32 |
+
folder_path = os.path.join(temp_dir, folder_name)
|
33 |
+
if not os.path.exists(folder_path):
|
34 |
+
return f"指定されたフォルダ {folder_name} が見つかりませんでした。"
|
35 |
+
|
36 |
+
# ZIPファイルに圧縮
|
37 |
+
with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
38 |
+
for root, dirs, files in os.walk(folder_path):
|
39 |
+
for file in files:
|
40 |
+
file_path = os.path.join(root, file)
|
41 |
+
arcname = os.path.relpath(file_path, start=folder_path)
|
42 |
+
zipf.write(file_path, arcname)
|
43 |
+
|
44 |
+
# 一時ディレクトリを削除
|
45 |
+
shutil.rmtree(temp_dir)
|
46 |
+
|
47 |
+
return output_zip
|
48 |
+
|
49 |
+
def gradio_interface(dataset_name, folder_name):
|
50 |
+
"""
|
51 |
+
Gradioインターフェース用関数。
|
52 |
+
|
53 |
+
Args:
|
54 |
+
dataset_name (str): データセット名。
|
55 |
+
folder_name (str): フォルダ名。
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
file: ZIPファイル。
|
59 |
+
"""
|
60 |
+
output_zip = "output.zip"
|
61 |
+
result = zip_folder(dataset_name, folder_name, output_zip)
|
62 |
+
|
63 |
+
if not os.path.exists(result):
|
64 |
+
return f"エラー: {result}"
|
65 |
+
|
66 |
+
return result
|
67 |
+
|
68 |
+
# Gradio UI
|
69 |
+
description = "Hugging Faceデータセット内の指定フォルダをZIP形式でダウンロード"
|
70 |
+
iface = gr.Interface(
|
71 |
+
fn=gradio_interface,
|
72 |
+
inputs=[
|
73 |
+
gr.Textbox(label="Hugging Faceデータセット名", placeholder="例: squad"),
|
74 |
+
gr.Textbox(label="フォルダ名", placeholder="例: train"),
|
75 |
+
],
|
76 |
+
outputs=gr.File(label="ダウンロード: ZIPファイル"),
|
77 |
+
title="Hugging FaceフォルダZIP変換",
|
78 |
+
description=description
|
79 |
+
)
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
iface.launch()
|