Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6e9f2e7a0 | ||
|
|
821edbc8f5 | ||
|
|
5e2df178e1 | ||
|
|
761e74ff82 | ||
|
|
09ac80f1ec | ||
|
|
3975a3370a | ||
|
|
7313f8da67 |
204
main.py
204
main.py
@@ -121,22 +121,28 @@ def init_db():
|
||||
# ModelScope 搜索
|
||||
# ============================================================
|
||||
|
||||
def search_models(keyword: str, limit: int = 100) -> list:
|
||||
def search_models(keyword: str, limit: int = 50) -> list:
|
||||
"""从 ModelScope 搜索模型"""
|
||||
url = f"{MODELSCOPE_API}/models"
|
||||
url = "https://modelscope.cn/openapi/v1/models"
|
||||
params = {
|
||||
'Query': keyword,
|
||||
'PageSize': limit,
|
||||
'SortBy': 'GITHUB_SCORE',
|
||||
'search': keyword,
|
||||
'page_size': min(limit, 50), # API 上限 50
|
||||
'page_number': 1,
|
||||
'sort': 'downloads',
|
||||
}
|
||||
try:
|
||||
resp = requests.get(url, params=params, timeout=15)
|
||||
resp = requests.get(url, params=params, timeout=20,
|
||||
headers={'User-Agent': 'Mozilla/5.0'})
|
||||
data = resp.json()
|
||||
models = data.get('Data', {}).get('Models', [])
|
||||
return models
|
||||
if data.get('success'):
|
||||
models = data.get('data', {}).get('models', [])
|
||||
log(f" 搜索 [{keyword}]: status={resp.status_code} models={len(models)}")
|
||||
return models
|
||||
else:
|
||||
log(f" 搜索 [{keyword}]: success=false, data={str(data)[:200]}")
|
||||
except Exception as e:
|
||||
log(f" 搜索失败 [{keyword}]: {e}")
|
||||
return []
|
||||
return []
|
||||
|
||||
|
||||
def check_architecture(model_id: str) -> tuple:
|
||||
@@ -367,16 +373,16 @@ def run_pipeline(gpus: list = None, submit_limit: int = 30):
|
||||
for kw in SEARCH_KEYWORDS:
|
||||
models = search_models(kw, limit=100)
|
||||
for m in models:
|
||||
mid = m.get('ModelId', m.get('Name', ''))
|
||||
mid = m.get('id', '')
|
||||
if mid and mid not in seen:
|
||||
seen.add(mid)
|
||||
downloads = m.get('Downloads', 0)
|
||||
downloads = m.get('downloads', 0)
|
||||
if downloads >= 50:
|
||||
all_models.append({
|
||||
'model_id': mid,
|
||||
'url': f"https://modelscope.cn/{mid}",
|
||||
'downloads': downloads,
|
||||
'params': m.get('Parameters', ''),
|
||||
'params': m.get('params', ''),
|
||||
'category': 'quantized' if 'GGUF' in mid.upper() else 'standard',
|
||||
})
|
||||
time.sleep(0.3)
|
||||
@@ -481,6 +487,8 @@ class AgentHandler(BaseHTTPRequestHandler):
|
||||
'last_run': state['last_run'],
|
||||
'last_result': state['last_result'],
|
||||
})
|
||||
elif path == '/test':
|
||||
self._json(self._run_connectivity_test())
|
||||
elif path == '/status':
|
||||
self._json({
|
||||
'running': state['running'],
|
||||
@@ -532,6 +540,85 @@ class AgentHandler(BaseHTTPRequestHandler):
|
||||
else:
|
||||
self._json({'error': 'not found'}, 404)
|
||||
|
||||
def _run_connectivity_test(self) -> dict:
|
||||
"""测试各 API 连通性"""
|
||||
results = {}
|
||||
|
||||
# 1. ModelScope 搜索 API
|
||||
try:
|
||||
resp = requests.get(
|
||||
'https://modelscope.cn/openapi/v1/models',
|
||||
params={'search': 'qwen', 'page_size': 2, 'sort': 'downloads'},
|
||||
timeout=10,
|
||||
headers={'User-Agent': 'Mozilla/5.0'}
|
||||
)
|
||||
results['modelscope_search'] = {
|
||||
'status': resp.status_code,
|
||||
'ok': resp.status_code == 200,
|
||||
'body_preview': resp.text[:200] if resp.status_code == 200 else resp.text[:100],
|
||||
}
|
||||
except Exception as e:
|
||||
results['modelscope_search'] = {'ok': False, 'error': str(e)}
|
||||
|
||||
# 2. ModelScope config.json API
|
||||
try:
|
||||
resp = requests.get(
|
||||
'https://modelscope.cn/api/v1/models/Qwen/Qwen3-8B/repo?Revision=master&FilePath=config.json',
|
||||
timeout=10,
|
||||
)
|
||||
results['modelscope_config'] = {
|
||||
'status': resp.status_code,
|
||||
'ok': resp.status_code == 200,
|
||||
'body_preview': resp.text[:200] if resp.status_code == 200 else resp.text[:100],
|
||||
}
|
||||
except Exception as e:
|
||||
results['modelscope_config'] = {'ok': False, 'error': str(e)}
|
||||
|
||||
# 3. ModelHub 查询 API
|
||||
try:
|
||||
token = list(ACCOUNTS.values())[0]
|
||||
resp = requests.get(
|
||||
'https://modelhub.org.cn/api/adapt/task/page',
|
||||
headers={'Xc-Token': token, 'Accept': 'application/json'},
|
||||
params={'current': 1, 'pageSize': 1, 'onlyMine': 'true'},
|
||||
timeout=10,
|
||||
)
|
||||
data = resp.json()
|
||||
results['modelhub_query'] = {
|
||||
'ok': data.get('code') == 0,
|
||||
'code': data.get('code'),
|
||||
'total': data.get('data', {}).get('total'),
|
||||
}
|
||||
except Exception as e:
|
||||
results['modelhub_query'] = {'ok': False, 'error': str(e)}
|
||||
|
||||
# 4. ModelHub 提交 API (dry test)
|
||||
try:
|
||||
token = list(ACCOUNTS.values())[0]
|
||||
resp = requests.post(
|
||||
'https://modelhub.org.cn/api/adapt/task/add',
|
||||
headers={'Xc-Token': token, 'Accept': 'application/json', 'Content-Type': 'application/json'},
|
||||
json={
|
||||
'modelAddress': 'https://www.modelscope.cn/models/Qwen/Qwen3-8B',
|
||||
'taskType': 'text-generation',
|
||||
'targetGpu': 'Kunlunxin_p-800',
|
||||
'framework': 'vllm',
|
||||
'strategyId': STRATEGY_ID,
|
||||
'configParams': 'framework: vllm\n',
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
data = resp.json()
|
||||
results['modelhub_submit'] = {
|
||||
'ok': data.get('code') == 0,
|
||||
'code': data.get('code'),
|
||||
'message': data.get('message', '')[:100],
|
||||
}
|
||||
except Exception as e:
|
||||
results['modelhub_submit'] = {'ok': False, 'error': str(e)}
|
||||
|
||||
return results
|
||||
|
||||
def _json(self, body: dict, status: int = 200):
|
||||
payload = json.dumps(body, ensure_ascii=False).encode()
|
||||
self.send_response(status)
|
||||
@@ -570,6 +657,99 @@ def main():
|
||||
log(f"STRATEGY_ID: {STRATEGY_ID}")
|
||||
log(f"GPU: {', '.join(GPU_CONFIGS.keys())}")
|
||||
|
||||
# 启动后自动运行连通性测试
|
||||
def _startup_test():
|
||||
time.sleep(2)
|
||||
log("=" * 50)
|
||||
log("连通性测试开始")
|
||||
log("=" * 50)
|
||||
|
||||
# 1. ModelScope 搜索
|
||||
try:
|
||||
resp = requests.get(
|
||||
'https://modelscope.cn/openapi/v1/models',
|
||||
params={'search': 'qwen', 'page_size': 2, 'sort': 'downloads'},
|
||||
timeout=10, headers={'User-Agent': 'Mozilla/5.0'}
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
models = data.get('data', {}).get('models', [])
|
||||
log(f"[ModelScope搜索] ✅ status={resp.status_code} models={len(models)}")
|
||||
for m in models[:2]:
|
||||
log(f" {m.get('id')} downloads={m.get('downloads')}")
|
||||
else:
|
||||
log(f"[ModelScope搜索] ❌ status={resp.status_code} body={resp.text[:100]}")
|
||||
except Exception as e:
|
||||
log(f"[ModelScope搜索] ❌ error={e}")
|
||||
|
||||
# 2. ModelScope config.json
|
||||
try:
|
||||
resp = requests.get(
|
||||
'https://modelscope.cn/api/v1/models/Qwen/Qwen3-8B/repo?Revision=master&FilePath=config.json',
|
||||
timeout=10
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
cfg = resp.json()
|
||||
log(f"[ModelScope配置] ✅ arch={cfg.get('architectures')} type={cfg.get('model_type')}")
|
||||
else:
|
||||
log(f"[ModelScope配置] ❌ status={resp.status_code}")
|
||||
except Exception as e:
|
||||
log(f"[ModelScope配置] ❌ error={e}")
|
||||
|
||||
# 3. ModelHub 查询
|
||||
try:
|
||||
token = list(ACCOUNTS.values())[0]
|
||||
resp = requests.get(
|
||||
'https://modelhub.org.cn/api/adapt/task/page',
|
||||
headers={'Xc-Token': token, 'Accept': 'application/json'},
|
||||
params={'current': 1, 'pageSize': 1, 'onlyMine': 'true'},
|
||||
timeout=10
|
||||
)
|
||||
data = resp.json()
|
||||
if data.get('code') == 0:
|
||||
log(f"[ModelHub查询] ✅ total={data['data'].get('total')}")
|
||||
else:
|
||||
log(f"[ModelHub查询] ❌ code={data.get('code')} msg={data.get('message','')[:80]}")
|
||||
except Exception as e:
|
||||
log(f"[ModelHub查询] ❌ error={e}")
|
||||
|
||||
# 4. ModelHub 提交
|
||||
try:
|
||||
token = list(ACCOUNTS.values())[0]
|
||||
resp = requests.post(
|
||||
'https://modelhub.org.cn/api/adapt/task/add',
|
||||
headers={'Xc-Token': token, 'Accept': 'application/json', 'Content-Type': 'application/json'},
|
||||
json={
|
||||
'modelAddress': 'https://www.modelscope.cn/models/Qwen/Qwen3-8B',
|
||||
'taskType': 'text-generation', 'targetGpu': 'Kunlunxin_p-800',
|
||||
'framework': 'vllm', 'strategyId': STRATEGY_ID,
|
||||
'configParams': 'framework: vllm\n',
|
||||
}, timeout=10
|
||||
)
|
||||
data = resp.json()
|
||||
log(f"[ModelHub提交] code={data.get('code')} msg={data.get('message','')[:80]}")
|
||||
except Exception as e:
|
||||
log(f"[ModelHub提交] ❌ error={e}")
|
||||
|
||||
log("=" * 50)
|
||||
log("连通性测试完成")
|
||||
log("=" * 50)
|
||||
|
||||
# 开始正式提交流程
|
||||
log("\n自动触发提交流程...")
|
||||
try:
|
||||
state['running'] = True
|
||||
state['last_run'] = datetime.now().isoformat()
|
||||
count = run_pipeline(submit_limit=30)
|
||||
state['last_result'] = {'submitted': count, 'success': True}
|
||||
except Exception as e:
|
||||
log(f"流程异常: {traceback.format_exc()}")
|
||||
state['last_result'] = {'error': str(e), 'success': False}
|
||||
finally:
|
||||
state['running'] = False
|
||||
|
||||
threading.Thread(target=_startup_test, daemon=True).start()
|
||||
|
||||
while not shutdown_requested:
|
||||
server.handle_request()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user