Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import re
|
| 3 |
import requests
|
| 4 |
import gradio as gr
|
|
@@ -74,21 +75,33 @@ def list_files_with_no(repo_type):
|
|
| 74 |
except Exception as e:
|
| 75 |
return [[f"エラー: ファイルリストの取得に失敗しました: {e}", None]]
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
def
|
| 79 |
try:
|
| 80 |
target_repo = MODEL_REPO if repo_type == "model" else LORA_REPO
|
| 81 |
if not target_repo:
|
| 82 |
return "エラー: リポジトリが設定されていません。"
|
| 83 |
|
| 84 |
-
#
|
| 85 |
reordered_files = sorted(data, key=lambda x: int(x[0])) # No.列の順序に基づいて並び替え
|
| 86 |
-
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
return data, f"リポジトリ更新中にエラーが発生しました: {e}"
|
| 94 |
|
|
@@ -111,12 +124,12 @@ with gr.Blocks() as demo:
|
|
| 111 |
load_button.click(list_files_with_no, inputs=repo_choice, outputs=file_list_output)
|
| 112 |
|
| 113 |
# 並び替えセクション
|
| 114 |
-
gr.Markdown("### No
|
| 115 |
reorder_button = gr.Button("並び替え適用")
|
| 116 |
sorted_table = gr.Dataframe(headers=["No.", "Filename"], datatype=["number", "str"], interactive=False)
|
| 117 |
|
| 118 |
def apply_reorder(data, repo_type):
|
| 119 |
-
reordered_data, message =
|
| 120 |
return reordered_data, message
|
| 121 |
|
| 122 |
reorder_button.click(apply_reorder, inputs=[file_list_output, repo_choice], outputs=[sorted_table, output])
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
import re
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
return [[f"エラー: ファイルリストの取得に失敗しました: {e}", None]]
|
| 77 |
|
| 78 |
+
# 並び替え結果をリポジトリにメタデータとして保存
|
| 79 |
+
def save_reorder_to_repo(data, repo_type):
|
| 80 |
try:
|
| 81 |
target_repo = MODEL_REPO if repo_type == "model" else LORA_REPO
|
| 82 |
if not target_repo:
|
| 83 |
return "エラー: リポジトリが設定されていません。"
|
| 84 |
|
| 85 |
+
# 並び替えられたファイルリストをJSONとして保存
|
| 86 |
reordered_files = sorted(data, key=lambda x: int(x[0])) # No.列の順序に基づいて並び替え
|
| 87 |
+
metadata = {"order": [{"No": file[0], "Filename": file[1]} for file in reordered_files]}
|
| 88 |
|
| 89 |
+
# JSONファイルに保存
|
| 90 |
+
metadata_filename = "file_order.json"
|
| 91 |
+
with open(metadata_filename, "w") as f:
|
| 92 |
+
json.dump(metadata, f, indent=4)
|
| 93 |
+
|
| 94 |
+
# JSONファイルをリポジトリにアップロード
|
| 95 |
+
api.upload_file(
|
| 96 |
+
path_or_fileobj=metadata_filename,
|
| 97 |
+
path_in_repo=metadata_filename,
|
| 98 |
+
repo_id=target_repo,
|
| 99 |
+
token=HF_TOKEN,
|
| 100 |
+
repo_type="dataset",
|
| 101 |
+
commit_message="Update file order metadata"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
return reordered_files, f"ファイル順序がリポジトリに反映されました ({metadata_filename})"
|
| 105 |
except Exception as e:
|
| 106 |
return data, f"リポジトリ更新中にエラーが発生しました: {e}"
|
| 107 |
|
|
|
|
| 124 |
load_button.click(list_files_with_no, inputs=repo_choice, outputs=file_list_output)
|
| 125 |
|
| 126 |
# 並び替えセクション
|
| 127 |
+
gr.Markdown("### No.列を変更して並び替えを適用し、リポジトリに保存")
|
| 128 |
reorder_button = gr.Button("並び替え適用")
|
| 129 |
sorted_table = gr.Dataframe(headers=["No.", "Filename"], datatype=["number", "str"], interactive=False)
|
| 130 |
|
| 131 |
def apply_reorder(data, repo_type):
|
| 132 |
+
reordered_data, message = save_reorder_to_repo(data, repo_type)
|
| 133 |
return reordered_data, message
|
| 134 |
|
| 135 |
reorder_button.click(apply_reorder, inputs=[file_list_output, repo_choice], outputs=[sorted_table, output])
|