Update main.py
Browse files
main.py
CHANGED
|
@@ -5,7 +5,6 @@ import time
|
|
| 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,10 +12,12 @@ import uvicorn
|
|
| 13 |
from pydantic import BaseModel
|
| 14 |
from typing import List, Optional
|
| 15 |
|
|
|
|
| 16 |
try:
|
| 17 |
-
import
|
| 18 |
except ImportError:
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
from vietnamese_ai.serving.batch_server import MayChuBatch # type: ignore
|
|
@@ -60,25 +61,6 @@ batch_server = MayChuBatch(
|
|
| 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:
|
|
@@ -86,7 +68,7 @@ def start_heartbeat(port):
|
|
| 86 |
"role": "Gateway",
|
| 87 |
"port": port,
|
| 88 |
"public_url": os.environ.get("SPACE_HOST", ""),
|
| 89 |
-
"metrics":
|
| 90 |
}, timeout=5)
|
| 91 |
except Exception:
|
| 92 |
pass
|
|
|
|
| 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 |
from pydantic import BaseModel
|
| 13 |
from typing import List, Optional
|
| 14 |
|
| 15 |
+
# Import shared metrics helper (cùng folder, không cần path manipulation)
|
| 16 |
try:
|
| 17 |
+
from shared_metrics import get_hf_metrics
|
| 18 |
except ImportError:
|
| 19 |
+
def get_hf_metrics():
|
| 20 |
+
return {"total_memory_mb": 16384, "used_memory_mb": 4096, "cpu_count": 2, "cpu_percent": 0, "platform": "Linux", "max_workers": 6, "hardware_tier": "cpu-basic", "gpu": None}
|
| 21 |
|
| 22 |
try:
|
| 23 |
from vietnamese_ai.serving.batch_server import MayChuBatch # type: ignore
|
|
|
|
| 61 |
so_worker=4
|
| 62 |
)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def start_heartbeat(port):
|
| 65 |
while True:
|
| 66 |
try:
|
|
|
|
| 68 |
"role": "Gateway",
|
| 69 |
"port": port,
|
| 70 |
"public_url": os.environ.get("SPACE_HOST", ""),
|
| 71 |
+
"metrics": get_hf_metrics()
|
| 72 |
}, timeout=5)
|
| 73 |
except Exception:
|
| 74 |
pass
|