Spaces:
Sleeping
Sleeping
多分error
Browse files
app.py
CHANGED
|
@@ -19,21 +19,35 @@ else:
|
|
| 19 |
print("HF_TOKEN error")
|
| 20 |
api = HfApi() # トークンなしで初期化
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
# 履歴ファイルを定義
|
| 23 |
-
HISTORY_FILE = "
|
| 24 |
|
| 25 |
# 履歴をロードする関数
|
|
|
|
|
|
|
| 26 |
def load_history():
|
| 27 |
history = []
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
return history
|
| 36 |
|
|
|
|
| 37 |
# 履歴を初期化時にロード
|
| 38 |
history = load_history()
|
| 39 |
|
|
@@ -72,15 +86,11 @@ euler_scheduler = EulerAncestralDiscreteScheduler.from_config(
|
|
| 72 |
dpm_scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 73 |
pipe.scheduler = euler_scheduler
|
| 74 |
|
| 75 |
-
# 画像をアップロードするリポジトリID
|
| 76 |
-
HF_REPO_ID = "cocoat/images"
|
| 77 |
-
|
| 78 |
-
|
| 79 |
def upload_image_to_hub(image_pil, prompt_text):
|
| 80 |
# ファイル名を生成(タイムスタンプとプロンプトの一部)
|
| 81 |
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
| 82 |
# プロンプトから安全なファイル名の一部を生成
|
| 83 |
-
safe_prompt = "".join(c for c in prompt_text if c.isalnum() or c in (' ', '.', '_')).replace(' ', '_')[:30]
|
| 84 |
filename = f"image_{timestamp}.png"
|
| 85 |
filepath = f"temp_{filename}"
|
| 86 |
image_pil.save(filepath)
|
|
@@ -173,27 +183,45 @@ def infer(prompt, neg, seed, rand, w, h, cfg, steps, scheduler_type,
|
|
| 173 |
f"Scheduler: {scheduler_type}"
|
| 174 |
)
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
img.save(image_filename)
|
| 180 |
-
|
| 181 |
# 履歴を更新
|
| 182 |
global history
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
history_max_items = 10
|
| 186 |
if len(history) > history_max_items:
|
| 187 |
-
|
| 188 |
|
| 189 |
-
#
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
progress(1.0, desc="Done!")
|
| 195 |
|
| 196 |
-
|
|
|
|
|
|
|
| 197 |
processed_img, processed_gallery_items = process_image(img, gallery_items)
|
| 198 |
|
| 199 |
latest_caption_table = make_html_table(caption_text)
|
|
@@ -437,18 +465,12 @@ window.addEventListener('load', () => {
|
|
| 437 |
)
|
| 438 |
|
| 439 |
# ページロード時に history から初期表示
|
| 440 |
-
# demo.load(
|
| 441 |
-
# fn=lambda: (
|
| 442 |
-
# [(img_or_url, caption) for img_or_url, caption in history],
|
| 443 |
-
# "".join(f'<div style="margin-bottom:12px">{make_html_table(caption)}</div>' for _, caption in history)
|
| 444 |
-
# ),
|
| 445 |
-
# inputs=[],
|
| 446 |
-
# outputs=[history_gallery, history_tables]
|
| 447 |
-
# )
|
| 448 |
demo.load(
|
| 449 |
fn=lambda: (
|
| 450 |
-
|
| 451 |
-
|
|
|
|
|
|
|
| 452 |
),
|
| 453 |
inputs=[],
|
| 454 |
outputs=[history_gallery, history_tables]
|
|
|
|
| 19 |
print("HF_TOKEN error")
|
| 20 |
api = HfApi() # トークンなしで初期化
|
| 21 |
|
| 22 |
+
# 画像をアップロードするリポジトリID
|
| 23 |
+
HF_REPO_ID = "cocoat/images"
|
| 24 |
+
|
| 25 |
# 履歴ファイルを定義
|
| 26 |
+
HISTORY_FILE = "history/generation_history_coamixXL3.txt"
|
| 27 |
|
| 28 |
# 履歴をロードする関数
|
| 29 |
+
import os
|
| 30 |
+
import requests
|
| 31 |
def load_history():
|
| 32 |
history = []
|
| 33 |
+
hf_raw_file_url = f"https://huggingface.co/datasets/{HF_REPO_ID}/raw/main/{HISTORY_FILE}"
|
| 34 |
+
try:
|
| 35 |
+
response = requests.get(hf_raw_file_url)
|
| 36 |
+
response.raise_for_status() # HTTPエラー(4xx, 5xx)があれば例外を発生させます。
|
| 37 |
+
|
| 38 |
+
for line in response.text.splitlines():
|
| 39 |
+
parts = line.strip().split("|||")
|
| 40 |
+
if len(parts) == 2:
|
| 41 |
+
image_path_in_repo, caption = parts
|
| 42 |
+
history.append((image_path_in_repo, caption))
|
| 43 |
+
print(f"History loaded from Hub via raw URL: {len(history)} entries.")
|
| 44 |
+
except requests.exceptions.RequestException as e:
|
| 45 |
+
print(f"Error loading history from Hub via raw URL: {e}. Starting with empty history.")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"An unexpected error occurred while parsing history: {e}. Starting with empty history.")
|
| 48 |
return history
|
| 49 |
|
| 50 |
+
|
| 51 |
# 履歴を初期化時にロード
|
| 52 |
history = load_history()
|
| 53 |
|
|
|
|
| 86 |
dpm_scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 87 |
pipe.scheduler = euler_scheduler
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
def upload_image_to_hub(image_pil, prompt_text):
|
| 90 |
# ファイル名を生成(タイムスタンプとプロンプトの一部)
|
| 91 |
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
| 92 |
# プロンプトから安全なファイル名の一部を生成
|
| 93 |
+
# safe_prompt = "".join(c for c in prompt_text if c.isalnum() or c in (' ', '.', '_')).replace(' ', '_')[:30]
|
| 94 |
filename = f"image_{timestamp}.png"
|
| 95 |
filepath = f"temp_{filename}"
|
| 96 |
image_pil.save(filepath)
|
|
|
|
| 183 |
f"Scheduler: {scheduler_type}"
|
| 184 |
)
|
| 185 |
|
| 186 |
+
# 画像をHubにアップロードし、そのURLを取得
|
| 187 |
+
uploaded_image_url = upload_image_to_hub(img, prompt)
|
| 188 |
+
|
|
|
|
|
|
|
| 189 |
# 履歴を更新
|
| 190 |
global history
|
| 191 |
+
if uploaded_image_url: # アップロードが成功した場合のみ履歴に追加
|
| 192 |
+
# 履歴には画像のHub上のパスを保存
|
| 193 |
+
path_in_repo_for_history = uploaded_image_url.split(f"huggingface.co/datasets/{HF_REPO_ID}/resolve/main/")[1]
|
| 194 |
+
history.insert(0, (path_in_repo_for_history, caption_text))
|
| 195 |
|
| 196 |
history_max_items = 10
|
| 197 |
if len(history) > history_max_items:
|
| 198 |
+
history.pop() # ローカルファイルの削除は不要になるため、パスの取得も不要です。
|
| 199 |
|
| 200 |
+
# 履歴ファイルを更新し、Hubにアップロードする
|
| 201 |
+
temp_history_filepath = "temp_history.txt"
|
| 202 |
+
with open(temp_history_filepath, "w", encoding="utf-8") as f:
|
| 203 |
+
for img_path_in_repo, cap_text in history:
|
| 204 |
+
f.write(f"{img_path_in_repo}|||{cap_text}\n")
|
| 205 |
+
|
| 206 |
+
try:
|
| 207 |
+
api.upload_file(
|
| 208 |
+
path_or_fileobj=temp_history_filepath,
|
| 209 |
+
path_in_repo=HISTORY_FILE,
|
| 210 |
+
repo_id=HF_REPO_ID,
|
| 211 |
+
repo_type="dataset",
|
| 212 |
+
)
|
| 213 |
+
print(f"History file '{HISTORY_FILE}' updated on Hugging Face Hub.")
|
| 214 |
+
except Exception as e:
|
| 215 |
+
print(f"Error updating history file on Hub: {e}")
|
| 216 |
+
finally:
|
| 217 |
+
if os.path.exists(temp_history_filepath):
|
| 218 |
+
os.remove(temp_history_filepath)
|
| 219 |
|
| 220 |
progress(1.0, desc="Done!")
|
| 221 |
|
| 222 |
+
# ギャラリー表示用のアイテムリストを生成(Hub上のURLを使用)
|
| 223 |
+
gallery_items = [(f"https://huggingface.co/datasets/{HF_REPO_ID}/resolve/main/{item[0]}", item[1]) for item in history]
|
| 224 |
+
|
| 225 |
processed_img, processed_gallery_items = process_image(img, gallery_items)
|
| 226 |
|
| 227 |
latest_caption_table = make_html_table(caption_text)
|
|
|
|
| 465 |
)
|
| 466 |
|
| 467 |
# ページロード時に history から初期表示
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
demo.load(
|
| 469 |
fn=lambda: (
|
| 470 |
+
# history リストの各要素が (Hub上のファイルパス, キャプション) なので、URLに変換
|
| 471 |
+
[ (f"https://huggingface.co/datasets/{HF_REPO_ID}/resolve/main/{img_path_in_repo}", caption)
|
| 472 |
+
for img_path_in_repo, caption in history ],
|
| 473 |
+
make_html_table(history[0][1]) if history else "" # 最初のアイテムのキャプションを表示
|
| 474 |
),
|
| 475 |
inputs=[],
|
| 476 |
outputs=[history_gallery, history_tables]
|