File size: 1,768 Bytes
b0281cb
ecd93dc
949f5b0
ecd93dc
b0281cb
949f5b0
ecd93dc
d0045c2
949f5b0
 
 
 
 
d0045c2
949f5b0
 
c4bcd75
949f5b0
 
b0281cb
949f5b0
 
 
 
 
 
 
 
 
 
 
 
ecd93dc
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import zipfile
import datasets
import gradio as gr

# Hugging Faceのデータセットをダウンロードして指定されたフォルダをZIP圧縮する関数
def download_and_zip(repo_id, folder_name):
    try:
        # データセットをHugging Face Hubからダウンロード
        dataset = datasets.load_dataset(repo_id)

        # ダウンロードされたデータセットのパスを取得
        dataset_path = dataset.data_files['train']  # 'train' データセットを例にします。必要に応じて変更

        # フォルダのパス
        folder_path = os.path.join(dataset_path, folder_name)

        # ZIP圧縮の保存先パス
        zip_path = f"{folder_name}.zip"

        # フォルダが存在するかをチェック
        if os.path.exists(folder_path) and os.path.isdir(folder_path):
            # フォルダをZIP圧縮する
            with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
                for root, dirs, files in os.walk(folder_path):
                    for file in files:
                        zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), folder_path))
            return zip_path
        else:
            return "指定されたフォルダが見つかりません"
    except Exception as e:
        return f"エラー: {str(e)}"

# Gradioインターフェースの作成
def interface(repo_id, folder_name):
    return download_and_zip(repo_id, folder_name)

# Gradioインターフェースのセットアップ
gr.Interface(
    fn=interface,
    inputs=["text", "text"],
    outputs="file",
    live=True,
    description="Hugging Faceのデータセットの指定フォルダをZIPに圧縮します。"
).launch()