12 Commits

2 changed files with 115 additions and 76 deletions

View File

@@ -4,6 +4,8 @@ ENV PYTHONUNBUFFERED=1
WORKDIR /app
RUN mkdir -p /app/data
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

161
main.py
View File

@@ -31,7 +31,7 @@ PORT = 8080
STRATEGY_ID = os.getenv("STRATEGY_ID", "")
# 目标GPU
TARGET_GPU = "ppu_zw_810e"
TARGET_GPU = "Iluvatar_bi-100"
# 账号Token
TARGET_TOKEN = "f45f1aae2c094426be237c88b1085015"
@@ -47,7 +47,7 @@ SUPPORTED_SPECIAL_ARCHS = ['Eagle3Speculator', 'LlamaForCausalLMEagle3']
MODELHUB_API = "https://modelhub.org.cn/api"
# 搜索关键词
SEARCH_KEYWORDS = ['qwen', 'Qwen2', 'Qwen3', 'Qwen3.5', 'Qwen1.5', 'Qwen-']
SEARCH_KEYWORDS = ['qwen', 'Qwen2', 'Qwen3', 'Qwen3.5', 'Llama-3', 'Llama-3.1', 'Mistral', 'DeepSeek']
# ============================================================
# 全局状态
@@ -81,16 +81,35 @@ db_conn = None
def init_db():
global db_conn
db_conn = sqlite3.connect(':memory:', check_same_thread=False)
db_conn.execute('''CREATE TABLE IF NOT EXISTS queue (
model_id TEXT, gpu TEXT, url TEXT, downloads INTEGER,
params TEXT, category TEXT, score REAL,
PRIMARY KEY(model_id, gpu)
)''')
os.makedirs('/app/data', exist_ok=True)
db_conn = sqlite3.connect('/app/data/submit_history.db', check_same_thread=False)
db_conn.execute('''CREATE TABLE IF NOT EXISTS submitted (
model_id TEXT, gpu TEXT, task_id TEXT, submitted_at TEXT,
model_id TEXT, gpu TEXT, task_id TEXT, status TEXT,
submitted_at TEXT, checked_at TEXT,
PRIMARY KEY(model_id, gpu)
)''')
db_conn.execute('''CREATE TABLE IF NOT EXISTS failed (
model_id TEXT, gpu TEXT, reason TEXT, failed_at TEXT,
PRIMARY KEY(model_id, gpu)
)''')
db_conn.commit()
def is_model_failed(model_id: str) -> bool:
"""检查模型是否已知失败"""
if db_conn:
row = db_conn.execute(
'SELECT 1 FROM failed WHERE model_id=? AND gpu=?',
(model_id, TARGET_GPU)
).fetchone()
return row is not None
return False
def record_failed(model_id: str, reason: str):
"""记录失败的模型"""
if db_conn:
db_conn.execute(
'INSERT OR REPLACE INTO failed VALUES (?,?,?,?)',
(model_id, TARGET_GPU, reason, datetime.now().isoformat())
)
db_conn.commit()
@@ -99,15 +118,20 @@ def init_db():
# ============================================================
MODELSCOPE_API = "https://modelscope.cn/api/v1"
DOWNLOAD_MIN = 50
DOWNLOAD_MAX = 5000
SEARCH_PAGES = 5 # 每个关键词搜5页50*5=250个结果
def search_models(keyword: str, limit: int = 50) -> list:
"""从 ModelScope 搜索模型"""
def search_models(keyword: str) -> list:
"""从 ModelScope 搜索模型多页筛选下载量50-5000的冷门模型"""
url = "https://modelscope.cn/openapi/v1/models"
models = []
for page in range(1, SEARCH_PAGES + 1):
params = {
'search': keyword,
'page_size': min(limit, 50),
'page_number': 1,
'page_size': 50,
'page_number': page,
'sort': 'downloads',
}
try:
@@ -115,14 +139,21 @@ def search_models(keyword: str, limit: int = 50) -> list:
headers={'User-Agent': 'Mozilla/5.0'})
data = resp.json()
if data.get('success'):
models = data.get('data', {}).get('models', [])
log(f" [{keyword}]: {len(models)} 个结果")
return [{'id': m.get('id'), 'downloads': m.get('downloads', 0)} for m in models]
page_models = data.get('data', {}).get('models', [])
for m in page_models:
dl = m.get('downloads', 0)
if DOWNLOAD_MIN <= dl <= DOWNLOAD_MAX:
models.append({'id': m.get('id'), 'downloads': dl})
if len(page_models) < 50:
break # 最后一页,不继续
else:
log(f" [{keyword}]: success=false")
break
except Exception as e:
log(f" [{keyword}]: 失败 {e}")
return []
log(f" [{keyword}] page={page}: {e}")
break
time.sleep(0.3)
log(f" [{keyword}]: {len(models)} 个 (50<={DOWNLOAD_MAX})")
return models
def check_architecture(model_id: str) -> tuple:
@@ -223,25 +254,26 @@ def check_queue_available() -> int:
def build_config_params() -> str:
"""构建 YAML 配置 - 完全匹配平台自动生成的格式只支持vllm"""
"""构建 YAML 配置 - llama.cpp (支持 GGUF)"""
params = {
'framework': 'vllm',
'nv_framework': 'vllm',
'framework': 'llama.cpp',
'nv_framework': 'llama.cpp',
'api': 'completion',
'max_tokens': 1024,
'temperature': 0.7,
'repetition_penalty': 1.2,
'top_p': 0.9,
'lang': 'zh',
'max_model_len': 2048,
'max_model_len': 4096,
'sut_config': {
'gpu_num': 1,
'values': {
'command': [
'vllm', 'serve', '/model', '--port', '8000',
'--served-model-name', 'llm', '--max-model-len', '2048',
'--dtype', 'auto', '--gpu-memory-utilization', '0.95',
'-tp', '1', '--enforce-eager', '--trust-remote-code',
'llama-server', '--model', '/model', '--alias', 'llm',
'--threads', '20', '--n-gpu-layers', '999', '--prio', '3',
'--min_p', '0.01', '--ctx-size', '4096',
'--host', '0.0.0.0', '--port', '8000',
'--jinja', '--flash-attn', 'off',
]
}
},
@@ -249,9 +281,9 @@ def build_config_params() -> str:
'gpu_num': 1,
'values': {
'command': [
'vllm', 'serve', '/model', '--port', '80',
'--served-model-name', 'llm', '--max-model-len', '4096',
'--enforce-eager', '--trust-remote-code', '-tp', '1',
'llama-server', '--model', '/model', '--alias', 'llm',
'--threads', '20', '--n-gpu-layers', '999',
'--ctx-size', '4096', '--host', '0.0.0.0', '--port', '8000',
]
}
},
@@ -271,7 +303,7 @@ def submit_model(model_url: str) -> tuple:
'modelAddress': normalize_model_url(model_url),
'taskType': 'text-generation',
'targetGpu': TARGET_GPU,
'framework': 'vllm',
'framework': 'llama.cpp',
'strategyId': STRATEGY_ID,
'configParams': build_config_params(),
}
@@ -290,7 +322,7 @@ def submit_model(model_url: str) -> tuple:
# 主流程
# ============================================================
def run_pipeline(submit_limit: int = 30):
def run_pipeline(submit_limit: int = 2):
"""完整流程搜索→筛选→提交只针对目标GPU"""
init_db()
log("=" * 50)
@@ -303,47 +335,45 @@ def run_pipeline(submit_limit: int = 30):
seen = set()
all_models = []
for kw in SEARCH_KEYWORDS:
models = search_models(kw, limit=100)
models = search_models(kw)
for m in models:
mid = m.get('id', '')
if mid and mid not in seen:
seen.add(mid)
downloads = m.get('downloads', 0)
if downloads >= 50:
all_models.append({
'model_id': mid,
'url': f"https://modelscope.cn/{mid}",
'downloads': downloads,
'downloads': m.get('downloads', 0),
})
time.sleep(0.3)
log(f"搜索完成: {len(seen)} 个唯一模型, {len(all_models)} 个下载量>=50")
log(f"搜索完成: {len(seen)} 个唯一模型, {len(all_models)} 个下载量{DOWNLOAD_MIN}-{DOWNLOAD_MAX}")
# 2. 格式筛选(只保留HuggingFace格式排除GGUF
# 2. 格式筛选(排除 GPTQ/AWQ保留 GGUF 和 HuggingFace
log("\n--- 阶段2: 格式筛选 ---")
hf_models = []
gguf_skipped = 0
format_skipped = 0
SKIP_FORMATS = ['GPTQ', 'AWQ']
for m in all_models:
mid = m['model_id'].upper()
if 'GGUF' in mid:
gguf_skipped += 1
log(f"{m['model_id']}: GGUF格式跳过")
else:
mid_upper = m['model_id'].upper()
skip = False
for fmt in SKIP_FORMATS:
if fmt in mid_upper:
format_skipped += 1
log(f" x {m['model_id']}: {fmt}格式,跳过")
skip = True
break
if not skip:
hf_models.append(m)
log(f"格式筛选: {len(hf_models)} 通过 (HuggingFace), {gguf_skipped} 跳过 (GGUF)")
log(f"格式筛选: {len(hf_models)} 通过, {format_skipped} 跳过 (GPTQ/AWQ)")
# 3. 架构筛选
log("\n--- 阶段3: 架构筛选 ---")
arch_passed = []
arch_rejected = 0
# 3. 架构筛选(参考检查,不做严格过滤,让平台决定兼容性)
log("\n--- 阶段3: 架构检查 ---")
for m in hf_models:
ok, reason = check_architecture(m['model_id'])
if ok:
arch_passed.append(m)
else:
arch_rejected += 1
log(f"{m['model_id']}: {reason}")
time.sleep(0.15)
log(f"架构筛选: {len(arch_passed)} 通过, {arch_rejected} 拒绝")
if not ok:
log(f" ! {m['model_id']}: {reason} (仍保留)")
time.sleep(0.1)
log(f"架构检查: {len(hf_models)} 个模型进入下一阶段")
# 4. 筛选并提交只针对目标GPU
log(f"\n--- 阶段4: 筛选并提交 [{TARGET_GPU}] ---")
@@ -357,7 +387,7 @@ def run_pipeline(submit_limit: int = 30):
# 筛选
to_submit = []
for m in arch_passed:
for m in hf_models:
model_id = m['model_id']
# 检查全平台验证状态
@@ -369,6 +399,10 @@ def run_pipeline(submit_limit: int = 30):
if check_my_submitted(model_id):
continue
# 检查是否已知失败(避免重复提交)
if is_model_failed(model_id):
continue
to_submit.append(m)
if len(to_submit) >= min(submit_limit, available):
break
@@ -385,10 +419,13 @@ def run_pipeline(submit_limit: int = 30):
log(f"{m['model_id']}")
db_conn.execute(
'INSERT OR REPLACE INTO submitted VALUES (?,?,?,?)',
(m['model_id'], gpu, str(task_id), datetime.now().isoformat())
(m['model_id'], TARGET_GPU, str(task_id), datetime.now().isoformat())
)
else:
log(f"{m['model_id']}: {msg}")
# 永久失败类型记录到 failed 表
if any(kw in str(msg) for kw in ['保护期', '白名单', '唯一性']):
record_failed(m['model_id'], msg)
time.sleep(0.5)
log(f" 提交完成: {submitted}/{len(to_submit)}")
@@ -449,7 +486,7 @@ class AgentHandler(BaseHTTPRequestHandler):
if content_len > 0:
body = json.loads(self.rfile.read(content_len))
limit = body.get('limit', 30)
limit = body.get('limit', 2)
self._json({'status': 'started', 'gpu': TARGET_GPU, 'limit': limit})
@@ -530,7 +567,7 @@ class AgentHandler(BaseHTTPRequestHandler):
'modelAddress': 'https://www.modelscope.cn/models/Qwen/Qwen3-8B',
'taskType': 'text-generation',
'targetGpu': TARGET_GPU,
'framework': 'vllm',
'framework': 'llama.cpp',
'strategyId': STRATEGY_ID,
'configParams': build_config_params(),
},
@@ -648,7 +685,7 @@ def main():
json={
'modelAddress': 'https://www.modelscope.cn/models/Qwen/Qwen3-8B',
'taskType': 'text-generation', 'targetGpu': TARGET_GPU,
'framework': 'vllm', 'strategyId': STRATEGY_ID,
'framework': 'llama.cpp', 'strategyId': STRATEGY_ID,
'configParams': build_config_params(),
}, timeout=10
)
@@ -666,7 +703,7 @@ def main():
try:
state['running'] = True
state['last_run'] = datetime.now().isoformat()
count = run_pipeline(submit_limit=30)
count = run_pipeline(submit_limit=2)
state['last_result'] = {'submitted': count, 'success': True}
except Exception as e:
log(f"流程异常: {traceback.format_exc()}")