forked from luopingyi/xc_agent_platform_demo
56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
|
|
"""Read-only platform pair status; return only fixed statuses and GPU codes."""
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
import urllib.error
|
||
|
|
import urllib.parse
|
||
|
|
import urllib.request
|
||
|
|
from .config_params import GPU_ENUMS
|
||
|
|
from .task_add_client import resolve_token
|
||
|
|
|
||
|
|
SAFE_STATUSES = {'ok', 'transport_error', 'http_error', 'malformed_json',
|
||
|
|
'platform_error', 'malformed_data', 'missing/invalid_token'}
|
||
|
|
|
||
|
|
|
||
|
|
def precheck_model(model, token=None, opener=urllib.request.urlopen):
|
||
|
|
try:
|
||
|
|
token = resolve_token() if token is None else token
|
||
|
|
if not isinstance(token, str) or not token.strip() or '<' in token or '>' in token:
|
||
|
|
return 'missing/invalid_token', {}
|
||
|
|
request = urllib.request.Request(
|
||
|
|
os.environ.get('MODELHUB_API_BASE', 'https://modelhub.org.cn').rstrip('/')
|
||
|
|
+ '/api/computility/models/search-by-model-id?modelId='
|
||
|
|
+ urllib.parse.quote(model, safe=''),
|
||
|
|
headers={'Xc-Token': token, 'Accept': 'application/json'}, method='GET')
|
||
|
|
except Exception:
|
||
|
|
return 'missing/invalid_token', {}
|
||
|
|
try:
|
||
|
|
with opener(request, timeout=30) as response:
|
||
|
|
if response.status != 200:
|
||
|
|
return 'http_error', {}
|
||
|
|
raw = response.read()
|
||
|
|
except urllib.error.HTTPError:
|
||
|
|
return 'http_error', {}
|
||
|
|
except Exception:
|
||
|
|
return 'transport_error', {}
|
||
|
|
try:
|
||
|
|
body = json.loads(raw)
|
||
|
|
except (ValueError, TypeError, UnicodeError):
|
||
|
|
return 'malformed_json', {}
|
||
|
|
if not isinstance(body, dict) or 'code' not in body:
|
||
|
|
return 'malformed_data', {}
|
||
|
|
if type(body['code']) not in (str, int) or str(body['code']) != '0':
|
||
|
|
return 'platform_error', {}
|
||
|
|
data = body.get('data')
|
||
|
|
if not isinstance(data, dict) or not isinstance(data.get('verifyResult'), dict):
|
||
|
|
return 'malformed_data', {}
|
||
|
|
result = {}
|
||
|
|
for gpu, row in data['verifyResult'].items():
|
||
|
|
if gpu not in GPU_ENUMS.values():
|
||
|
|
continue
|
||
|
|
if not isinstance(row, dict) or not isinstance(row.get('result'), str):
|
||
|
|
return 'malformed_data', {}
|
||
|
|
code = {'已验证': 60027, '验证中': 60028}.get(row['result'])
|
||
|
|
if code is not None:
|
||
|
|
result[gpu] = code
|
||
|
|
return 'ok', result
|