Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,42 +13,32 @@ CIVITAI_TOKEN = os.environ.get("CIVITAI_TOKEN", None) # Civitai用トークン
|
|
| 13 |
api = HfApi()
|
| 14 |
|
| 15 |
def download_and_upload(url, repo_type):
|
| 16 |
-
# 入力チェック
|
| 17 |
if not url or not repo_type:
|
| 18 |
return "URLまたはタイプが未指定です。"
|
| 19 |
|
| 20 |
-
# Civitai API用ヘッダ
|
| 21 |
headers = {}
|
| 22 |
if CIVITAI_TOKEN:
|
| 23 |
headers["Authorization"] = f"Bearer {CIVITAI_TOKEN}"
|
| 24 |
|
| 25 |
-
# ファイルダウンロード実行
|
| 26 |
-
# CivitaiのAPIからモデルを取得
|
| 27 |
try:
|
| 28 |
response = requests.get(url, headers=headers)
|
| 29 |
-
response.raise_for_status()
|
| 30 |
except Exception as e:
|
| 31 |
return f"ファイルのダウンロードに失敗しました: {e}"
|
| 32 |
|
| 33 |
-
# Content-Dispositionからfilenameを抽出
|
| 34 |
-
# 例: Content-Disposition: attachment; filename="model.safetensors"
|
| 35 |
content_disp = response.headers.get("content-disposition", "")
|
| 36 |
filename = None
|
| 37 |
if content_disp:
|
| 38 |
-
# filename="..." という部分を正規表現で抽出
|
| 39 |
match = re.search(r'filename="?([^"]+)"?', content_disp)
|
| 40 |
if match:
|
| 41 |
filename = match.group(1)
|
| 42 |
|
| 43 |
if not filename:
|
| 44 |
-
# ファイル名がヘッダに無い場合のfallback
|
| 45 |
filename = "downloaded_file.bin"
|
| 46 |
|
| 47 |
-
# ダウンロードファイルをローカル一時ファイルに保存
|
| 48 |
with open(filename, "wb") as f:
|
| 49 |
f.write(response.content)
|
| 50 |
|
| 51 |
-
# 選択されたリポジトリを決定
|
| 52 |
if repo_type == "model":
|
| 53 |
target_repo = MODEL_REPO
|
| 54 |
else:
|
|
@@ -57,24 +47,50 @@ def download_and_upload(url, repo_type):
|
|
| 57 |
if not target_repo:
|
| 58 |
return f"{repo_type}用リポジトリが環境変数で設定されていません。"
|
| 59 |
|
| 60 |
-
# Hugging Face Hub にアップロード
|
| 61 |
try:
|
| 62 |
api.upload_file(
|
| 63 |
path_or_fileobj=filename,
|
| 64 |
path_in_repo=filename,
|
| 65 |
repo_id=target_repo,
|
| 66 |
token=HF_TOKEN,
|
| 67 |
-
repo_type="dataset",
|
| 68 |
commit_message=f"Add {filename} from {url}"
|
| 69 |
)
|
| 70 |
return f"ファイル '{filename}' を '{target_repo}' にアップロードしました。"
|
| 71 |
except Exception as e:
|
| 72 |
return f"アップロード中にエラーが発生しました: {e}"
|
| 73 |
-
finally:
|
| 74 |
-
# 必要ならローカルファイル削除などの後処理
|
| 75 |
-
pass
|
| 76 |
|
| 77 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
gr.Markdown("## Civitai経由ファイルダウンロード&アップロードツール")
|
| 80 |
gr.Markdown("Civitai APIのURLを指定して、'model'または'lora'リポジトリへアップロードします。")
|
|
@@ -86,5 +102,26 @@ with gr.Blocks() as demo:
|
|
| 86 |
|
| 87 |
run_button.click(download_and_upload, inputs=[url_input, repo_choice], outputs=output)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
if __name__ == "__main__":
|
| 90 |
demo.launch()
|
|
|
|
| 13 |
api = HfApi()
|
| 14 |
|
| 15 |
def download_and_upload(url, repo_type):
|
|
|
|
| 16 |
if not url or not repo_type:
|
| 17 |
return "URLまたはタイプが未指定です。"
|
| 18 |
|
|
|
|
| 19 |
headers = {}
|
| 20 |
if CIVITAI_TOKEN:
|
| 21 |
headers["Authorization"] = f"Bearer {CIVITAI_TOKEN}"
|
| 22 |
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
response = requests.get(url, headers=headers)
|
| 25 |
+
response.raise_for_status()
|
| 26 |
except Exception as e:
|
| 27 |
return f"ファイルのダウンロードに失敗しました: {e}"
|
| 28 |
|
|
|
|
|
|
|
| 29 |
content_disp = response.headers.get("content-disposition", "")
|
| 30 |
filename = None
|
| 31 |
if content_disp:
|
|
|
|
| 32 |
match = re.search(r'filename="?([^"]+)"?', content_disp)
|
| 33 |
if match:
|
| 34 |
filename = match.group(1)
|
| 35 |
|
| 36 |
if not filename:
|
|
|
|
| 37 |
filename = "downloaded_file.bin"
|
| 38 |
|
|
|
|
| 39 |
with open(filename, "wb") as f:
|
| 40 |
f.write(response.content)
|
| 41 |
|
|
|
|
| 42 |
if repo_type == "model":
|
| 43 |
target_repo = MODEL_REPO
|
| 44 |
else:
|
|
|
|
| 47 |
if not target_repo:
|
| 48 |
return f"{repo_type}用リポジトリが環境変数で設定されていません。"
|
| 49 |
|
|
|
|
| 50 |
try:
|
| 51 |
api.upload_file(
|
| 52 |
path_or_fileobj=filename,
|
| 53 |
path_in_repo=filename,
|
| 54 |
repo_id=target_repo,
|
| 55 |
token=HF_TOKEN,
|
| 56 |
+
repo_type="dataset",
|
| 57 |
commit_message=f"Add {filename} from {url}"
|
| 58 |
)
|
| 59 |
return f"ファイル '{filename}' を '{target_repo}' にアップロードしました。"
|
| 60 |
except Exception as e:
|
| 61 |
return f"アップロード中にエラーが発生しました: {e}"
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# ファイルリストを取得
|
| 64 |
+
def list_files(repo_type):
|
| 65 |
+
target_repo = MODEL_REPO if repo_type == "model" else LORA_REPO
|
| 66 |
+
if not target_repo:
|
| 67 |
+
return f"{repo_type}用リポジトリが環境変数で設定されていません。"
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
files = api.list_repo_files(repo_id=target_repo, token=HF_TOKEN, repo_type="dataset")
|
| 71 |
+
return files
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"ファイルリストの取得に失敗しました: {e}"
|
| 74 |
+
|
| 75 |
+
# メタデータ並び替え
|
| 76 |
+
def sort_data(data, column):
|
| 77 |
+
try:
|
| 78 |
+
if not data or column not in data[0]:
|
| 79 |
+
return "並び替えに使用するカラムが見つかりません。"
|
| 80 |
+
|
| 81 |
+
sorted_data = sorted(data, key=lambda x: x.get(column, ""))
|
| 82 |
+
return sorted_data
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"並び替え中にエラーが発生しました: {e}"
|
| 85 |
+
|
| 86 |
+
# 初期データ(メモリ内で管理)
|
| 87 |
+
metadata = [
|
| 88 |
+
{"name": "file1", "size": 123, "type": "model"},
|
| 89 |
+
{"name": "file2", "size": 456, "type": "lora"},
|
| 90 |
+
{"name": "file3", "size": 789, "type": "model"}
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
# Gradio UI構築
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
gr.Markdown("## Civitai経由ファイルダウンロード&アップロードツール")
|
| 96 |
gr.Markdown("Civitai APIのURLを指定して、'model'または'lora'リポジトリへアップロードします。")
|
|
|
|
| 102 |
|
| 103 |
run_button.click(download_and_upload, inputs=[url_input, repo_choice], outputs=output)
|
| 104 |
|
| 105 |
+
# ファイルリスト表示セクション
|
| 106 |
+
gr.Markdown("### リポジトリ内のファイルを表示")
|
| 107 |
+
load_button = gr.Button("ロード")
|
| 108 |
+
file_list_output = gr.Textbox(label="リポジトリ内のファイル", interactive=False)
|
| 109 |
+
load_button.click(list_files, inputs=repo_choice, outputs=file_list_output)
|
| 110 |
+
|
| 111 |
+
# メタデータ並び替えセクション
|
| 112 |
+
gr.Markdown("### メタデータ並び替え")
|
| 113 |
+
metadata_table = gr.Dataframe(value=metadata, headers=["name", "size", "type"], datatype=["str", "number", "str"], label="並び替え対象のデータ")
|
| 114 |
+
sort_column = gr.Textbox(label="並び替えの基準列 (例: name, size, type)")
|
| 115 |
+
sort_button = gr.Button("並び替え実行")
|
| 116 |
+
sorted_table = gr.Dataframe(headers=["name", "size", "type"], datatype=["str", "number", "str"], label="並び替え後のデータ", interactive=False)
|
| 117 |
+
|
| 118 |
+
def update_metadata_table(data, column):
|
| 119 |
+
sorted_result = sort_data(data, column)
|
| 120 |
+
if isinstance(sorted_result, str): # エラーメッセージが返ってきた場合
|
| 121 |
+
return metadata, sorted_result
|
| 122 |
+
return sorted_result, "並び替えが完了しました。"
|
| 123 |
+
|
| 124 |
+
sort_button.click(update_metadata_table, inputs=[metadata_table, sort_column], outputs=[sorted_table, output])
|
| 125 |
+
|
| 126 |
if __name__ == "__main__":
|
| 127 |
demo.launch()
|