waveshaker commited on
Commit
2f475e0
ยท
verified ยท
1 Parent(s): e785cfd

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +191 -1284
index.html CHANGED
@@ -1,1307 +1,214 @@
1
- """Comfy Backend Image Studio (Comfy-Org) - Config-Driven Edition."""
2
-
3
- import asyncio
4
- import glob
5
- import json
6
- import os
7
- import pathlib
8
- import random
9
- import re
10
- import shutil
11
- import subprocess
12
- import sys
13
- import traceback
14
- import uuid
15
- from typing import Any
16
- import gradio as gr
17
- import requests
18
- from huggingface_hub import hf_hub_download, list_repo_files
19
- from tqdm.auto import tqdm
20
- import tqdm as tqdm_lib
21
- import huggingface_hub
22
-
23
- import os
24
-
25
- from model_config import MODEL_CONFIG
26
-
27
- # ==========================================================================
28
- # ๐Ÿ› ๏ธ Environment Patches & Global Paths
29
- # ==========================================================================
30
- try:
31
- import spaces
32
- except ImportError:
33
- class _SpacesFallback:
34
- @staticmethod
35
- def GPU(**_kwargs):
36
- def decorate(function):
37
- return function
38
- return decorate
39
- spaces = _SpacesFallback()
40
-
41
- import torch
42
- if hasattr(torch.cuda, 'get_device_properties'):
43
- _orig_get_device_properties = torch.cuda.get_device_properties
44
- def _patched_get_device_properties(device):
45
- props = _orig_get_device_properties(device)
46
- if not hasattr(props, 'is_integrated'):
47
- props.is_integrated = False
48
- return props
49
- torch.cuda.get_device_properties = _patched_get_device_properties
50
-
51
- ROOT = pathlib.Path(__file__).resolve().parent
52
- COMFY = ROOT / "ComfyUI"
53
- MODELS = COMFY / "models"
54
- INPUT = COMFY / "input"
55
- OUTPUT = COMFY / "output"
56
-
57
- _comfy_ready = False
58
- _nodes_ready = False
59
- tasks_status = {}
60
- _last_used_architecture = None # ๆ›ฟไปฃ _last_used_engine (ไปฃ่กจๅบ•ๅฑ‚ๆžถๆž„๏ผŒๅฆ‚ moss/pony)
61
- _last_used_unet = None # ไฟ็•™ (ไปฃ่กจๅ…ทไฝ“็š„ UNet ๆƒ้‡ๆ–‡ไปถๅ)
62
- # ==========================================================================
63
- # โš™๏ธ Comfy Config
64
- # ==========================================================================
65
- SAMPLERS = ["euler", "euler_ancestral", "euler_a", "dpmpp_2m", "dpmpp_2m_sde", "dpmpp_sde", "heun", "lms","lcm"]
66
- SCHEDULERS = ["simple", "beta", "normal", "karras", "exponential", "sgm_uniform"]
67
-
68
- UNET_CHOICES = {}
69
- BASE_LORAS_CHOICES = {}
70
- CHAR_LORA_CHOICES = {}
71
-
72
- # ==========================================================================
73
- # ๐Ÿ“Š Load Config & LoRAs List (่‡ชๅŠจๅŒน้… WebP)
74
- # ==========================================================================
75
- def load_config_from_json():
76
- global UNET_CHOICES, BASE_LORAS_CHOICES
77
- try:
78
- print(f"ๆญฃๅœจ่Žทๅ–่ฟœ็ซฏ JSON ้…็ฝฎ: {MODEL_CONFIG['config_url']}")
79
- res = requests.get(MODEL_CONFIG['config_url'], timeout=5)
80
- if res.ok:
81
- full_data = res.json()
82
- root_mapping = MODEL_CONFIG['json_keys']['root_mapping']
83
-
84
- for remote_key, local_engine in root_mapping.items():
85
- engine_data = full_data.get(remote_key, {})
86
-
87
- # ๐ŸŒŸ ๆ ธๅฟƒไฟฎๆ”น 1๏ผšๅŽป models ้‡Œๆ‰พๅˆฐ็œŸๅ๏ผŒๅฆ‚ๆžœๆ‰พไธๅˆฐๅฐฑ็”จ็Ÿญๅๅ…œๅบ•
88
- display_engine = MODEL_CONFIG["models"].get(local_engine, {}).get("name", local_engine)
89
-
90
- for item in engine_data.get(MODEL_CONFIG['json_keys']['unets'], []):
91
- # ๐ŸŒŸ ไธ‹ๆ‹‰ๆก†ๆ˜พ็คบ็œŸๅ๏ผŒไฝ† dictionary ็š„ value ้‡Œไพ็„ถๅญ˜็Ÿญๅ
92
- display_name = f"{display_engine} | {item['name']}"
93
- UNET_CHOICES[display_name] = {"repo": item["repo"], "subfolder": item["subfolder"], "filename": item["filename"], "engine": local_engine}
94
- for item in engine_data.get(MODEL_CONFIG['json_keys']['base_loras'], []):
95
- display_name = f"{item['name']}"
96
- BASE_LORAS_CHOICES[display_name] = {"repo": item["repo"], "subfolder": item["subfolder"], "filename": item["filename"], "defaultEnable": str(item.get("defaultEnable", "false")).lower() == "true", "engine": local_engine}
97
- print("โœ… ๆˆๅŠŸๅŠ ่ฝฝ JSON ้…็ฝฎ")
98
- return
99
- except Exception as e:
100
- print(f"โš ๏ธ JSON ้…็ฝฎๅŠ ่ฝฝๅคฑ่ดฅ: {e}")
101
-
102
- for engine, cfg in MODEL_CONFIG.get("models", {}).items():
103
- # ๐ŸŒŸ ๆ ธๅฟƒไฟฎๆ”น 2๏ผšfallback ้‡ŒไนŸ่ฆๅ–็œŸๅ
104
- display_engine = cfg.get("name", engine)
105
-
106
- for name, info in cfg.get("fallback_unets", {}).items():
107
- UNET_CHOICES[f"{display_engine} | {name}"] = {**info, "engine": engine}
108
- for name, info in cfg.get("fallback_base_loras", {}).items():
109
- BASE_LORAS_CHOICES[f"{name}"] = {**info, "engine": engine}
110
-
111
- import time
112
- LAST_SCAN_TIME = 0 # ๅ…จๅฑ€ๆ—ถ้—ดๆˆณ
113
-
114
- def load_char_loras_from_repo():
115
- global CHAR_LORA_CHOICES, LAST_SCAN_TIME
116
- current_time = time.time()
117
-
118
- # =========================================================================
119
- # TODO: ๆ€ง่ƒฝไผ˜ๅŒ–ไธŽๅŠ ่ฝฝไฝ“้ชŒ (ๅฆ‚ๆžœๆœชๆฅๅ‡บ็ŽฐๅŠ ่ฝฝๅก้กฟ๏ผŒๅฏๅ‚่€ƒไปฅไธ‹ๆ–นๆกˆ)
120
- # 1. ๅฝ“ๅ‰ๅทฒ้‡‡็”จ 300 ็ง’(5ๅˆ†้’Ÿ) ็ผ“ๅญ˜๏ผŒ้ฟๅ…้ข‘็น่ฏทๆฑ‚ HF ๆŽฅๅฃใ€‚
121
- # 2. ็ซ™ๅ†…่ฏทๆฑ‚้€šๅธธๆžๅฟซ(ๅ‡ ๅๆฏซ๏ฟฝ๏ฟฝ๏ฟฝ)๏ผŒๅ› ๆญค็›ฎๅ‰้‡‡็”จๅŒๆญฅ็ญ‰ๅพ…๏ผŒไฟ่ฏ่ฟ›ๅ…ฅ้กต้ขๅณๆœ€ๆ–ฐๆ•ฐๆฎใ€‚
122
- # 3. ๅฆ‚ๆžœๅŽ็ปญ HF ่Š‚็‚นๆŠ–ๅŠจๅฏผ่‡ด้กต้ข็™ฝๅฑ๏ผŒๅฏ่€ƒ่™‘ๅฐ†ไธ‹ๆ–น่ฏทๆฑ‚ๆ”นไธบๅŽๅฐ้™้ป˜ๅˆทๆ–ฐ
123
- # (ๅฆ‚ไฝฟ็”จ threading.Thread) ๆˆ–ไธบ list_repo_files ๅขžๅŠ  timeout ๆŽงๅˆถใ€‚
124
- # =========================================================================
125
- if current_time - LAST_SCAN_TIME < 300:
126
- print("โšก ไฝฟ็”จ็ผ“ๅญ˜๏ผŒ่ทณ่ฟ‡้‡ๆ–ฐๆ‰ซๆ LoRA ๅˆ—่กจ")
127
- return
128
-
129
- print(f"๐Ÿ”„ ่งฆๅ‘ๅŠจๆ€ๅˆทๆ–ฐ๏ผŒๆญฃๅœจ้‡ๆ–ฐๆ‰ซๆ {MODEL_CONFIG['char_lora_repo']}...")
130
- exclude_list = [x.lower() for x in MODEL_CONFIG.get("exclude_folders", [])]
131
-
132
- new_char_choices = {}
133
-
134
- try:
135
- files = list_repo_files(repo_id=MODEL_CONFIG['char_lora_repo'], token=os.environ.get("HF_TOKEN"))
136
- for engine, cfg in MODEL_CONFIG.get("models", {}).items():
137
- prefix = cfg.get("char_lora_prefix", "")
138
- new_char_choices[engine] = {}
139
- for f in files:
140
- if f.startswith(prefix) and f.endswith(".safetensors") and "loras/" not in f:
141
- relative_path = f[len(prefix):]
142
- parts = relative_path.split("/")
143
- if len(parts) > 1 and parts[0].lower() in exclude_list:
144
- continue
145
-
146
- filename = parts[-1]
147
- webp_path = f.replace(".safetensors", ".webp")
148
- webp_url = f"https://huggingface.co/{MODEL_CONFIG['char_lora_repo']}/resolve/main/{webp_path}"
149
-
150
- new_char_choices[engine][filename] = {
151
- "path": f,
152
- "webp": webp_url
153
- }
154
-
155
- CHAR_LORA_CHOICES.clear()
156
- CHAR_LORA_CHOICES.update(new_char_choices)
157
- LAST_SCAN_TIME = current_time
158
- print(f"โœ… ๆˆๅŠŸๅˆทๆ–ฐ่ง’่‰ฒ LoRA: {[f'{k}: {len(v)}ไธช' for k, v in CHAR_LORA_CHOICES.items()]}")
159
- except Exception as e:
160
- print(f"โš ๏ธ ๅŠจๆ€ๅˆทๆ–ฐๅคฑ่ดฅ: {e}")
161
-
162
- load_config_from_json()
163
- load_char_loras_from_repo()
164
-
165
- # ==========================================================================
166
- # โฌ‡๏ธ Download & Init
167
- # ==========================================================================
168
- def _run(command: list[str], cwd: pathlib.Path | None = None, check: bool = True) -> None:
169
- subprocess.run(command, cwd=str(cwd) if cwd else None, check=check)
170
-
171
- def _install_filtered_requirements(path: pathlib.Path) -> None:
172
- if not path.exists(): return
173
- blocked = {"torch", "torchvision", "torchaudio", "transformers", "huggingface-hub", "accelerate"}
174
- requirements = []
175
- for raw in path.read_text(encoding="utf-8", errors="ignore").splitlines():
176
- item = raw.strip()
177
- if not item or item.startswith("#"): continue
178
- package = re.split(r"[<>=!~;\[\s]", item.lower().replace("_", "-"), maxsplit=1)[0]
179
- if package not in blocked: requirements.append(item)
180
- if requirements:
181
- _run([sys.executable, "-m", "pip", "install", "--no-cache-dir", *requirements], check=False)
182
-
183
- def _ensure_comfy() -> None:
184
- global _comfy_ready
185
- if _comfy_ready: return
186
- if not COMFY.exists():
187
- _run(["git", "clone", "--depth", "1", "https://github.com/comfyanonymous/ComfyUI.git", str(COMFY)])
188
- _install_filtered_requirements(COMFY / "requirements.txt")
189
- for folder in ("diffusion_models", "text_encoders", "vae", "loras"):
190
- (MODELS / folder).mkdir(parents=True, exist_ok=True)
191
- INPUT.mkdir(parents=True, exist_ok=True)
192
- OUTPUT.mkdir(parents=True, exist_ok=True)
193
- _comfy_ready = True
194
-
195
- def _init_comfy_nodes() -> None:
196
- global _nodes_ready
197
- if _nodes_ready: return
198
- sys.path.insert(0, str(COMFY))
199
- os.chdir(COMFY)
200
- import execution, nodes, server
201
- from app.assets.manager import default_asset_manager
202
- loop = asyncio.new_event_loop()
203
- asyncio.set_event_loop(loop)
204
- server_instance = server.PromptServer(loop, default_asset_manager())
205
- execution.PromptQueue(server_instance)
206
- loop.run_until_complete(nodes.init_extra_nodes())
207
- _nodes_ready = True
208
-
209
- def _download_if_needed(repo: str, subfolder: str, filename: str, dest_root: pathlib.Path) -> None:
210
- dest_path = dest_root / filename
211
- if dest_path.exists(): return
212
- print(f"โณ Downloading: {filename} ...")
213
- dest_root.mkdir(parents=True, exist_ok=True)
214
- hf_hub_download(repo_id=repo, subfolder=subfolder if subfolder else None, filename=filename, local_dir=str(dest_root))
215
- src = dest_root / subfolder / filename if subfolder else dest_root / filename
216
- if src.exists() and src != dest_path:
217
- shutil.move(str(src), str(dest_path))
218
-
219
- def _ensure_models() -> None:
220
- print("ๅผ€ๅง‹ๅ…จ้‡ไธ‹่ฝฝๅŸบ็ก€ๆจกๅž‹...")
221
- for engine_cfg in MODEL_CONFIG["models"].values():
222
- for repo, file_path in engine_cfg.get("companion_downloads", []):
223
- path = pathlib.Path(file_path)
224
- if "text_encoders" in str(path): dest_root = MODELS / "text_encoders"
225
- elif "vae" in str(path): dest_root = MODELS / "vae"
226
- else: dest_root = MODELS / path.parent
227
- _download_if_needed(repo, str(path.parent), path.name, dest_root)
228
- for name, cfg in UNET_CHOICES.items():
229
- _download_if_needed(cfg["repo"], cfg["subfolder"], cfg["filename"], MODELS / "diffusion_models")
230
- lora_root = MODELS / "loras"
231
- for name, cfg in BASE_LORAS_CHOICES.items():
232
- _download_if_needed(cfg["repo"], cfg["subfolder"], cfg["filename"], lora_root)
233
- print("โœ… ๅŸบ็ก€ๆจกๅž‹ไธ‹่ฝฝๅฎŒๆฏ•๏ผ")
234
-
235
- def _prepare_runtime() -> None:
236
- _ensure_comfy()
237
- _ensure_models()
238
- _init_comfy_nodes()
239
-
240
- try:
241
- print("โฌ‡๏ธ Starting up...")
242
- _prepare_runtime()
243
- except Exception as e:
244
- print(f"โš ๏ธ Startup failed: {e}")
245
-
246
- # ==========================================================================
247
- # โš›๏ธ Comfy Backend & Workflow
248
- # ==========================================================================
249
-
250
- def _ref(node: str, output: int = 0) -> list[Any]:
251
- return [node, output]
252
-
253
- def _execute_workflow(workflow: dict[str, Any], task_id: str = None) -> list[str]:
254
- # ๐ŸŒŸ ๅŒ้‡ๅŠซๆŒ๏ผšๅŒๆ—ถๆ‹ฆๆˆช tqdm ๅ’Œ tqdm.auto.tqdm
255
- import tqdm as tqdm_lib
256
- import tqdm.auto
257
- import time # ๐ŸŒŸ ๅผ•ๅ…ฅ time
258
-
259
- original_tqdm = tqdm_lib.tqdm
260
- original_auto_tqdm = tqdm.auto.tqdm
261
-
262
- tqdm_lib.tqdm = UnifiedProgressTqdm
263
- tqdm.auto.tqdm = UnifiedProgressTqdm
264
-
265
- try:
266
- if str(COMFY) not in sys.path:
267
- sys.path.insert(0, str(COMFY))
268
- import execution, server
269
- from app.assets.manager import default_asset_manager
270
- loop = asyncio.new_event_loop()
271
- asyncio.set_event_loop(loop)
272
- server_instance = server.PromptServer(loop, default_asset_manager())
273
- # ๐ŸŒŸ ไฟๆŒไบ†ไฝ ๆ”นๅฅฝ็š„ lru: 0
274
- executor = execution.PromptExecutor(server_instance, cache_type=execution.CacheType.RAM_PRESSURE, cache_args={"lru": 0, "ram": 2.0, "ram_inactive": 8.0})
275
- prompt_id = str(uuid.uuid4())
276
- save_id = next(k for k, v in workflow.items() if v.get("class_type") == "SaveImage")
277
-
278
- # ๐ŸŒŸ ้€š็Ÿฅๅ‰็ซฏๅ‡†ๅค‡ๆ‰ง่กŒๅทฅไฝœๆต๏ผŒๅนถ่ฎฐๅฝ•ๆจกๅž‹ๅŠ ่ฝฝ่ตท็‚น
279
- if task_id and task_id in tasks_status:
280
- tasks_status[task_id]["message"] = "๐Ÿ”„ Executing Workflow..."
281
-
282
- import json
283
- import os
284
- import time
285
- try:
286
- metric_file = f"/tmp/metrics_{task_id}.json"
287
- saved_data = {}
288
- if os.path.exists(metric_file):
289
- with open(metric_file, "r") as f:
290
- saved_data = json.load(f)
291
-
292
- # ไธ€ๆฌกๆ€งๅ†™ๅ…ฅ๏ผš็œŸๅฎž้˜ถๆฎต + ๆจกๅž‹ๅŠ ่ฝฝ่ตท็‚นๆ—ถ้—ด
293
- saved_data["phase"] = "๐Ÿ”„ Executing Workflow..."
294
- saved_data["load_start"] = time.time()
295
-
296
- with open(metric_file, "w") as f:
297
- json.dump(saved_data, f)
298
- except Exception:
299
- pass
300
-
301
- print(f"[ๆ‰ง่กŒๅ™จ] ๐Ÿš€ ๅผ€ๅง‹ๆ‰ง่กŒ ComfyUI Workflow...", flush=True)
302
-
303
- # ๐ŸŒŸ ๆ ธๅฟƒๆ–ฐๅขž๏ผš่ฎฐๅฝ•ๅผ€ๅง‹ๆ—ถ้—ด (็”จไบŽๆœ€ๅŽ็ฎ— workflow ๆ€ป่€—ๆ—ถ)
304
- workflow_start = time.time()
305
- # ๆ‰ง่กŒๅทฅไฝœๆต
306
- executor.execute(workflow, prompt_id, extra_data={}, execute_outputs=[save_id])
307
-
308
- # ๐ŸŒŸ ็ปˆๆžๆ–นๆกˆ๏ผšๅ†™ๅ…ฅๆœฌๅœฐไธดๆ—ถๆ–‡ไปถ
309
- import json
310
- import os
311
- wf_time = round(time.time() - workflow_start, 2)
312
-
313
- if task_id:
314
- try:
315
- metric_file = f"/tmp/metrics_{task_id}.json"
316
- saved_data = {}
317
- if os.path.exists(metric_file):
318
- with open(metric_file, "r") as f:
319
- saved_data = json.load(f)
320
-
321
- saved_data["workflow"] = wf_time
322
-
323
- # ๐ŸŒŸ ็ปๅ‘ฝๅ…œๅบ•๏ผšๅฆ‚ๆžœๅ› ไธบ่ทณๆญฅ๏ผŒๆŽข้’ˆๆฒกๆŠ“ๅˆฐ load_time๏ผŒๅฐฑๅœจ่ฟ™้‡Œๅผบ่กŒ่กฅ็ฎ—๏ผ
324
- if "load_time" not in saved_data:
325
- load_start = saved_data.get("load_start", workflow_start)
326
- # ็”จๆ€ปๆต็จ‹่€—ๆ—ถๅ‡ๅŽป็บฏ่ท‘ๅ›พ่€—ๆ—ถไฝœไธบๅ…œๅบ•
327
- saved_data["load_time"] = round(wf_time * 0.8, 2) # ๆˆ–่€…็›ดๆŽฅ round(wf_time, 2)
328
- print(f"[ๆŽข้’ˆ] โš ๏ธ ๅ…œๅบ•่กฅๅฝ• load_time: {saved_data['load_time']}s", flush=True)
329
-
330
- with open(metric_file, "w") as f:
331
- json.dump(saved_data, f)
332
- except Exception as e:
333
- print(f"ๅ†™ๅ…ฅ workflow ๆŒ‡ๆ ‡ๅคฑ่ดฅ: {e}")
334
-
335
- print(f"[ๆ‰ง่กŒๅ™จ] โœ… ComfyUI Workflow ๆ‰ง่กŒๅฎŒๆฏ•๏ผŒ่€—ๆ—ถ: {wf_time}s", flush=True)
336
-
337
- if not executor.success:
338
- message = executor.status_messages[-1] if executor.status_messages else "ComfyUI execution failed"
339
- raise RuntimeError(str(message))
340
-
341
- paths = []
342
- for output in executor.history_result.get("outputs", {}).values():
343
- for items in output.values():
344
- if not isinstance(items, list): continue
345
- for item in items:
346
- if not isinstance(item, dict) or not item.get("filename"): continue
347
- candidate = OUTPUT / item.get("subfolder", "") / item["filename"]
348
- if candidate.exists(): paths.append(str(candidate))
349
- if not paths:
350
- paths = sorted([p for p in glob.glob(str(OUTPUT / "**" / "*.png"), recursive=True)], key=lambda p: os.path.getmtime(p), reverse=True)
351
- return paths[:1]
352
- finally:
353
- # ๐ŸŒŸ ไธฅๆ ผ่ฟ˜ๅŽŸ
354
- tqdm_lib.tqdm = original_tqdm
355
- tqdm.auto.tqdm = original_auto_tqdm
356
-
357
- # ๐ŸŒŸ ๆ–ฐๅขž๏ผšไธปๅŠจๅ…ณ้—ญ event loop๏ผŒ้˜ฒๆญข Python 3.12 ็š„ GC ๆŠฅ้”™
358
- if 'loop' in locals() and not loop.is_closed():
359
- try:
360
- loop.close()
361
- except Exception:
362
- pass
363
-
364
- # ==========================================================================
365
- # ๐ŸŒŸ ็ปŸไธ€่ฟ›ๅบฆๆ‹ฆๆˆชๅ™จ๏ผšๅŒๆ—ถๆŽฅ็ฎกไธ‹่ฝฝ & ่ท‘ๅ›พ่ฟ›ๅบฆ
366
- # ==========================================================================
367
-
368
-
369
- _current_download_task_id = None
370
- _current_generation_task_id = None
371
-
372
- class UnifiedProgressTqdm(tqdm):
373
- def update(self, n=1):
374
- super().update(n)
375
-
376
- # ========================================================
377
- # ๐ŸŒŸ 1. ไธ‹่ฝฝ LoRA ่ฟ›ๅบฆ (ไฟๆŒๅŽŸๆ ท)
378
- # ========================================================
379
- if _current_download_task_id and _current_download_task_id in tasks_status:
380
- if self.total and self.total > 0:
381
- percent = int((self.n / self.total) * 100)
382
- tasks_status[_current_download_task_id]["message"] = f"๐Ÿ“ฅ Downloading LoRA: {percent}%..."
383
- else:
384
- # ๅฆ‚ๆžœ total ๆ‹ฟไธๅˆฐ๏ผŒ่‡ณๅฐ‘ๆŠŠๅฝ“ๅ‰ไธ‹่ฝฝไบ†ๅคšๅฐ‘ๅญ—่Š‚ๅ‘Š่ฏ‰ๅ‰็ซฏ
385
- tasks_status[_current_download_task_id]["message"] = f"๐Ÿ“ฅ Downloading LoRA: {self.n} units..."
386
- return # ๐ŸŒŸ ไธ‹่ฝฝๅฎŒๆฏ•็›ดๆŽฅ return๏ผŒ็ปไธๅพ€ไธ‹่ตฐๅŽป็ขฐ่ท‘ๅ›พ้€ป่พ‘
387
-
388
- # ========================================================
389
- # ๐ŸŒŸ 2. ComfyUI ่ท‘ๅ›พ่ฟ›ๅบฆ
390
- # ========================================================
391
- if _current_generation_task_id and _current_generation_task_id in tasks_status:
392
- if self.total and self.total > 1:
393
- # ๐ŸŒŸ ไฟฎๅค้˜ฒ่ทณๆญฅ๏ผšๅช่ฆ็œ‹ๅˆฐ่ฟ›ๅบฆๆก๏ผŒไธ”่ฟ˜ๆฒก็ฎ—่ฟ‡๏ผŒๅฐฑ็ซ‹ๅˆป็ฎ—ไธ€ๆฌก
394
- import json
395
- import os
396
- import time
397
- try:
398
- metric_file = f"/tmp/metrics_{_current_generation_task_id}.json"
399
- if os.path.exists(metric_file):
400
- with open(metric_file, "r") as f:
401
- saved_data = json.load(f)
402
-
403
- # ๐ŸŒŸ 1. ่ฎก็ฎ—ๅนถๅ†™ๅ…ฅ load_time๏ผˆๅชๅœจ็ฌฌไธ€ๆฌกๆ‹ฆๆˆชๆ—ถ่ฎก็ฎ—๏ผ‰
404
- if "load_time" not in saved_data:
405
- load_start = saved_data.get("load_start", time.time())
406
- load_time = round(time.time() - load_start, 2)
407
-
408
- saved_data["load_time"] = load_time
409
- print(f"[ๆŽข้’ˆ] ๐ŸŽฏ ๆˆๅŠŸ่ฎก็ฎ—ๅนถๅ†™ๅ…ฅ load_time: {load_time}s", flush=True)
410
-
411
- # ๐ŸŒŸ 2. ๅผบๅˆถๆ›ดๆ–ฐ็‰ฉ็†ๆ–‡ไปถ็š„ phase ็Šถๆ€ไธบ Sampling
412
- # ๅช่ฆ่ฟ˜ๅœจๆ‰ง่กŒๆต้˜ถๆฎต๏ผŒๅฐฑๅผบ่กŒๅˆ‡ๅˆฐ Sampling๏ผŒ่ฎฉๅ‰็ซฏ UI ็ซ‹ๅˆปๅ“ๅบ”
413
- saved_data["phase"] = "๐ŸŽจ Running KSampler..."
414
-
415
- with open(metric_file, "w") as f:
416
- json.dump(saved_data, f)
417
- except Exception as e:
418
- print(f"[ๆŽข้’ˆ] โŒ ๆ›ดๆ–ฐ็‰ฉ็†ๆ–‡ไปถๅคฑ่ดฅ: {e}", flush=True)
419
- # ๐Ÿ‘†๐Ÿ‘†๐Ÿ‘† ๆ ธๅฟƒ่กฅไธ็ป“ๆŸ ๐Ÿ‘†๐Ÿ‘†๐Ÿ‘†
420
-
421
- percent = int((self.n / self.total) * 100)
422
- current_step_msg = f"๐ŸŽจ Rendering: {self.n}/{self.total} steps ({percent}%)"
423
- tasks_status[_current_generation_task_id]["message"] = current_step_msg
424
-
425
- # ๐ŸŒŸ ๆ ธๅฟƒไฟฎๅค๏ผšๆŠŠ็œŸๅฎž็š„่ฟ›ๅบฆๅ†™่ฟ›็‰ฉ็†ๆ–‡ไปถ๏ผŒ่€Œไธๆ˜ฏๅชๅ†™ไธ€ๅฅๅนฒๅทดๅทด็š„ KSampler๏ผ
426
- try:
427
- import json
428
- import os
429
- metric_file = f"/tmp/metrics_{_current_generation_task_id}.json"
430
- if os.path.exists(metric_file):
431
- with open(metric_file, "r") as f:
432
- saved_data = json.load(f)
433
-
434
- # ็›ดๆŽฅๆŠŠๅฝ“ๅ‰ๆญฅๆ•ฐไฝœไธบ phase ๅ‘ๅ‡บๅŽป
435
- saved_data["phase"] = current_step_msg
436
-
437
- with open(metric_file, "w") as f:
438
- json.dump(saved_data, f)
439
- except Exception:
440
- pass
441
-
442
- print(f"[่ฟ›ๅบฆๆ‹ฆๆˆช] Task {_current_generation_task_id[:8]} | {self.n}/{self.total} steps ({percent}%)")
443
-
444
-
445
- def prepare_env(char_enable: bool, char_lora: str, unet_choice: str, task_id: str = None):
446
- global _current_download_task_id
447
- _current_download_task_id = task_id
448
-
449
- # ๐ŸŒŸ ๆ ธๅฟƒ๏ผš็›ดๆŽฅๅŠซๆŒๅ…จๅฑ€็š„ tqdm ๆจกๅ—๏ผŒ่€Œไธๆ˜ฏๅŽป็Œœ huggingface_hub ๆŠŠ tqdm ๆ”พๅœจไบ†ๅ“ช
450
- import tqdm.auto
451
- original_tqdm = tqdm.auto.tqdm
452
- tqdm.auto.tqdm = UnifiedProgressTqdm
453
-
454
- try:
455
- engine = UNET_CHOICES.get(unet_choice, {}).get("engine", "MOSS")
456
- if char_enable and char_lora:
457
- lora_root = MODELS / "loras"
458
- dest = lora_root / char_lora
459
- if not dest.exists():
460
- file_path = CHAR_LORA_CHOICES.get(engine, {}).get(char_lora, {}).get("path")
461
- if file_path:
462
- if task_id and task_id in tasks_status:
463
- tasks_status[task_id]["message"] = f"๐Ÿ“ฅ Downloading LoRA ({char_lora})..."
464
-
465
- hf_hub_download(repo_id=MODEL_CONFIG['char_lora_repo'], filename=file_path, local_dir=str(lora_root))
466
- src = lora_root / file_path
467
- if src.exists(): shutil.move(str(src), str(dest))
468
-
469
- if task_id and task_id in tasks_status:
470
- tasks_status[task_id]["message"] = "๐Ÿ“ฆ LoRA downloaded, loading..."
471
- finally:
472
- # ๐ŸŒŸ ่ฟ˜ๅŽŸๅ…จๅฑ€็š„ tqdm ๆจกๅ—
473
- tqdm.auto.tqdm = original_tqdm
474
- _current_download_task_id = None
475
-
476
- @spaces.GPU(duration=30)
477
- def generate(
478
- prompt: str, unet_choice: str, custom_w: int, custom_h: int,
479
- steps: int, cfg: float, seed: int, sampler: str, scheduler: str,
480
- char_enable: bool, char_lora: str, char_strength: float,
481
- *lora_states,
482
- task_id: str = None, # ๐ŸŒŸ ๅฟ…้กปๆœ‰ = None
483
- ) -> str:
484
-
485
- import time
486
- import json
487
- import os
488
-
489
- # ๐ŸŒŸ ๅฎ‰ๅ…จๅ…œๅบ•๏ผš้˜ฒๆญข ZeroGPU ่ท‘ๅพ—ๅคชๅฟซๅฏผ่‡ดๅญ—ๅ…ธ่ฟ˜ๆฒกๅปบๅฅฝ
490
- if task_id:
491
- if task_id not in tasks_status:
492
- tasks_status[task_id] = {}
493
-
494
- #q_time = max(0.0, round(time.time() - tasks_status[task_id].get("queue_start_time", time.time()), 2))
495
-
496
- # ๐ŸŒŸ ๆ‹†่งฃ่ฎก็ฎ—ๆŽ’้˜Ÿๆ—ถ้—ด
497
- now_ts = time.time()
498
- start_ts = tasks_status[task_id].get("queue_start_time", now_ts)
499
- raw_diff = now_ts - start_ts
500
-
501
- print(f"[ๆŽ’้˜ŸๆŽข้’ˆ] ๆ‹ฟๅˆฐๅกๆ—ถ้—ด: {now_ts:.2f}, ๆŽ’้˜Ÿ่ตท็‚นๆ—ถ้—ด: {start_ts:.2f}, ๅŽŸๅง‹ๅทฎๅ€ผ: {raw_diff:.2f}s", flush=True)
502
-
503
- q_time = max(0.0, round(raw_diff, 2))
504
-
505
- tasks_status[task_id]["metrics_queue"] = q_time
506
- tasks_status[task_id]["message"] = "โš™๏ธ Allocating GPU..."
507
-
508
- # ๐ŸŒŸ ๆŠŠๆŽ’้˜Ÿๆ—ถ้—ดๅ†™ๅ…ฅ็‰ฉ็†ๆ–‡ไปถ
509
- try:
510
- saved_data = {}
511
- metric_file = f"/tmp/metrics_{task_id}.json"
512
- if os.path.exists(metric_file):
513
- with open(metric_file, "r") as f:
514
- saved_data = json.load(f)
515
-
516
- saved_data["queue"] = q_time
517
- saved_data["phase"] = "Allocating GPU..."
518
- with open(metric_file, "w") as f:
519
- json.dump(saved_data, f)
520
- except Exception as e:
521
- print(f"ๅ†™ๅ…ฅ queue ๆŒ‡ๆ ‡ๅคฑ่ดฅ: {e}")
522
-
523
- print(f"[GPU ๆŽข้’ˆ] ๐ŸŸข ๆˆๅŠŸ่Žทๅ– GPU๏ผๆŽ’้˜Ÿ่€—ๆ—ถ: {q_time}s", flush=True)
524
-
525
- effective_seed = int(seed) # ็›ดๆŽฅไฝฟ็”จๅค–้ขไผ ่ฟ›ๆฅ็š„ๆœ€็ปˆ seed
526
- width, height = int(custom_w), int(custom_h)
527
- unet_info = UNET_CHOICES[unet_choice]
528
- unet_name = unet_info["filename"]
529
- engine = unet_info["engine"]
530
- engine_cfg = MODEL_CONFIG["models"].get(engine, {})
531
- base_lora_inputs = []
532
- base_choices_list = list(BASE_LORAS_CHOICES.items())
533
- for i in range(0, len(lora_states), 2):
534
- enable = lora_states[i]
535
- strength = float(lora_states[i+1])
536
- if enable and strength > 0:
537
- lora_name, lora_cfg = base_choices_list[i // 2]
538
- if lora_cfg["engine"] == engine:
539
- base_lora_inputs.append((lora_cfg["filename"], strength))
540
- try:
541
- current_model_node = "1"
542
- nodes = MODEL_CONFIG["node_types"]
543
- clip_loader_node = engine_cfg.get("clip_loader_override", nodes["clip_loader"])
544
- clip_type = engine_cfg.get("clip_type", "moss_image")
545
- clip_inputs = {"type": clip_type}
546
- te_filenames = engine_cfg.get("local_te_filename", [])
547
- if clip_loader_node == "DualCLIPLoader":
548
- if len(te_filenames) >= 2:
549
- if "clip_l" in te_filenames[0].lower():
550
- clip_inputs["clip_name1"] = te_filenames[0]
551
- clip_inputs["clip_name2"] = te_filenames[1]
552
- else:
553
- clip_inputs["clip_name1"] = te_filenames[1]
554
- clip_inputs["clip_name2"] = te_filenames[0]
555
- elif len(te_filenames) == 1:
556
- clip_inputs["clip_name1"] = te_filenames[0]
557
- else:
558
- if te_filenames: clip_inputs["clip_name"] = te_filenames[0]
559
- if "clip_device" in MODEL_CONFIG.get("loader_args", {}):
560
- clip_inputs["device"] = MODEL_CONFIG["loader_args"]["clip_device"]
561
- vae_filename = engine_cfg.get("local_vae_filename", "")
562
- workflow = {
563
- "1": {"class_type": nodes["unet_loader"], "inputs": {"unet_name": unet_name, "weight_dtype": "default"}},
564
- "2": {"class_type": clip_loader_node, "inputs": clip_inputs},
565
- "3": {"class_type": nodes["vae_loader"], "inputs": {"vae_name": vae_filename}},
566
  }
567
- node_idx = 4
568
- for lora_file, strength in base_lora_inputs:
569
- workflow[str(node_idx)] = {"class_type": nodes["lora_loader"], "inputs": {"model": _ref(current_model_node), "lora_name": lora_file, "strength_model": strength}}
570
- current_model_node = str(node_idx)
571
- node_idx += 1
572
- if char_enable and char_lora:
573
- workflow[str(node_idx)] = {"class_type": nodes["lora_loader"], "inputs": {"model": _ref(current_model_node), "lora_name": char_lora, "strength_model": char_strength}}
574
- current_model_node = str(node_idx)
575
- node_idx += 1
576
- final_prompt = prompt.strip()
577
- workflow[str(node_idx)] = {"class_type": "CLIPTextEncode", "inputs": {"clip": _ref("2"), "text": final_prompt}}
578
- workflow[str(node_idx+1)] = {"class_type": "ConditioningZeroOut", "inputs": {"conditioning": _ref(str(node_idx))}}
579
- workflow[str(node_idx+2)] = {"class_type": "EmptyLatentImage", "inputs": {"width": width, "height": height, "batch_size": 1}}
580
- workflow[str(node_idx+3)] = {"class_type": "KSampler", "inputs": {"model": _ref(current_model_node), "positive": _ref(str(node_idx)), "negative": _ref(str(node_idx+1)), "latent_image": _ref(str(node_idx+2)), "seed": effective_seed, "steps": steps, "cfg": cfg, "sampler_name": sampler, "scheduler": scheduler, "denoise": 1.0}}
581
- workflow[str(node_idx+4)] = {"class_type": "VAEDecode", "inputs": {"samples": _ref(str(node_idx+3)), "vae": _ref("3")}}
582
- workflow[str(node_idx+5)] = {"class_type": "SaveImage", "inputs": {"images": _ref(str(node_idx+4)), "filename_prefix": MODEL_CONFIG["name"].lower()}}
583
-
584
- print(f"[GPU ๆŽข้’ˆ] ๐Ÿ“ฆ ๆจกๅž‹่Š‚็‚นๆž„ๅปบๅฎŒๆฏ•๏ผŒๅ‡†ๅค‡่ฐƒ็”จ ComfyUI ๆ‰ง่กŒๅผ•ๆ“Ž...", flush=True)
585
-
586
- # ๐ŸŒŸ 2. ๅฐ† metrics ไผ ้€’่ฟ›ๆ‰ง่กŒๅ™จ
587
- return _execute_workflow(workflow, task_id)[0]
588
- except Exception as exc:
589
- print(traceback.format_exc(), flush=True)
590
- raise gr.Error(f"generation failed: {str(exc)[:500]}") from exc
591
-
592
- # ==========================================================================
593
- # ๐Ÿ–ฅ๏ธ GUI (ๅฎŒๅ…จ้”ๆญปๅฎฝๅบฆ๏ผŒๅŽปๆމๆ‰€ๆœ‰ๅŠจๆ€ CSS ๆณจๅ…ฅ)
594
- # ==========================================================================
595
- custom_css = """
596
- /* ๅŸบ็ก€้‡็ฝฎไธŽ้€ๆ˜Ž */
597
- .gradio-container {
598
- max-width: 100% !important;
599
- width: 100% !important;
600
- }
601
- .gradio-container .gr-group {
602
- background: transparent !important;
603
- padding: 16px !important;
604
- gap: 12px !important;
605
- }
606
- .gradio-container .gr-group > div,
607
- .gradio-container .form,
608
- .gradio-container .block {
609
- background: transparent !important;
610
- border: none !important;
611
- box-shadow: none !important;
612
- }
613
- .lora-row {
614
- flex-wrap: nowrap !important;
615
- align-items: center !important;
616
- }
617
- .label-textbox {
618
- min-width: 80px !important;
619
- max-width: 80px !important;
620
- white-space: nowrap !important;
621
- text-align: right !important;
622
- display: flex !important;
623
- justify-content: flex-end !important;
624
- align-items: center !important;
625
- color: var(--block-title-text-color);
626
- }
627
-
628
- .label-textbox textarea,
629
- .label-textbox input {
630
- line-height: var(--line-lg) !important;;
631
- font-weight: var(--prose-text-weight) !important;
632
- font-size: var(--text-md) !important !important;;
633
- color: var(--block-title-text-color) !important;;
634
- -webkit-text-fill-color: var(--block-title-text-color) !important; /* ่งฃๅ†ณ Safari/Chrome ็š„้ขœ่‰ฒ่ฆ†็›–้—ฎ้ข˜ */
635
- opacity: 1 !important; /* ๆœ‰ไบ› disabled ็Šถๆ€ไผš้™ไฝŽ้€ๆ˜Žๅบฆ */
636
- }
637
-
638
-
639
- .readonly-name-box {
640
- border: 1px solid var(--border-color-primary) !important;
641
- border-radius: 8px !important;
642
- background: var(--background-fill-secondary) !important;
643
- }
644
- .lora-row input[type="checkbox"] {
645
- width: 20px !important;
646
- height: 20px !important;
647
- min-width: 20px !important;
648
- max-width: 20px !important;
649
- }
650
- .lora_checkbox { max-width: 36px !important; }
651
- .lora_strength_input {
652
- min-width: 100px !important;
653
- max-width: 100px !important;
654
- flex-shrink: 0 !important;
655
- }
656
- .output-group {
657
- border: 1px solid var(--border-color-primary) !important;
658
- border-radius: 8px !important;
659
- padding: 16px !important;
660
- background: transparent !important;
661
- height: 100%;
662
- }
663
- .output-group .gr-image { border: none !important; }
664
-
665
- .selected-status-text {
666
- color: var(--body-text-color) !important;
667
- font-weight: var(--input-text-weight);
668
- font-size: clamp(8px, 1.2vw, 14px) !important;
669
- line-height: 1.2 !important;
670
- text-align: left !important;
671
- white-space: nowrap !important;
672
- overflow: hidden !important;
673
- text-overflow: ellipsis !important;
674
- margin: 0 !important;
675
- padding: 0 !important;
676
- }
677
- .selected-status-text .highlight { color: #FF7F50 !important; }
678
-
679
- /* ็”ปๅปŠๅŒบๅŸŸ */
680
- .gallery-wrapper {
681
- border: 1px solid var(--border-color-primary) !important;
682
- border-radius: 8px !important;
683
- padding: 12px !important;
684
- box-sizing: border-box !important;
685
- background: transparent !important;
686
- }
687
- .super-gallery {
688
- background: transparent !important;
689
- border: none !important;
690
- }
691
- .super-gallery .grid-wrap {
692
- gap: 12px !important;
693
- max-height: 600px !important;
694
- overflow-y: auto !important;
695
- overflow-x: hidden !important;
696
- border: none !important;
697
- padding: 0 !important;
698
- box-sizing: border-box !important;
699
- background: transparent !important;
700
- }
701
- .super-gallery .grid-wrap::-webkit-scrollbar { width: 6px !important; background: transparent !important; }
702
- .super-gallery .grid-wrap::-webkit-scrollbar-thumb { background: rgba(150, 150, 150, 0.3) !important; border-radius: 10px !important; }
703
- .super-gallery .grid-wrap:hover::-webkit-scrollbar-thumb { background: rgba(150, 150, 150, 0.7) !important; }
704
-
705
- .super-gallery .icon-button-wrapper,
706
- .super-gallery button[title="Download"],
707
- .super-gallery .tooltip,
708
- .super-gallery [role="tooltip"],
709
- .gallery-wrapper .wrap > div[style*="position: absolute"] {
710
- display: none !important;
711
- opacity: 0 !important;
712
- pointer-events: none !important;
713
- }
714
-
715
- .super-gallery .thumbnail-item {
716
- border-radius: 8px !important;
717
- overflow: hidden !important;
718
- aspect-ratio: 3 / 4 !important;
719
- background: var(--background-fill-secondary) !important;
720
- border: 3px solid transparent !important;
721
- position: relative !important;
722
- box-sizing: border-box !important;
723
- transition: border-color 0.2s ease !important;
724
- }
725
- .super-gallery .thumbnail-item img {
726
- display: block !important;
727
- width: 100% !important;
728
- height: 100% !important;
729
- object-fit: cover !important;
730
- object-position: top center !important;
731
- border: none !important;
732
- outline: none !important;
733
- }
734
- .super-gallery .thumbnail-item.selected {
735
- border: 3px solid #FF7F50 !important;
736
- z-index: 10 !important;
737
- }
738
- .super-gallery .caption-wrap {
739
- background: linear-gradient(to top, rgba(0, 0, 0, 0.85) 0%, rgba(0, 0, 0, 0) 100%) !important;
740
- border: none !important;
741
- position: absolute !important;
742
- bottom: 0 !important;
743
- left: 0 !important;
744
- right: 0 !important;
745
- padding: 20px 8px 8px 8px !important;
746
- z-index: 3 !important;
747
- box-sizing: border-box !important;
748
- }
749
- .super-gallery .caption-wrap .caption {
750
- color: #fff !important;
751
- font-size: 10px !important;
752
- text-align: left !important;
753
- white-space: nowrap !important;
754
- overflow: hidden !important;
755
- text-overflow: ellipsis !important;
756
- }
757
- .super-gallery .preview { display: none !important; }
758
-
759
- a:hover {
760
- opacity: 0.8 !important;
761
- text-decoration: underline !important;
762
- }
763
- """
764
 
765
- initial_unet = list(UNET_CHOICES.keys())[0] if UNET_CHOICES else None
766
- initial_engine = UNET_CHOICES.get(initial_unet, {}).get("engine", "moss")
767
- initial_char_strength = MODEL_CONFIG["models"].get(initial_engine, {}).get("defaults", {}).get("char_lora_strength", 1.0)
768
- all_base_lora_rows = []
769
-
770
- # ๐ŸŒŸ ๆœ€ๅค–ๅฑ‚ๅŠ ็ปŸไธ€็š„ padding๏ผŒๅผบ่กŒๆ’‘ๆปกๅ…จๅฎฝ
771
- with gr.Blocks(title=MODEL_CONFIG["ui_title"], css=custom_css) as demo:
772
- with gr.Column(elem_classes="main-container"):
773
- gr.Markdown(f"# {MODEL_CONFIG['ui_header']}")
774
-
775
- with gr.Row():
776
- # ๐ŸŸข 1. ๅทฆไพงๅ‚ๆ•ฐๅŒบ
777
- with gr.Column(scale=6):
778
- with gr.Group():
779
- unet_choice = gr.Dropdown(choices=list(UNET_CHOICES.keys()), value=initial_unet, label="Base Model")
780
- prompt = gr.Textbox(
781
- value=MODEL_CONFIG.get("default_prompt", ""),
782
- label="Prompt", lines=5
783
- )
784
-
785
- with gr.Row():
786
- custom_w = gr.Slider(1, 1536, value=1024, step=16, label="Width")
787
- custom_h = gr.Slider(1, 1536, value=1024, step=16, label="Height")
788
-
789
- with gr.Row():
790
- steps = gr.Slider(4, 50, value=MODEL_CONFIG["models"][initial_engine]["defaults"]["steps"], step=1, label="steps")
791
- cfg = gr.Slider(1.0, 10.0, value=MODEL_CONFIG["models"][initial_engine]["defaults"]["cfg"], step=0.1, label="CFG")
792
-
793
- with gr.Row():
794
- sampler = gr.Dropdown(choices=SAMPLERS, value=MODEL_CONFIG["models"][initial_engine]["defaults"]["sampler"], label="Sampler")
795
- scheduler = gr.Dropdown(choices=SCHEDULERS, value=MODEL_CONFIG["models"][initial_engine]["defaults"]["scheduler"], label="Scheduler")
796
- seed = gr.Number(value=-1, precision=0, label="Seed")
797
-
798
- #base_lora_title = gr.Markdown("โš™๏ธ Base / Turbo LoRAs")
799
-
800
- base_lora_title_html = f'''
801
- <div style="line-height: var(--line-lg); font-weight: var(--prose-text-weight) !important; font-size: var(--text-md) !important; color: var(--block-title-text-color); margin-bottom: -4px !important;">
802
- โš™๏ธ Base / Turbo LoRAs
803
- </div>
804
- '''
805
- base_lora_title = gr.HTML(value=base_lora_title_html)
806
-
807
- base_lora_components = []
808
- if BASE_LORAS_CHOICES:
809
- for name, cfg_item in BASE_LORAS_CHOICES.items():
810
- is_visible = (cfg_item["engine"] == initial_engine)
811
- with gr.Row(elem_classes="lora-row", visible=is_visible) as row:
812
- cb = gr.Checkbox(value=cfg_item["defaultEnable"], container=False, scale=0, min_width=30, elem_classes="lora_checkbox")
813
- name_box = gr.Textbox(value=name, show_label=False, container=False, interactive=False, scale=8)
814
- gr.Textbox(value="Strength", show_label=False, container=False, interactive=False, text_align="right", elem_classes="label-textbox", scale=1, min_width=80)
815
- num = gr.Number(value=1.0, show_label=False, container=True, step=0.05, scale=0, elem_classes="lora_strength_input", min_width=100)
816
-
817
- base_lora_components.extend([cb, num])
818
- all_base_lora_rows.append((cfg_item["engine"], row))
819
-
820
- # ๐ŸŸก 2. ไธญ้—ด็”ปๅปŠๅŒบ
821
- with gr.Column(scale=6):
822
- with gr.Group():
823
- link_url = "https://huggingface.co/spaces/waveshaker/loras_gallery"
824
-
825
- gallery_title_html = f'''
826
- <div style="line-height: var(--line-lg); font-weight: var(--prose-text-weight) !important; font-size: var(--text-md) !important; color: var(--block-title-text-color); margin-bottom: -4px !important;">
827
- ๐ŸŽญ Character LoRA (Click to select)
828
- <span style="color: var(--border-color-primary); margin: 0 8px;">|</span>
829
- <a href="{link_url}" target="_blank" style="color: #FF7F50; text-decoration: none; transition: opacity 0.2s;" onmouseover="this.style.opacity=0.8" onmouseout="this.style.opacity=1">
830
- Browse in ๐Ÿ–ผ๏ธ Gallery โ†—
831
- </a>
832
- </div>
833
- '''
834
- gr.HTML(value=gallery_title_html)
835
-
836
- with gr.Row(elem_classes="lora-row", visible=False) as selected_row:
837
- char_enable = gr.Checkbox(value=True, container=False, scale=0, min_width=30, elem_classes="lora_checkbox")
838
- selected_text = gr.HTML(value='<div class="selected-status-text">None</div>', scale=1)
839
- gr.Textbox(value="Strength", show_label=False, container=False, interactive=False, text_align="right", elem_classes="label-textbox", scale=0, min_width=80)
840
- char_strength = gr.Number(value=initial_char_strength, show_label=False, container=True, step=0.05, scale=0, elem_classes="lora_strength_input", min_width=100)
841
-
842
- with gr.Column(elem_classes="gallery-wrapper"):
843
- char_gallery = gr.Gallery(
844
- label="",
845
- show_label=False,
846
- columns=5,
847
- height=400,
848
- object_fit="cover",
849
- allow_preview=False,
850
- elem_classes="super-gallery"
851
- )
852
-
853
- char_lora_state = gr.State(value="")
854
- url_lora_state = gr.State(value="")
855
-
856
- # ๐Ÿ”ต 3. ๅณไพงๅ‡บๅ›พๅŒบ
857
- with gr.Column(scale=5):
858
- with gr.Group(elem_classes="output-group"):
859
- output = gr.Image(label="output", height=600)
860
- status_text = gr.Markdown("", visible=False)
861
- button = gr.Button("generate", variant="primary", size="lg")
862
-
863
-
864
- # ==========================================
865
- # UNet ๅˆ‡ๆข่”ๅŠจ
866
- # ==========================================
867
- def on_unet_change(selected_unet, preferred_lora=None):
868
- engine = UNET_CHOICES.get(selected_unet, {}).get("engine", "MOSS")
869
- row_updates = [gr.update(visible=(re == engine)) for re, _ in all_base_lora_rows]
870
- has_base_lora = any(cfg["engine"] == engine for cfg in BASE_LORAS_CHOICES.values())
871
- title_update = gr.update(visible=has_base_lora)
872
-
873
- new_char_dict = CHAR_LORA_CHOICES.get(engine, {})
874
- gallery_items = []
875
- for filename, info in new_char_dict.items():
876
- display_name = filename.replace(".safetensors", "")
877
- gallery_items.append((info["webp"], display_name))
878
-
879
- engine_defaults = MODEL_CONFIG["models"].get(engine, {}).get("defaults", {})
880
- steps_update = gr.update(value=engine_defaults.get("steps", 8))
881
- cfg_update = gr.update(value=engine_defaults.get("cfg", 1.0))
882
- sampler_update = gr.update(value=engine_defaults.get("sampler", "euler"))
883
- scheduler_update = gr.update(value=engine_defaults.get("scheduler", "simple"))
884
- char_strength_update = gr.update(value=engine_defaults.get("char_lora_strength", 1.0))
885
-
886
- default_lora = None
887
- if preferred_lora and preferred_lora in new_char_dict:
888
- default_lora = preferred_lora
889
- elif gallery_items:
890
- default_lora = list(new_char_dict.keys())[0]
891
-
892
- # ๐ŸŒŸ ๆ ธๅฟƒไฟฎๅค๏ผšไธ่ฆๆณจๅ…ฅ CSS๏ผŒ่€Œๆ˜ฏๅ‘Š่ฏ‰ Gallery ๅ“ชไธช index ่ขซ้€‰ไธญไบ†๏ผ
893
- selected_index = None
894
- if default_lora:
895
- selected_index = list(new_char_dict.keys()).index(default_lora)
896
-
897
- gallery_update = gr.update(value=gallery_items, selected_index=selected_index)
898
-
899
- if default_lora:
900
- display_name = default_lora.replace(".safetensors", "")
901
- card_update = gr.update(visible=True)
902
- text_html = f'<div class="selected-status-text"><span class="highlight">{display_name}</span></div>'
903
- text_update = gr.update(value=text_html)
904
- state_update = gr.update(value=default_lora)
905
- else:
906
- card_update = gr.update(visible=False)
907
- text_update = gr.update(value='<div class="selected-status-text">None</div>')
908
- state_update = gr.update(value="")
909
-
910
- # ๆณจๆ„๏ผš่ฟ™้‡ŒๅŽปๆމไบ†ๅŠจๆ€ CSS ้‚ฃไธช่ฟ”ๅ›ž้กน
911
- return [title_update, *row_updates, gallery_update, steps_update, cfg_update, sampler_update, scheduler_update, char_strength_update, card_update, text_update, state_update]
912
-
913
- unet_choice.change(
914
- fn=on_unet_change,
915
- inputs=[unet_choice, url_lora_state],
916
- # ๅŽปๆމไบ† dynamic_highlight_css
917
- outputs=[base_lora_title,*[row for _, row in all_base_lora_rows], char_gallery, steps, cfg, sampler, scheduler, char_strength, selected_row, selected_text, char_lora_state]
918
- )
919
-
920
- # ==========================================
921
- # Gallery ็‚นๅ‡ป้€‰ไธญไบ‹ไปถ
922
- # ==========================================
923
- def on_gallery_select(evt: gr.SelectData, selected_unet):
924
- engine = UNET_CHOICES.get(selected_unet, {}).get("engine", "MOSS")
925
- char_list = list(CHAR_LORA_CHOICES.get(engine, {}).keys())
926
-
927
- if evt.index < len(char_list):
928
- selected_lora = char_list[evt.index]
929
- display_name = selected_lora.replace(".safetensors", "")
930
- text_html = f'<div class="selected-status-text"><span class="highlight">{display_name}</span></div>'
931
- return gr.update(visible=True), gr.update(value=text_html), gr.update(value=selected_lora)
932
- return gr.update(visible=False), gr.update(value='<div class="selected-status-text">None</div>'), gr.update(value="")
933
-
934
- char_gallery.select(
935
- fn=on_gallery_select, inputs=[unet_choice],
936
- outputs=[selected_row, selected_text, char_lora_state]
937
- )
938
-
939
- # ==========================================
940
- # URL ไผ ๅ‚่งฃๆž
941
- # ==========================================
942
- def on_page_load(request: gr.Request):
943
- try:
944
- load_char_loras_from_repo()
945
- except Exception as e:
946
- print(f"โš ๏ธ ้กต้ขๅŠ ่ฝฝๆ—ถๅˆทๆ–ฐ LoRA ๅคฑ่ดฅ๏ผŒๅฐ†ไฝฟ็”จๆ—ง็ผ“ๅญ˜ๆ•ฐๆฎ: {e}")
947
-
948
- model_param = request.query_params.get("model", "")
949
- lora_param = request.query_params.get("lora", "")
950
- target_unet = initial_unet
951
- if model_param:
952
- mapped_engine = MODEL_CONFIG["json_keys"]["root_mapping"].get(model_param.lower(), model_param)
953
- for k, v in UNET_CHOICES.items():
954
- if v["engine"] == mapped_engine:
955
- target_unet = k
956
- break
957
-
958
- return target_unet, lora_param
959
-
960
- demo.load(
961
- fn=on_page_load, inputs=None, outputs=[unet_choice, url_lora_state]
962
- ).then(
963
- fn=on_unet_change, inputs=[unet_choice, url_lora_state],
964
- # ๅŽปๆމไบ† dynamic_highlight_css
965
- outputs=[
966
- base_lora_title, *[row for _, row in all_base_lora_rows],
967
- char_gallery, steps, cfg, sampler, scheduler,
968
- char_strength, selected_row, selected_text, char_lora_state
969
- ]
970
- )
971
-
972
- # ==========================================
973
- # ็Šถๆ€ๆœบ๏ผšไธคๆญฅ่ตฐ (ไธ‹่ฝฝ -> ็”Ÿๆˆ)
974
- # ==========================================
975
- button.click(
976
- fn=lambda: (gr.update(visible=True, value="โณ Selected LoRA is Downloading..."), gr.update(interactive=False)),
977
- inputs=[], outputs=[status_text, button]
978
- ).then(
979
- fn=prepare_env, inputs=[char_enable, char_lora_state, unet_choice], outputs=[],
980
- ).then(
981
- fn=lambda: gr.update(visible=True, value="๐Ÿš€ Generating..."), inputs=[], outputs=[status_text]
982
- ).then(
983
- fn=generate,
984
- inputs=[
985
- prompt, unet_choice, custom_w, custom_h,
986
- steps, cfg, seed, sampler, scheduler,
987
- char_enable, char_lora_state, char_strength,
988
- *base_lora_components
989
- ],
990
- outputs=[output]
991
- ).then(
992
- fn=lambda: (gr.update(visible=True, value="โœ… Done!"), gr.update(interactive=True)),
993
- inputs=[], outputs=[status_text, button]
994
- )
995
-
996
- demo.queue()
997
-
998
- # ==========================================================================
999
- # ๐ŸŒ API ่ทฏ็”ฑไธŽๆž้€ŸๆŒ‚่ฝฝ
1000
- # ==========================================================================
1001
- import uuid
1002
- import base64
1003
- import uvicorn
1004
- import os
1005
- from fastapi.responses import RedirectResponse
1006
- from fastapi import FastAPI, BackgroundTasks
1007
- from fastapi.middleware.cors import CORSMiddleware
1008
- from fastapi.responses import HTMLResponse
1009
- from pydantic import BaseModel
1010
- from gradio.routes import mount_gradio_app
1011
-
1012
- class GenerateRequest(BaseModel):
1013
- prompt: str
1014
- unet_choice: str
1015
- custom_w: int = 1024
1016
- custom_h: int = 1024
1017
- steps: int = 8
1018
- cfg: float = 1.0
1019
- seed: int = -1
1020
- sampler: str = "euler"
1021
- scheduler: str = "simple"
1022
- char_enable: bool = True
1023
- char_lora: str = ""
1024
- char_strength: float = 1.0
1025
- base_lora_states: list = []
1026
 
1027
- import threading
 
 
 
 
 
 
 
 
 
1028
 
1029
- # ๅœจๆ–‡ไปถ้กถ้ƒจๅฎšไน‰
1030
- _model_switch_lock = threading.Lock()
 
 
 
 
 
 
1031
 
1032
- def background_generate_task(task_id: str, req: GenerateRequest):
1033
- global _current_generation_task_id, _last_used_architecture, _last_used_unet
1034
- import time
1035
- import random
1036
- import json
1037
- import os
 
 
 
 
1038
 
1039
- _current_generation_task_id = task_id
1040
- tasks_status[task_id] = {"status": "processing", "message": "โณ Queue for GPU..."}
1041
-
1042
- try:
1043
- # ๐ŸŒŸ 1. ่ฎฐๅฝ•ไธ‹่ฝฝ่€—ๆ—ถ
1044
- download_start = time.time()
1045
- prepare_env(req.char_enable, req.char_lora, req.unet_choice, task_id=task_id)
1046
- real_download_time = round(time.time() - download_start, 2)
1047
-
1048
- current_arch = UNET_CHOICES.get(req.unet_choice, {}).get("engine", "unknown")
1049
-
1050
- # 2. ๅˆคๆ–ญๆ˜ฏๅฆ้œ€่ฆ้‡่ฝฝๆจกๅž‹
1051
- need_heavy_load = (_last_used_architecture != current_arch) or (_last_used_unet != req.unet_choice)
1052
-
1053
- if need_heavy_load:
1054
- with _model_switch_lock:
1055
- # ๅŒ้‡ๆฃ€ๆŸฅ๏ผŒ้˜ฒๆญขๆŽ’้˜ŸๆœŸ้—ดๅˆซ็š„็บฟ็จ‹ๅทฒ็ปๅŠ ่ฝฝๅฅฝไบ†
1056
- if _last_used_architecture != current_arch:
1057
- tasks_status[task_id]["message"] = f"๐Ÿ“ฆ Queue for GPU... (Loading Base Model: {current_arch.upper()})"
1058
- _last_used_architecture = current_arch
1059
- _last_used_unet = req.unet_choice
1060
- elif _last_used_unet != req.unet_choice:
1061
- tasks_status[task_id]["message"] = "๐Ÿ”„ Queue for GPU... (Swapping Model Checkpoint)"
1062
- _last_used_unet = req.unet_choice
1063
- else:
1064
- # ๅœบๆ™ฏ 3๏ผšLoRA ๅˆ‡ๆข๏ผŒ็ง’ๅผ€๏ผŒ็›ดๆŽฅๆธฒๆŸ“๏ผŒไธๆŽ’้˜Ÿ๏ผ
1065
- tasks_status[task_id]["message"] = "๐ŸŽจ Rendering image (GPU processing)..."
1066
 
1067
- # ็”Ÿๆˆ้šๆœบ็งๅญ
1068
- final_seed = req.seed if req.seed >= 0 else random.randint(0, 2**32 - 1)
1069
- tasks_status[task_id]["seed"] = final_seed
1070
-
1071
- # ๐ŸŒŸ ๆ ธๅฟƒไฟฎๆ”น๏ผšๆŠŠ่ตทๅง‹ๆ—ถ้—ด่ฎฐๅฝ•ๅœจ tasks_status ้‡Œ๏ผŒ่ฎฉ generate ๅŽปๅ–๏ผ
1072
- tasks_status[task_id]["queue_start_time"] = time.time()
1073
- tasks_status[task_id]["message"] = "โณ Requesting GPU resources..."
1074
- print(f"[GPU ๆŽข้’ˆ] ๐Ÿ”ด ๅ‘่ตท Request๏ผŒ็”ณ่ฏท GPU ่ต„ๆบ...", flush=True)
1075
 
1076
- # 4. ่ฐƒ็”จ generate๏ผŒ่ฎฉ generate ๆŠŠ queue ๅ’Œ workflow ๅกซ่ฟ›ๅŽป
1077
- image_path = generate(
1078
- req.prompt, req.unet_choice, req.custom_w, req.custom_h,
1079
- req.steps, req.cfg, final_seed, req.sampler, req.scheduler,
1080
- req.char_enable, req.char_lora, req.char_strength,
1081
- *req.base_lora_states,
1082
- task_id=task_id,
1083
- )
1084
-
1085
- tasks_status[task_id]["message"] = "โœจ Processing output..."
1086
- with open(image_path, "rb") as f:
1087
- img_base64 = base64.b64encode(f.read()).decode("utf-8")
1088
 
1089
- # ==========================================================
1090
- # ๐ŸŒŸ ็ปˆๆžๆ–นๆกˆ๏ผšไปŽๆ–‡ไปถ่ฏปๅ–็œŸๅฎžๆ—ถ้—ด
1091
- # ==========================================================
1092
- final_queue = 0
1093
- final_workflow = 0
1094
- final_load_time = 0 # ๐Ÿ‘ˆ ๆ ธๅฟƒๆ–ฐๅขž๏ผšๆๅ–ๅŠ ่ฝฝ่€—ๆ—ถ
1095
- metric_file = f"/tmp/metrics_{task_id}.json"
1096
-
1097
- if os.path.exists(metric_file):
1098
- try:
1099
- with open(metric_file, "r") as f:
1100
- saved_metrics = json.load(f)
1101
- final_queue = saved_metrics.get("queue", 0)
1102
- final_workflow = saved_metrics.get("workflow", 0)
1103
- final_load_time = saved_metrics.get("load_time", 0) # ๐Ÿ‘ˆ ๆ ธๅฟƒๆ–ฐๅขž
1104
- os.remove(metric_file) # ็”จๅฎŒๅˆ ๆމ
1105
- except Exception as e:
1106
- print(f"่ฏปๅ–ๆŒ‡ๆ ‡ๆ–‡ไปถๅคฑ่ดฅ: {e}")
1107
-
1108
- # ==========================================================
1109
- # ๐ŸŒŸ 6. ๆ‰“ๅŒ…ๆœ€็ปˆ็ป“ๆžœ่ฟ”ๅ›ž
1110
- # ==========================================================
1111
- tasks_status[task_id] = {
1112
- "status": "success",
1113
- "image": f"data:image/png;base64,{img_base64}",
1114
- "seed": final_seed,
1115
- "metrics": {
1116
- "download": real_download_time,
1117
- "queue": final_queue,
1118
- "workflow": final_workflow,
1119
- "load_time": final_load_time, # ๐Ÿ‘ˆ ๆ ธๅฟƒๆ–ฐๅขž๏ผšๅ‘็ป™ๅ‰็ซฏ
1120
- "steps": req.steps
1121
- }
1122
  }
1123
- except Exception as e:
1124
- tasks_status[task_id] = {"status": "error", "message": str(e)}
1125
- finally:
1126
- _current_generation_task_id = None
1127
 
1128
- # 1. ็บฏๅ‡€็š„ FastAPI
1129
- app = FastAPI()
 
 
 
 
 
 
1130
 
1131
- app.add_middleware(
1132
- CORSMiddleware,
1133
- allow_origins=["*"],
1134
- allow_credentials=True,
1135
- allow_methods=["*"],
1136
- allow_headers=["*"],
1137
- )
1138
 
1139
- from model_config import CORE_STATUS # ๐ŸŒŸ ๅฏผๅ…ฅๅˆšๆ‰ๅ†™ๅฅฝ็š„็Šถๆ€
 
 
 
 
 
1140
 
1141
- async def serve_html():
1142
- # ็›ดๆŽฅ่ฏปๅ–ๅนถ่ฟ”ๅ›ž HTML
1143
- frontend_url = MODEL_CONFIG.get("frontend_url", "#")
1144
- html_path = os.path.join(os.path.dirname(__file__), "index.html")
1145
- with open(html_path, "r", encoding="utf-8") as f:
1146
- html_content = f.read()
1147
-
1148
- html_content = html_content.replace("__FRONTEND_URL__", frontend_url)
1149
- html_content = html_content.replace("__DEV_MODE__", "True" if CORE_STATUS["dev_mode"] else "False")
1150
- html_content = html_content.replace("__TARGET_MODEL__", CORE_STATUS["target_model"])
1151
- html_content = html_content.replace("__GRADIO_UI__", "Enabled" if CORE_STATUS["gradio_ui"] else "Disabled")
1152
-
1153
- return HTMLResponse(content=html_content)
1154
-
1155
-
1156
- # 2. ๆŒ‚่ฝฝ API ่ทฏ็”ฑ๏ผˆไธฅๆ ผๅธฆไธŠ /api ๅ‰็ผ€๏ผ‰
1157
- @app.post("/api/generate")
1158
- async def handle_generate(req: GenerateRequest, background_tasks: BackgroundTasks):
1159
- if req.char_enable and req.char_lora:
1160
- if not req.char_lora.endswith(".safetensors"):
1161
- req.char_lora = f"{req.char_lora}.safetensors"
1162
 
1163
- task_id = str(uuid.uuid4())
1164
-
1165
- # ็ซ‹ๅˆปๅ†™ๅ…ฅๅˆๅง‹็Šถๆ€
1166
- tasks_status[task_id] = {"status": "queued", "message": "โณ ไปปๅŠกๅทฒ่ฟ›ๅ…ฅ้˜Ÿๅˆ—..."}
1167
-
1168
- background_tasks.add_task(background_generate_task, task_id, req)
1169
- return {"task_id": task_id, "status": "queued"}
1170
-
1171
- @app.get("/api/status/{task_id}")
1172
- async def check_status(task_id: str):
1173
- status_data = tasks_status.get(task_id, {"status": "not_found"})
1174
-
1175
- # ๐ŸŒŸ ๅช่ฆไปปๅŠก่ฟ˜ๅœจๅค„็†ไธญ๏ผŒไผ˜ๅ…ˆ่ฏปๅ–็‰ฉ็†ๆ–‡ไปถ้‡Œ็š„ phase
1176
- if status_data.get("status") == "processing":
1177
- metric_file = f"/tmp/metrics_{task_id}.json"
1178
- if os.path.exists(metric_file):
1179
- try:
1180
- with open(metric_file, "r") as f:
1181
- file_data = json.load(f)
1182
- if "phase" in file_data:
1183
- status_data["message"] = file_data["phase"]
1184
- except Exception:
1185
- pass
1186
-
1187
- return status_data
1188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1189
 
1190
- @app.get("/api/models")
1191
- async def get_models():
1192
- models_data = {}
1193
- global_default_prompt = MODEL_CONFIG.get("default_prompt", "")
1194
 
1195
- for unet_name, unet_info in UNET_CHOICES.items():
1196
- engine = unet_info["engine"]
1197
- engine_name = MODEL_CONFIG["models"].get(engine, {}).get("name", engine)
1198
-
1199
- if " | " in unet_name:
1200
- model_specific_name = unet_name.split(" | ", 1)[1]
1201
- else:
1202
- model_specific_name = unet_name
1203
-
1204
- display_unet_name = f"{engine_name} | {model_specific_name}"
1205
-
1206
- base_loras_for_engine = []
1207
- for name, cfg in BASE_LORAS_CHOICES.items():
1208
- if cfg["engine"] == engine:
1209
- base_loras_for_engine.append({
1210
- "name": name,
1211
- "filename": cfg["filename"],
1212
- "defaultEnable": cfg.get("defaultEnable", False)
1213
- })
1214
-
1215
- engine_cfg = MODEL_CONFIG["models"].get(engine, {})
1216
-
1217
- # ๐ŸŒŸ ๆ ธๅฟƒ้€ป่พ‘๏ผšๅฆ‚ๆžœๆœ‰ไธ“ๅฑž override ๅฐฑ็”จ๏ผŒๅฆๅˆ™็”จๅ…จๅฑ€
1218
- final_prompt = engine_cfg.get("default_prompt_override", global_default_prompt)
1219
 
1220
- models_data[display_unet_name] = {
1221
- "engine": engine,
1222
- "defaults": engine_cfg.get("defaults", {}),
1223
- "base_loras": base_loras_for_engine,
1224
- "default_prompt": final_prompt # ๐ŸŒŸ ็ปŸไธ€ๅ‘็ป™ๅ‰็ซฏ
1225
  }
1226
 
1227
- return {"models": models_data}
1228
-
1229
- @app.get("/api/loras")
1230
- async def get_loras():
1231
- return {"loras": CHAR_LORA_CHOICES}
1232
-
1233
- @app.get("/api/config")
1234
- async def get_sampler_config():
1235
- return {
1236
- "samplers": SAMPLERS,
1237
- "schedulers": SCHEDULERS,
1238
- "root_mapping": MODEL_CONFIG["json_keys"]["root_mapping"], # ๐ŸŒŸ ๆŠŠ root_mapping ็›ดๆŽฅๅ‘็ป™ๅ‰็ซฏ
1239
- "default_prompt": MODEL_CONFIG.get("default_prompt", ""),
1240
- "frontend_url": MODEL_CONFIG.get("frontend_url", "")
1241
- }
1242
-
1243
 
1244
- @app.get("/api/health")
1245
- async def health_check():
1246
- # ๅช่ฆ่ฟ™ไธชๆŽฅๅฃ่ƒฝ่ฟ”ๅ›ž๏ผŒ่ฏดๆ˜Ž FastAPI ๅทฒ็ปๅฏๅŠจไบ†
1247
- return {"status": "ok"}
1248
-
1249
- # ==========================================================================
1250
- # ๐Ÿš€ ๅฏๅŠจ็ญ–็•ฅ๏ผšๅŒไฟ้™ฉๆŒ‚่ฝฝๆณ• (ๅทฒไฟฎๅค Python 3.12 Event Loop ๅดฉๆบƒ Bug)
1251
- # ==========================================================================
1252
- if __name__ == "__main__":
1253
- import threading
1254
- import time
1255
- import uvicorn
1256
-
1257
- # ๅ…ณ้—ญ SSR ้˜ฒๅ†ฒ็ช
1258
- os.environ["GRADIO_SSR_MODE"] = "false"
1259
-
1260
- # ๐ŸŒŸ ็›ดๆŽฅๅฏผๅ…ฅ model_config ้‡Œ็š„ๅผ€ๅ…ณ
1261
- from model_config import ENABLE_GRADIO_UI
1262
-
1263
- if ENABLE_GRADIO_UI:
1264
- print("๐Ÿš€ Gradio UI is ENABLED. Mounting UI to root path...")
1265
- app = mount_gradio_app(
1266
- app=app,
1267
- blocks=demo,
1268
- path="/",
1269
- )
1270
-
1271
- def trigger_gpu_scan():
1272
- demo.launch(
1273
- server_name="127.0.0.1",
1274
- server_port=7861,
1275
- share=False,
1276
- prevent_thread_lock=True,
1277
- css=custom_css,
1278
- )
1279
- else:
1280
- print("๐Ÿš€ Gradio UI is DISABLED. Running in pure API mode...")
1281
 
1282
- # ๐ŸŒŸ ๆ ธๅฟƒไฟฎๅค 2๏ผšๅ…ณ้—ญ Gradio ๆ—ถ๏ผŒๆ‰‹ๅŠจๆŠŠ HTML ๆŒ‚่ฝฝๅˆฐ `/` ่ทฏๅพ„
1283
- # (ๆณจๆ„๏ผš่ฟ™้‡Œ็š„ serve_html ๅ‡ฝๆ•ฐๅฐฑๆ˜ฏๆˆ‘ไปฌๅœจไธŠไธ€ๆญฅ้‡Œๅ‰ฅ็ฆปๅ‡บๆฅ็š„้‚ฃไธชๅ‡ฝๆ•ฐ)
1284
- app.add_api_route("/", serve_html, methods=["GET"], response_class=HTMLResponse)
1285
-
1286
- def trigger_gpu_scan():
1287
- demo.launch(
1288
- server_name="127.0.0.1",
1289
- server_port=7861,
1290
- share=False,
1291
- prevent_thread_lock=True,
1292
- )
1293
-
1294
- print("๐Ÿš€ Triggering ZeroGPU scan...")
1295
- # ๅฐ†ๆ‰ซๆ็บฟ็จ‹ไฝœไธบๅฎˆๆŠค็บฟ็จ‹ๅฏๅŠจ
1296
- threading.Thread(target=trigger_gpu_scan, daemon=True).start()
1297
-
1298
- # ๐ŸŒŸ ๆ ธๅฟƒไฟฎๅค 1๏ผš็จๅพฎๅปถ้•ฟไผ‘็œ ๏ผŒ็กฎไฟ ZeroGPU ๆ‰ซๆๅฎŒๅ…จ็ป“ๆŸไธ”็ซฏๅฃ้‡Šๆ”พ
1299
- time.sleep(4)
1300
-
1301
- # ๐ŸŒŸ ๆ ธๅฟƒไฟฎๅค 2๏ผšไฝฟ็”จ Uvicorn Server ๅฏน่ฑกๆ˜พๅผๆŽฅ็ฎกไบ‹ไปถๅพช็Žฏ๏ผŒ้ฟๅ…็›ดๆŽฅ uvicorn.run() ๅฏผ่‡ด็š„่ทจ็บฟ็จ‹ๆญป้”
1302
- print("๐ŸŒ Starting main API Gateway on 7860...")
1303
- config = uvicorn.Config(app, host="0.0.0.0", port=7860, log_level="info")
1304
- server = uvicorn.Server(config)
1305
-
1306
- # ๅฏๅŠจไธปๆœๅŠก
1307
- server.run()
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Universal Model Character LoRA Studio - Core</title>
7
+ <style>
8
+ :root {
9
+ --bg-color: #121212;
10
+ --panel-bg: #1e1e1e;
11
+ --text-main: #e0e0e0;
12
+ --text-muted: #888;
13
+ --accent: #FF7F50;
14
+ --border: #444;
15
+ --primary-btn: #2563eb;
16
+ --primary-btn-hover: #1d4ed8;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ * { box-sizing: border-box; }
20
+
21
+ body {
22
+ background-color: var(--bg-color);
23
+ color: var(--text-main);
24
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
25
+ display: flex;
26
+ justify-content: center;
27
+ align-items: center;
28
+ min-height: 100vh;
29
+ margin: 0;
30
+ padding: 20px;
31
+ line-height: 1.5;
32
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ .container {
35
+ background-color: var(--panel-bg);
36
+ border: 1px solid var(--border);
37
+ border-radius: 12px;
38
+ padding: 24px;
39
+ width: 100%;
40
+ max-width: 300px;
41
+ text-align: center;
42
+ box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
43
+ }
44
 
45
+ h1 {
46
+ color: #e0e0e0;
47
+ font-size: 14px;
48
+ font-weight: 500;
49
+ margin-top: 0;
50
+ margin-bottom: 8px;
51
+ text-align: left;
52
+ }
53
 
54
+ .status-dot {
55
+ display: inline-block;
56
+ width: 8px;
57
+ height: 8px;
58
+ background-color: #00C851;
59
+ border-radius: 50%;
60
+ margin-right: 6px;
61
+ box-shadow: 0 0 10px rgba(0, 200, 81, 0.8);
62
+ animation: breathe-green 2s infinite ease-in-out;
63
+ }
64
 
65
+ @keyframes breathe-green {
66
+ 0%, 100% { opacity: 0.6; box-shadow: 0 0 4px rgba(0, 200, 81, 0.2); }
67
+ 50% { opacity: 1; box-shadow: 0 0 12px rgba(0, 200, 81, 1); }
68
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ p {
71
+ color: var(--text-muted);
72
+ font-size: 11px;
73
+ margin-bottom: 24px;
74
+ font-weight: 400;
75
+ text-align: left;
76
+ }
 
77
 
78
+ /* ==========================================
79
+ ๐ŸŒŸ ๆž็ฎ€็ปˆ็ซฏ้ฃŽ SYSTEM STATUS
80
+ ========================================== */
81
+ .sys-status {
82
+ background-color: #121212;
83
+ border: 1px solid #333;
84
+ border-radius: 6px;
85
+ padding: 12px;
86
+ margin-bottom: 20px;
87
+ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
88
+ text-align: left;
89
+ }
90
 
91
+ .sys-status .title {
92
+ font-size: 9px;
93
+ color: var(--text-muted);
94
+ letter-spacing: 1px;
95
+ margin-bottom: 10px;
96
+ text-transform: uppercase;
97
+ border-bottom: 1px dashed #333;
98
+ padding-bottom: 6px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  }
 
 
 
 
100
 
101
+ .sys-row {
102
+ display: flex;
103
+ justify-content: space-between;
104
+ align-items: flex-start;
105
+ font-size: 10px;
106
+ margin-bottom: 6px;
107
+ color: var(--text-muted);
108
+ }
109
 
110
+ .sys-val {
111
+ color: var(--text-main);
112
+ font-weight: 600;
113
+ }
 
 
 
114
 
115
+ /* ๐ŸŒŸ ๆจกๅž‹ไธ“ๅฑžๅŒบๅ— */
116
+ .sys-models-block {
117
+ margin-top: 8px;
118
+ border-top: 1px dashed #333;
119
+ padding-top: 8px;
120
+ }
121
 
122
+ .sys-label {
123
+ font-size: 10px;
124
+ color: var(--text-muted);
125
+ margin-bottom: 4px;
126
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
+ .sys-models-val {
129
+ font-size: 10px;
130
+ color: var(--text-main);
131
+ font-weight: 600;
132
+ line-height: 1.4;
133
+ word-break: break-word;
134
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
+ .btn {
137
+ display: inline-flex;
138
+ align-items: center;
139
+ justify-content: center;
140
+ width: 100%;
141
+ padding: 8px 12px;
142
+ background-color: var(--accent);
143
+ color: white;
144
+ text-decoration: none;
145
+ border-radius: 6px;
146
+ font-weight: 600;
147
+ font-size: 13px;
148
+ transition: background-color 0.2s ease, transform 0.1s ease;
149
+ border: 1px solid transparent;
150
+ }
151
 
152
+ .btn:hover {
153
+ background-color: rgb(255, 102, 51);
154
+ transform: translateY(-1px);
155
+ }
156
 
157
+ .footer {
158
+ margin-top: 20px;
159
+ padding-top: 16px;
160
+ border-top: 1px solid var(--border);
161
+ font-size: 11px;
162
+ color: var(--text-muted);
163
+ display: flex;
164
+ justify-content: center;
165
+ gap: 15px;
166
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
+ .footer a {
169
+ color: var(--text-muted);
170
+ text-decoration: none;
171
+ transition: color 0.2s;
 
172
  }
173
 
174
+ .footer a:hover { color: var(--text-main); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
+ </style>
177
+ </head>
178
+ <body>
179
+ <div class="container">
180
+ <h1>Universal Model LoRA Studio - Core</h1>
181
+ <p><span class="status-dot"></span> Backend is Online</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
+ <!-- ๐ŸŒŸ ็ปˆ็ซฏ้ฃŽๆ ผ็Šถๆ€้ขๆฟ -->
184
+ <div class="sys-status">
185
+ <div class="title">System Status</div>
186
+
187
+ <div class="sys-row">
188
+ <span>Dev Mode</span>
189
+ <span class="sys-val">__DEV_MODE__</span>
190
+ </div>
191
+ <div class="sys-row">
192
+ <span>Gradio UI</span>
193
+ <span class="sys-val">__GRADIO_UI__</span>
194
+ </div>
195
+
196
+ <!-- ๐ŸŒŸ ๆจกๅž‹ๅˆ—่กจไธ“ๅฑžๅŒบๅ—๏ผŒ็‹ฌ็ซ‹ๆˆ่กŒๅฑ•็คบ -->
197
+ <div class="sys-models-block">
198
+ <div class="sys-label">Models:</div>
199
+ <div class="sys-models-val">[__TARGET_MODEL__]</div>
200
+ </div>
201
+ </div>
202
+
203
+ <!-- ่ทณ่ฝฌๆŒ‰้’ฎ -->
204
+ <a href="__FRONTEND_URL__" class="btn" target="_blank">Launch Studio UI</a>
205
+
206
+ <!-- ๅบ•้ƒจๅฐ้“พๆŽฅ -->
207
+ <div class="footer">
208
+ <a href="https://huggingface.co/spaces/waveshaker/loras_gallery" target="_blank">๐Ÿ–ผ๏ธ Gallery</a>
209
+ <span>โ€ข</span>
210
+ <a href="/docs" target="_blank">๐Ÿ“š API Docs</a>
211
+ </div>
212
+ </div>
213
+ </body>
214
+ </html>