BachDaThan commited on
Commit
bf6ef1a
·
verified ·
1 Parent(s): d6d3ebf

Upload yui_local_model_server.py

Browse files
Files changed (1) hide show
  1. yui_local_model_server.py +79 -12
yui_local_model_server.py CHANGED
@@ -1,5 +1,11 @@
1
  # ================================================================
2
- # yui_local_model_server.py — LOCAL MODEL SERVER v2.1 (2026-09-02)
 
 
 
 
 
 
3
  # Chạy TRÊN AREN (worker Space phụ) — KHÔNG chạy trên Space chính.
4
  #
5
  # 🆕 (2026-09-02) ĐỔI MODEL: Qwen3-4B-Instruct-2507 (Q4_K_M, ~2.5GB)
@@ -423,7 +429,7 @@ def _ensure_model_downloaded() -> bool:
423
 
424
  print(
425
  f"[LOCAL-MODEL] ⬇️ Đang tải {len(_HF_FILENAMES)} file GGUF "
426
- f"từ {_HF_REPO_ID} (revision=main)..."
427
  )
428
  _t0 = time.time()
429
 
@@ -431,22 +437,83 @@ def _ensure_model_downloaded() -> bool:
431
  for filename in _HF_FILENAMES:
432
  target_path = os.path.join(_MODEL_DIR, filename)
433
 
434
- if os.path.exists(target_path):
435
  print(f"[LOCAL-MODEL] ✅ Đã có file: {filename}")
436
  downloaded_paths.append(target_path)
437
  continue
438
 
439
  print(f"[LOCAL-MODEL] ⬇️ Tải file: {filename}")
440
- downloaded_path = hf_hub_download(
441
- repo_id=_HF_REPO_ID,
442
- filename=filename,
443
- revision="main",
444
- local_dir=_MODEL_DIR,
445
- )
446
 
447
- if downloaded_path != target_path and os.path.exists(downloaded_path):
448
- if not os.path.exists(target_path):
449
- os.rename(downloaded_path, target_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
 
451
  downloaded_paths.append(target_path)
452
 
 
1
  # ================================================================
2
+ # yui_local_model_server.py — LOCAL MODEL SERVER v2.2 (2026-09-02)
3
+ # 🆕 v2.2 FIX (2026-09-02): HF trả 404 dù repo/file SeaLLMs hiện tại tồn tại.
4
+ # Downloader không còn phụ thuộc cứng vào revision="main" của hf_hub_download;
5
+ # thử huggingface_hub trước, sau đó tự fallback sang HTTP resolve trực tiếp.
6
+ # File được tải theo stream vào .part và chỉ rename khi đủ kích thước, tránh
7
+ # model GGUF dở dang bị coi là đã tải xong.
8
+
9
  # Chạy TRÊN AREN (worker Space phụ) — KHÔNG chạy trên Space chính.
10
  #
11
  # 🆕 (2026-09-02) ĐỔI MODEL: Qwen3-4B-Instruct-2507 (Q4_K_M, ~2.5GB)
 
429
 
430
  print(
431
  f"[LOCAL-MODEL] ⬇️ Đang tải {len(_HF_FILENAMES)} file GGUF "
432
+ f"từ {_HF_REPO_ID} (v2.2 downloader)..."
433
  )
434
  _t0 = time.time()
435
 
 
437
  for filename in _HF_FILENAMES:
438
  target_path = os.path.join(_MODEL_DIR, filename)
439
 
440
+ if os.path.exists(target_path) and os.path.getsize(target_path) > 100 * 1024 * 1024:
441
  print(f"[LOCAL-MODEL] ✅ Đã có file: {filename}")
442
  downloaded_paths.append(target_path)
443
  continue
444
 
445
  print(f"[LOCAL-MODEL] ⬇️ Tải file: {filename}")
446
+ downloaded_ok = False
 
 
 
 
 
447
 
448
+ # Cách 1: huggingface_hub. Không ép revision='main' để tránh
449
+ # một số bản hub client cũ xử lý revision/resolve không ổn định.
450
+ try:
451
+ downloaded_path = hf_hub_download(
452
+ repo_id=_HF_REPO_ID,
453
+ filename=filename,
454
+ local_dir=_MODEL_DIR,
455
+ )
456
+ if downloaded_path != target_path and os.path.exists(downloaded_path):
457
+ if not os.path.exists(target_path):
458
+ os.replace(downloaded_path, target_path)
459
+ if os.path.exists(target_path) and os.path.getsize(target_path) > 100 * 1024 * 1024:
460
+ downloaded_ok = True
461
+ print(f"[LOCAL-MODEL] ✅ huggingface_hub tải xong: {filename}")
462
+ except Exception as hub_error:
463
+ print(
464
+ f"[LOCAL-MODEL] ⚠️ huggingface_hub lỗi "
465
+ f"({hub_error.__class__.__name__}): {str(hub_error)[:220]}"
466
+ )
467
+
468
+ # Cách 2: fallback HTTP trực tiếp tới endpoint resolve của HF.
469
+ # Dùng stream + file .part để không giữ ~1GB model trong RAM và
470
+ # không để file GGUF dở dang bị nhận nhầm là model hoàn chỉnh.
471
+ if not downloaded_ok:
472
+ try:
473
+ import requests
474
+
475
+ url = (
476
+ f"https://huggingface.co/{_HF_REPO_ID}/resolve/main/"
477
+ f"{filename}?download=true"
478
+ )
479
+ part_path = target_path + ".part"
480
+ print(f"[LOCAL-MODEL] 🔁 Fallback HTTP: {filename}")
481
+
482
+ with requests.get(url, stream=True, timeout=(20, 1200), allow_redirects=True) as resp:
483
+ resp.raise_for_status()
484
+ content_length = int(resp.headers.get("content-length", "0") or 0)
485
+ if content_length and content_length < 100 * 1024 * 1024:
486
+ raise RuntimeError(
487
+ f"Content-Length bất thường ({content_length} bytes), không phải GGUF hợp lệ"
488
+ )
489
+
490
+ with open(part_path, "wb") as out:
491
+ for chunk in resp.iter_content(chunk_size=8 * 1024 * 1024):
492
+ if chunk:
493
+ out.write(chunk)
494
+
495
+ if not os.path.exists(part_path) or os.path.getsize(part_path) <= 100 * 1024 * 1024:
496
+ raise RuntimeError("File tải về quá nhỏ hoặc rỗng")
497
+
498
+ os.replace(part_path, target_path)
499
+ downloaded_ok = True
500
+ print(f"[LOCAL-MODEL] ✅ HTTP fallback tải xong: {filename}")
501
+ except Exception as http_error:
502
+ try:
503
+ if os.path.exists(target_path + ".part"):
504
+ os.remove(target_path + ".part")
505
+ except Exception:
506
+ pass
507
+ print(
508
+ f"[LOCAL-MODEL] ⚠️ HTTP fallback lỗi "
509
+ f"({http_error.__class__.__name__}): {str(http_error)[:220]}"
510
+ )
511
+
512
+ if not downloaded_ok:
513
+ raise RuntimeError(
514
+ f"Không thể tải GGUF '{filename}' từ Hugging Face bằng cả "
515
+ "huggingface_hub và HTTP fallback."
516
+ )
517
 
518
  downloaded_paths.append(target_path)
519