Update main.py
Browse files
main.py
CHANGED
|
@@ -5,6 +5,7 @@ import time
|
|
| 5 |
import requests
|
| 6 |
import asyncio
|
| 7 |
import threading
|
|
|
|
| 8 |
from fastapi import FastAPI, Request, HTTPException, Depends
|
| 9 |
from fastapi.responses import JSONResponse
|
| 10 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
@@ -12,6 +13,11 @@ import uvicorn
|
|
| 12 |
from pydantic import BaseModel
|
| 13 |
from typing import List, Optional
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
from vietnamese_ai.serving.batch_server import MayChuBatch # type: ignore
|
| 17 |
from vietnamese_ai.llm.vietnamese_llm import VietnameseLLM # type: ignore
|
|
@@ -54,12 +60,33 @@ batch_server = MayChuBatch(
|
|
| 54 |
so_worker=4
|
| 55 |
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def start_heartbeat(port):
|
| 58 |
while True:
|
| 59 |
try:
|
| 60 |
requests.post(f"{CENTER_URL}/api/v1/admin/datacenter/register", json={
|
| 61 |
"role": "Gateway",
|
| 62 |
-
"port": port
|
|
|
|
|
|
|
| 63 |
}, timeout=5)
|
| 64 |
except Exception:
|
| 65 |
pass
|
|
|
|
| 5 |
import requests
|
| 6 |
import asyncio
|
| 7 |
import threading
|
| 8 |
+
import platform
|
| 9 |
from fastapi import FastAPI, Request, HTTPException, Depends
|
| 10 |
from fastapi.responses import JSONResponse
|
| 11 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
|
|
| 13 |
from pydantic import BaseModel
|
| 14 |
from typing import List, Optional
|
| 15 |
|
| 16 |
+
try:
|
| 17 |
+
import psutil
|
| 18 |
+
except ImportError:
|
| 19 |
+
psutil = None
|
| 20 |
+
|
| 21 |
try:
|
| 22 |
from vietnamese_ai.serving.batch_server import MayChuBatch # type: ignore
|
| 23 |
from vietnamese_ai.llm.vietnamese_llm import VietnameseLLM # type: ignore
|
|
|
|
| 60 |
so_worker=4
|
| 61 |
)
|
| 62 |
|
| 63 |
+
def get_metrics():
|
| 64 |
+
"""Thu thập chỉ số phần cứng thực tế của server"""
|
| 65 |
+
metrics = {
|
| 66 |
+
"total_memory_mb": 0,
|
| 67 |
+
"used_memory_mb": 0,
|
| 68 |
+
"cpu_percent": 0,
|
| 69 |
+
"cpu_count": os.cpu_count() or 1,
|
| 70 |
+
"platform": platform.system(),
|
| 71 |
+
"max_workers": 2,
|
| 72 |
+
}
|
| 73 |
+
if psutil:
|
| 74 |
+
mem = psutil.virtual_memory()
|
| 75 |
+
metrics["total_memory_mb"] = round(mem.total / 1024 / 1024)
|
| 76 |
+
metrics["used_memory_mb"] = round(mem.used / 1024 / 1024)
|
| 77 |
+
metrics["cpu_percent"] = psutil.cpu_percent(interval=0.1)
|
| 78 |
+
# Ước tính workers: mỗi worker cần ~2GB RAM
|
| 79 |
+
metrics["max_workers"] = max(1, int(mem.available / 1024 / 1024 / 2048))
|
| 80 |
+
return metrics
|
| 81 |
+
|
| 82 |
def start_heartbeat(port):
|
| 83 |
while True:
|
| 84 |
try:
|
| 85 |
requests.post(f"{CENTER_URL}/api/v1/admin/datacenter/register", json={
|
| 86 |
"role": "Gateway",
|
| 87 |
+
"port": port,
|
| 88 |
+
"public_url": os.environ.get("SPACE_HOST", ""),
|
| 89 |
+
"metrics": get_metrics()
|
| 90 |
}, timeout=5)
|
| 91 |
except Exception:
|
| 92 |
pass
|