Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,7 @@ import json
|
|
| 3 |
import re
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
-
import
|
| 7 |
-
from huggingface_hub import HfApi, hf_hub_download, create_repo
|
| 8 |
-
from safetensors.torch import load_file
|
| 9 |
-
from diffusers import StableDiffusionXLPipeline
|
| 10 |
|
| 11 |
# 環境変数から取得
|
| 12 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
|
@@ -16,8 +13,7 @@ CIVITAI_TOKEN = os.environ.get("CIVITAI_TOKEN", None) # Civitai用トークン
|
|
| 16 |
|
| 17 |
api = HfApi()
|
| 18 |
|
| 19 |
-
|
| 20 |
-
def download_and_upload(model_id, repo_type, new_repo_name=None):
|
| 21 |
print(f"モデルID: {model_id}, リポジトリタイプ: {repo_type}")
|
| 22 |
if not model_id or not repo_type:
|
| 23 |
return "モデルIDまたはタイプが未指定です。"
|
|
@@ -68,124 +64,102 @@ def download_and_upload(model_id, repo_type, new_repo_name=None):
|
|
| 68 |
print(f"ファイルを保存しました: {filename}")
|
| 69 |
|
| 70 |
if repo_type == "model":
|
| 71 |
-
|
| 72 |
-
return "新しいモデルリポジトリ名が指定されていません。"
|
| 73 |
-
# 新しいリポジトリを作成
|
| 74 |
-
try:
|
| 75 |
-
model_repo_id = f"{api.whoami(token=HF_TOKEN)['name']}/{new_repo_name}"
|
| 76 |
-
create_repo(repo_id=model_repo_id, token=HF_TOKEN, repo_type="model")
|
| 77 |
-
print(f"新しいモデルリポジトリを作成しました: {model_repo_id}")
|
| 78 |
-
except Exception as e:
|
| 79 |
-
print(f"モデルリポジトリの作成に失敗しました: {e}")
|
| 80 |
-
return f"モデルリポジトリの作成に失敗しました: {e}"
|
| 81 |
-
target_repo = model_repo_id
|
| 82 |
-
# SAFETENSORファイルをDIFFUSERS形式に変換
|
| 83 |
-
try:
|
| 84 |
-
print("SAFETENSORファイルをDIFFUSERS形式に変換中です...")
|
| 85 |
-
# SDXLモデル用の変換処理
|
| 86 |
-
model_path = filename
|
| 87 |
-
pipeline = StableDiffusionXLPipeline.from_single_file(model_path)
|
| 88 |
-
pipeline.save_pretrained(target_repo)
|
| 89 |
-
print("変換が完了しました。")
|
| 90 |
-
except Exception as e:
|
| 91 |
-
print(f"変換中にエラーが発生しました: {e}")
|
| 92 |
-
return f"変換中にエラーが発生しました: {e}"
|
| 93 |
-
return f"モデルを'{target_repo}'に保存しました。"
|
| 94 |
else:
|
| 95 |
target_repo = LORA_REPO
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
else:
|
| 134 |
-
print(f"存在しないファイルのメタデータを削除: {entry['filename']}")
|
| 135 |
-
|
| 136 |
-
# ファイル名の重複チェック
|
| 137 |
-
existing_filenames = [e['filename'] for e in updated_metadata]
|
| 138 |
-
if filename in existing_filenames:
|
| 139 |
-
print(f"ファイル名が既に存在します。ファイル名: {filename}")
|
| 140 |
-
return f"ファイル名 '{filename}' は既にリポジトリに存在します。"
|
| 141 |
-
|
| 142 |
-
# 新しい通し番号の設定
|
| 143 |
-
sequence_numbers = [e['sequence_number'] for e in updated_metadata]
|
| 144 |
-
if sequence_numbers:
|
| 145 |
-
new_sequence_number = max(sequence_numbers) + 1
|
| 146 |
else:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
# Gradio UI構築
|
| 191 |
with gr.Blocks() as demo:
|
|
@@ -193,41 +167,21 @@ with gr.Blocks() as demo:
|
|
| 193 |
gr.Markdown("CivitaiのモデルIDを指定して、'model'または'lora'リポジトリへアップロードします。")
|
| 194 |
|
| 195 |
# ダウンロード&アップロード機能
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
value="model"
|
| 205 |
-
)
|
| 206 |
-
# モデルリポジトリ名入力('model'が選択された場合のみ表示)
|
| 207 |
-
new_repo_name_input = gr.Textbox(
|
| 208 |
-
label="新しいモデルリポジトリ名",
|
| 209 |
-
placeholder="例: my-new-model-repo",
|
| 210 |
-
visible=False
|
| 211 |
)
|
| 212 |
run_button = gr.Button("実行")
|
| 213 |
output = gr.Textbox(label="結果メッセージ", interactive=False)
|
| 214 |
|
| 215 |
-
# リポジトリタイプに応じて入力フォームを表示/非表示にする関数
|
| 216 |
-
def toggle_repo_name(repo_type):
|
| 217 |
-
if repo_type == "model":
|
| 218 |
-
return gr.update(visible=True)
|
| 219 |
-
else:
|
| 220 |
-
return gr.update(visible=False)
|
| 221 |
-
|
| 222 |
-
repo_choice.change(
|
| 223 |
-
toggle_repo_name,
|
| 224 |
-
inputs=repo_choice,
|
| 225 |
-
outputs=new_repo_name_input
|
| 226 |
-
)
|
| 227 |
-
|
| 228 |
run_button.click(
|
| 229 |
download_and_upload,
|
| 230 |
-
inputs=[model_id_input, repo_choice
|
| 231 |
outputs=output
|
| 232 |
)
|
| 233 |
|
|
|
|
| 3 |
import re
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
+
from huggingface_hub import HfApi, hf_hub_download
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# 環境変数から取得
|
| 9 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
|
|
|
| 13 |
|
| 14 |
api = HfApi()
|
| 15 |
|
| 16 |
+
def download_and_upload(model_id, repo_type):
|
|
|
|
| 17 |
print(f"モデルID: {model_id}, リポジトリタイプ: {repo_type}")
|
| 18 |
if not model_id or not repo_type:
|
| 19 |
return "モデルIDまたはタイプが未指定です。"
|
|
|
|
| 64 |
print(f"ファイルを保存しました: {filename}")
|
| 65 |
|
| 66 |
if repo_type == "model":
|
| 67 |
+
target_repo = MODEL_REPO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
else:
|
| 69 |
target_repo = LORA_REPO
|
| 70 |
|
| 71 |
+
if not target_repo:
|
| 72 |
+
return f"{repo_type}用リポジトリが環境変数で設定されていません。"
|
| 73 |
+
|
| 74 |
+
# リポジトリ内のファイル一覧を取得
|
| 75 |
+
try:
|
| 76 |
+
repo_files = api.list_repo_files(repo_id=target_repo, repo_type="dataset", token=HF_TOKEN)
|
| 77 |
+
print(f"リポジトリ内のファイル一覧を取得しました。")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"リポジトリ内のファイル一覧の取得に失敗しました: {e}")
|
| 80 |
+
return f"リポジトリ内のファイル一覧の取得に失敗しました: {e}"
|
| 81 |
+
|
| 82 |
+
# メタデータファイル名
|
| 83 |
+
metadata_filename = "metadata.json"
|
| 84 |
+
|
| 85 |
+
# リポジトリからメタデータファイルをダウンロード
|
| 86 |
+
try:
|
| 87 |
+
metadata_file = hf_hub_download(
|
| 88 |
+
repo_id=target_repo,
|
| 89 |
+
filename=metadata_filename,
|
| 90 |
+
repo_type="dataset",
|
| 91 |
+
token=HF_TOKEN,
|
| 92 |
+
force_download=True
|
| 93 |
+
)
|
| 94 |
+
with open(metadata_file, 'r', encoding='utf-8') as f:
|
| 95 |
+
metadata = json.load(f)
|
| 96 |
+
print("既存のメタデータを読み込みました。")
|
| 97 |
+
except Exception as e:
|
| 98 |
+
# メタデータファイルが存在しない場合は新規作成
|
| 99 |
+
metadata = []
|
| 100 |
+
print("メタデータファイルが存在しないため、新規作成します。")
|
| 101 |
+
|
| 102 |
+
# メタデータを更新(存在しないファイルを削除)
|
| 103 |
+
updated_metadata = []
|
| 104 |
+
for entry in metadata:
|
| 105 |
+
if entry['filename'] in repo_files:
|
| 106 |
+
updated_metadata.append(entry)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
else:
|
| 108 |
+
print(f"存在しないファイルのメタデータを削除: {entry['filename']}")
|
| 109 |
+
|
| 110 |
+
# ファイル名の重複チェック
|
| 111 |
+
existing_filenames = [e['filename'] for e in updated_metadata]
|
| 112 |
+
if filename in existing_filenames:
|
| 113 |
+
print(f"ファイル名が既に存在します。ファイル名: {filename}")
|
| 114 |
+
return f"ファイル名 '{filename}' は既にリポジトリに存在します。"
|
| 115 |
+
|
| 116 |
+
# 新しい通し番号の設定
|
| 117 |
+
sequence_numbers = [e['sequence_number'] for e in updated_metadata]
|
| 118 |
+
if sequence_numbers:
|
| 119 |
+
new_sequence_number = max(sequence_numbers) + 1
|
| 120 |
+
else:
|
| 121 |
+
new_sequence_number = 1
|
| 122 |
+
|
| 123 |
+
# 新しいエントリを追加
|
| 124 |
+
new_entry = {
|
| 125 |
+
"sequence_number": new_sequence_number,
|
| 126 |
+
"filename": filename,
|
| 127 |
+
"trigger_words": trigger_words
|
| 128 |
+
}
|
| 129 |
+
updated_metadata.append(new_entry)
|
| 130 |
+
print(f"新しいメタデータを追加しました: {new_entry}")
|
| 131 |
+
|
| 132 |
+
# メタデータをファイルに保存
|
| 133 |
+
with open(metadata_filename, 'w', encoding='utf-8') as f:
|
| 134 |
+
json.dump(updated_metadata, f, ensure_ascii=False, indent=4)
|
| 135 |
+
print(f"メタデータファイルを更新しました。ファイル名: {metadata_filename}")
|
| 136 |
+
|
| 137 |
+
# ファイルのアップロード
|
| 138 |
+
try:
|
| 139 |
+
# 元のファイルをアップロード
|
| 140 |
+
api.upload_file(
|
| 141 |
+
path_or_fileobj=filename,
|
| 142 |
+
path_in_repo=filename,
|
| 143 |
+
repo_id=target_repo,
|
| 144 |
+
token=HF_TOKEN,
|
| 145 |
+
repo_type="dataset",
|
| 146 |
+
commit_message=f"Add {filename} from model ID {model_id}"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# 更新したメタデータファイルをアップロード
|
| 150 |
+
api.upload_file(
|
| 151 |
+
path_or_fileobj=metadata_filename,
|
| 152 |
+
path_in_repo=metadata_filename,
|
| 153 |
+
repo_id=target_repo,
|
| 154 |
+
token=HF_TOKEN,
|
| 155 |
+
repo_type="dataset",
|
| 156 |
+
commit_message="Update metadata"
|
| 157 |
+
)
|
| 158 |
+
print(f"ファイルとメタデータをアップロードしました。")
|
| 159 |
+
return f"ファイル '{filename}' とメタデータを '{target_repo}' にアップロードしました。"
|
| 160 |
+
except Exception as e:
|
| 161 |
+
print(f"アップロード中にエラーが発生しました: {e}")
|
| 162 |
+
return f"アップロード中にエラーが発生しました: {e}"
|
| 163 |
|
| 164 |
# Gradio UI構築
|
| 165 |
with gr.Blocks() as demo:
|
|
|
|
| 167 |
gr.Markdown("CivitaiのモデルIDを指定して、'model'または'lora'リポジトリへアップロードします。")
|
| 168 |
|
| 169 |
# ダウンロード&アップロード機能
|
| 170 |
+
model_id_input = gr.Textbox(
|
| 171 |
+
label="CivitaiモデルID",
|
| 172 |
+
placeholder="例: 1102"
|
| 173 |
+
)
|
| 174 |
+
repo_choice = gr.Radio(
|
| 175 |
+
choices=["model", "lora"],
|
| 176 |
+
label="アップロード先タイプの選択",
|
| 177 |
+
value="model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
)
|
| 179 |
run_button = gr.Button("実行")
|
| 180 |
output = gr.Textbox(label="結果メッセージ", interactive=False)
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
run_button.click(
|
| 183 |
download_and_upload,
|
| 184 |
+
inputs=[model_id_input, repo_choice],
|
| 185 |
outputs=output
|
| 186 |
)
|
| 187 |
|