update README and SERVICE
This commit is contained in:
@@ -2,6 +2,8 @@ import os
|
||||
import sys
|
||||
import uuid
|
||||
import time
|
||||
import socket
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
# Suppress verbose logs
|
||||
@@ -34,6 +36,49 @@ AVAILABLE_MODELS = {
|
||||
|
||||
# ── CLI: chọn model khi khởi động ────────────────────────────────────────────
|
||||
|
||||
def is_port_available(port: int) -> bool:
|
||||
"""Kiểm tra xem port có khả dụng không."""
|
||||
try:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.bind(("0.0.0.0", port))
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
def select_port(default_port: int = 8000) -> int:
|
||||
"""Chọn port khả dụng."""
|
||||
if is_port_available(default_port):
|
||||
return default_port
|
||||
|
||||
print(f"\n ⚠️ Port {default_port} đã bị chiếm!")
|
||||
while True:
|
||||
try:
|
||||
choice = input(f" Nhập port khác (hoặc Enter để dùng port tự động): ").strip()
|
||||
if not choice:
|
||||
# Tìm port tự động
|
||||
for port in range(8001, 9000):
|
||||
if is_port_available(port):
|
||||
print(f" ✓ Sử dụng port tự động: {port}")
|
||||
return port
|
||||
print(" ✗ Không tìm thấy port khả dụng trong khoảng 8001-8999")
|
||||
continue
|
||||
|
||||
port = int(choice)
|
||||
if port < 1024 or port > 65535:
|
||||
print(" ✗ Port phải trong khoảng 1024-65535")
|
||||
continue
|
||||
|
||||
if is_port_available(port):
|
||||
print(f" ✓ Sử dụng port: {port}")
|
||||
return port
|
||||
else:
|
||||
print(f" ✗ Port {port} đã bị chiếm, vui lòng chọn port khác")
|
||||
except ValueError:
|
||||
print(" ✗ Vui lòng nhập số port hợp lệ")
|
||||
except KeyboardInterrupt:
|
||||
print("\n Thoát.")
|
||||
sys.exit(0)
|
||||
|
||||
def download_model(repo: str, local_dir: Path) -> bool:
|
||||
"""Tải model từ Hugging Face về local."""
|
||||
try:
|
||||
@@ -61,7 +106,20 @@ def download_model(repo: str, local_dir: Path) -> bool:
|
||||
print(f"\n ✗ Lỗi không xác định: {e}")
|
||||
return False
|
||||
|
||||
def select_model() -> Path:
|
||||
def select_model(custom_path: str = None) -> Path:
|
||||
"""Chọn model để sử dụng."""
|
||||
|
||||
# Nếu có custom path, kiểm tra và sử dụng luôn
|
||||
if custom_path:
|
||||
custom_model = Path(custom_path)
|
||||
if custom_model.exists() and custom_model.suffix == ".litertlm":
|
||||
print(f"\n ✓ Sử dụng model từ đường dẫn: {custom_model}")
|
||||
return custom_model
|
||||
else:
|
||||
print(f"\n ✗ Không tìm thấy model tại: {custom_path}")
|
||||
print(f" Vui lòng kiểm tra lại đường dẫn.\n")
|
||||
sys.exit(1)
|
||||
|
||||
print("\n" + "="*52)
|
||||
print(" LiteRT-LM Server — Chọn model")
|
||||
print("="*52)
|
||||
@@ -73,10 +131,30 @@ def select_model() -> Path:
|
||||
print(f" {info['desc']}")
|
||||
print(f" {status}")
|
||||
print()
|
||||
|
||||
print(f" [3] Sử dụng model từ đường dẫn khác")
|
||||
print()
|
||||
|
||||
while True:
|
||||
try:
|
||||
choice = input("Chọn model (1/2): ").strip()
|
||||
choice = input("Chọn model (1/2/3): ").strip()
|
||||
|
||||
# Tùy chọn 3: Đường dẫn tùy chỉnh
|
||||
if choice == "3":
|
||||
custom_path = input("\n Nhập đường dẫn đầy đủ tới file .litertlm: ").strip()
|
||||
custom_model = Path(custom_path)
|
||||
if custom_model.exists() and custom_model.suffix == ".litertlm":
|
||||
print(f"\n Đã chọn: {custom_model.name}")
|
||||
print(f" Path: {custom_model}\n")
|
||||
return custom_model
|
||||
else:
|
||||
print(f"\n ✗ Không tìm thấy file model hợp lệ tại: {custom_path}")
|
||||
retry = input(" Thử lại? (y/n): ").strip().lower()
|
||||
if retry == "y":
|
||||
continue
|
||||
else:
|
||||
sys.exit(0)
|
||||
|
||||
idx = int(choice) - 1
|
||||
if 0 <= idx < len(AVAILABLE_MODELS):
|
||||
key = list(AVAILABLE_MODELS.keys())[idx]
|
||||
@@ -122,14 +200,43 @@ def select_model() -> Path:
|
||||
print(f" Path: {model_path}\n")
|
||||
return model_path
|
||||
else:
|
||||
print(" Vui lòng nhập 1 hoặc 2.")
|
||||
print(" Vui lòng nhập 1, 2 hoặc 3.")
|
||||
except (ValueError, KeyboardInterrupt):
|
||||
print("\n Thoát.")
|
||||
sys.exit(0)
|
||||
|
||||
# Chọn model trước khi FastAPI khởi động
|
||||
# Parse command line arguments
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="LiteRT-LM Server - Local AI inference server",
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog="""
|
||||
Ví dụ:
|
||||
python server.py
|
||||
python server.py --port 8080
|
||||
python server.py --model /path/to/model.litertlm
|
||||
python server.py --port 8080 --model /path/to/model.litertlm
|
||||
"""
|
||||
)
|
||||
parser.add_argument(
|
||||
"--port", "-p",
|
||||
type=int,
|
||||
default=8000,
|
||||
help="Port để chạy server (mặc định: 8000)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--model", "-m",
|
||||
type=str,
|
||||
default=None,
|
||||
help="Đường dẫn đầy đủ tới file model .litertlm"
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
||||
# Parse arguments và chọn model trước khi FastAPI khởi động
|
||||
args = parse_args()
|
||||
MODELS_DIR.mkdir(exist_ok=True)
|
||||
MODEL_PATH = select_model()
|
||||
MODEL_PATH = select_model(args.model)
|
||||
SERVER_PORT = select_port(args.port)
|
||||
|
||||
# ── Models ───────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -263,4 +370,9 @@ async def web_ui():
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
print(f"\n{'='*52}")
|
||||
print(f" 🚀 Server đang khởi động...")
|
||||
print(f" 📍 URL: http://localhost:{SERVER_PORT}")
|
||||
print(f" 📦 Model: {MODEL_PATH.name}")
|
||||
print(f"{'='*52}\n")
|
||||
uvicorn.run(app, host="0.0.0.0", port=SERVER_PORT)
|
||||
|
||||
Reference in New Issue
Block a user