Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,38 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import zipfile
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
import
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return f"指定されたフォルダ '{folder_name}' は見つかりません。利用可能なフォルダ: {available_folders}"
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
for root, dirs, files in os.walk(folder_path):
|
| 31 |
for file in files:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|