File size: 2,867 Bytes
aba90b6 415b561 9518c9e aba90b6 ccf3b5b 9518c9e 18e34ae 9518c9e 14f66d1 9518c9e d639110 42efe4e 93bc07b 9518c9e 99feee8 7c5f24b 99feee8 8c06d2e 7c5f24b 8c06d2e 94a9eb3 99feee8 18e34ae 094974c aba90b6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import os
import psutil
from functools import lru_cache
#
import cpuinfo
from fastapi import FastAPI, Request, Body
from coreference_resolution import CoreferenceResolver
app = FastAPI()
@lru_cache(maxsize=1)
def get_resolver():
return CoreferenceResolver()
resolver = get_resolver()
@app.post(path="/api/v1/fastcoref")
def get_coref_resolution(text:str = Body(...)) -> dict:
result = resolver.resolve_with_confidence(text)
return result
def get_container_cpu_count():
try:
# default CPU count from Python
cpu_count = os.cpu_count()
# read cgroup quota (if available)
quota_path = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
period_path = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
if os.path.exists(quota_path) and os.path.exists(period_path):
with open(quota_path) as f:
quota = int(f.read())
with open(period_path) as f:
period = int(f.read())
if quota > 0 and period > 0:
cpu_count = quota / period
# else:
# print(f"{quota_path} or {period_path} doesn't exist")
return int(cpu_count)
except Exception:
return os.cpu_count()
def get_container_memory_limit():
try:
mem_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
if os.path.exists(mem_limit_path):
with open(mem_limit_path) as f:
mem_bytes = int(f.read())
return mem_bytes // (1024 * 1024) # in MB
# else:
# print(f"{mem_limit_path} doesn't exist")
# fallback to total system memory
return int(psutil.virtual_memory().total // (1024 * 1024))
except Exception:
return None
# = for cgroup v2
def get_cgroupv2_cpu_limit():
try:
with open("/sys/fs/cgroup/cpu.max") as f:
data = f.read().strip()
quota, period = data.split()
if quota == "max":
print("no CPU limit")
return os.cpu_count() # no limit
return int(int(quota) / int(period))
except Exception as ex:
print(repr(ex))
return os.cpu_count()
def get_cgroupv2_memory_limit():
try:
with open("/sys/fs/cgroup/memory.max") as f:
mem_bytes = f.read().strip()
if mem_bytes == "max":
print("no MEMORY limit")
return int(psutil.virtual_memory().total // (1024*1024)) # no limit
return int(mem_bytes) // (1024*1024) # in MB
except Exception as ex:
print(repr(ex))
return int(psutil.virtual_memory().total // (1024*1024))
@app.get("/")
def greet_json():
return {"Hello": "World!", "cpu_count": get_cgroupv2_cpu_limit(), "total_ram_in_MB": get_cgroupv2_memory_limit(), "cpu_name":cpuinfo.get_cpu_info()['brand_raw']}
@app.get("/health")
def health():
return "ok"
|